import{defineComponent as t,h as e,renderSlot as i}from"vue";function s(t){return t.map((t=>{switch(typeof t){case"string":return{value:t,name:t};case"number":case"boolean":return{value:t,name:`${t}`}}return t}))}function n(t){return t.changedTouches||t.touches}function l(t){return n(t)?t.changedTouches[0]||t.touches[0]:t}var o=t({props:{modelValue:null,options:{type:Array,default:()=>[]},dragSensitivity:{type:Number,default:1.7},touchSensitivity:{type:Number,default:1.7},scrollSensitivity:{type:Number,default:1},empty:{type:String,default:"No Items"},placeholder:{type:String,default:null}},data(){var t,e;const i=s(this.options);let n=i.findIndex((t=>t.value==this.modelValue));-1===n&&!this.placeholder&&!this.$slots.placeholder&&this.options.length>0&&(n=0);return{refItems:[],internalOptions:i,internalIndex:n,internalValue:null!=(e=null==(t=i[n])?void 0:t.value)?e:null,pivots:[],pivotsMin:0,pivotsMax:0,scroll:null,scrollOffsetTop:0,scrollMin:0,scrollMax:0,transitioning:!1,transitionTimer:null,start:null,isMouseDown:!1,isDragging:!1}},beforeUpdate(){this.refItems=[]},mounted(){this.calculatePivots(),this.scroll=this.findScrollByIndex(this.internalIndex),this.internalValue!==this.modelValue&&this.$emit("update:modelValue",this.internalValue);const t=this.$el;t.addEventListener("touchstart",this.onStart),t.addEventListener("touchmove",this.onMove),t.addEventListener("touchend",this.onEnd),t.addEventListener("touchcancel",this.onCancel),t.addEventListener("mousewheel",this.onWheel),t.addEventListener("DOMMouseScroll",this.onWheel),t.addEventListener("wheel",this.onWheel),t.addEventListener("mousedown",this.onStart),t.addEventListener("mousemove",this.onMove),t.addEventListener("mouseup",this.onEnd),t.addEventListener("mouseleave",this.onCancel)},beforeUnmount(){const t=this.$el;t.removeEventListener("touchstart",this.onStart),t.removeEventListener("touchmove",this.onMove),t.removeEventListener("touchend",this.onEnd),t.removeEventListener("touchcancel",this.onCancel),t.removeEventListener("mousewheel",this.onWheel),t.removeEventListener("DOMMouseScroll",this.onWheel),t.removeEventListener("wheel",this.onWheel),t.removeEventListener("mousedown",this.onStart),t.removeEventListener("mousemove",this.onMove),t.removeEventListener("mouseup",this.onEnd),t.removeEventListener("mouseleave",this.onCancel)},watch:{modelValue(t){if(null==t&&this.hasPlaceholder)return void this.correction(-1);const e=this.internalOptions.findIndex((e=>e.value==t));-1!==e?this.internalIndex!==e&&this.correction(e):this.$emit("update:modelValue",this.internalValue)},options:{handler(t){var e,i;const n=this.internalOptions=s(t);let l=n.findIndex((t=>t.value==this.modelValue));-1===l&&!this.hasPlaceholder&&this.options.length>0&&(l=0);const o=null!=(i=null==(e=n[l])?void 0:e.value)?i:null;this.$nextTick((()=>{this.calculatePivots(),this.scroll=this.findScrollByIndex(l),this.internalIndex=l,this.internalValue!==o&&this.$emit("update:modelValue",this.internalValue=o)}))},deep:!0}},computed:{hasPlaceholder(){return!(!this.placeholder&&!this.$slots.placeholder)}},methods:{setRefItem(t){this.refItems.push(t)},calculatePivots(){const t=this.$refs.rotator,e=this.$refs.layerSelection,i=t.getBoundingClientRect().top,s=this.pivots=this.refItems.map((t=>function(t){const{top:e,bottom:i}=t.getBoundingClientRect();return(e+i)/2}(t)-i)).sort(((t,e)=>t-e)),n=this.pivotsMin=Math.min(...s),l=this.pivotsMax=Math.max(...s),o=this.scrollOffsetTop=e.offsetTop+e.offsetHeight/2;this.scrollMin=o-n,this.scrollMax=o-l},sanitizeInternalIndex(t){return Math.min(Math.max(t,this.hasPlaceholder?-1:0),this.internalOptions.length-1)},findIndexFromScroll(t){let e=null,i=0;return this.pivots.forEach(((s,n)=>{const l=s+t-this.scrollOffsetTop;(null===e||Math.abs(e)>Math.abs(l))&&(i=n,e=l)})),this.hasPlaceholder||0===this.options.length?i-1:i},findScrollByIndex(t){let e=t;return(this.hasPlaceholder||0===this.options.length)&&e++,t>-1&&e in this.pivots?this.scrollOffsetTop-this.pivots[e]:t>=this.pivots.length?this.scrollOffsetTop-this.pivotsMax:this.scrollOffsetTop-this.pivotsMin},onWheel(t){var e,i;if(this.scroll>=this.scrollMin&&t.deltaY<0)return;if(this.scroll<=this.scrollMax&&t.deltaY>0)return;if(1===this.pivots.length)return;t.preventDefault();const s=this.sanitizeInternalIndex(this.internalIndex+(t.deltaY>0?1:-1)),n=t.deltaY>0?this.findScrollByIndex(s-1)-this.findScrollByIndex(s):this.findScrollByIndex(s)-this.findScrollByIndex(s+1),l=Math.max(Math.min(t.deltaY,n),-1*n);this.scroll=Math.min(Math.max(this.scroll-l*this.scrollSensitivity,this.scrollMax),this.scrollMin);const o=this.sanitizeInternalIndex(this.findIndexFromScroll(this.scroll)),r=null!=(i=null==(e=this.internalOptions[o])?void 0:e.value)?i:null;this.internalIndex=o,this.internalValue!==r&&this.$emit("update:modelValue",this.internalValue=r),this.onAfterWheel((()=>{this.correction(this.findIndexFromScroll(this.scroll))}))},onAfterWheel:function(t,e=83){let i=null;return function(){i&&(clearTimeout(i),i=null);const s=this,n=arguments;i=setTimeout((()=>t.apply(s,n)),e)}}((t=>{t()}),200),onStart(t){t.cancelable&&t.preventDefault();const{clientY:e}=l(t);this.start=[this.scroll,e],n(t)||(this.isMouseDown=!0),this.isDragging=!1},onMove(t){if(t.cancelable&&t.preventDefault(),!this.start)return;const{clientY:e}=l(t),i=e-this.start[1];Math.abs(i)>1.5&&(this.isDragging=!0),this.scroll=this.start[0]+i*(n(t)?this.touchSensitivity:this.dragSensitivity)},onEnd(t){t.cancelable&&t.preventDefault(),this.isDragging?this.correction(this.findIndexFromScroll(this.scroll)):this.onClick(t),this.start=null,this.isDragging=!1,this.isMouseDown=!1},onCancel(t){t.cancelable&&t.preventDefault(),this.correction(this.findIndexFromScroll(this.scroll)),this.start=null,this.isMouseDown=!1,this.isDragging=!1},onClick(t){const e=this.$refs.layerTop,i=this.$refs.layerBottom,s=l(t),n=s.clientX,o=s.clientY,r=e.getBoundingClientRect(),h=i.getBoundingClientRect();r.left<=n&&n<=r.right&&r.top<=o&&o<=r.bottom?this.correction(this.internalIndex-1):h.left<=n&&n<=h.right&&h.top<=o&&o<=h.bottom&&this.correction(this.internalIndex+1)},correction(t){var e,i;const s=this.sanitizeInternalIndex(t),n=null!=(i=null==(e=this.internalOptions[s])?void 0:e.value)?i:null;this.scroll=this.findScrollByIndex(s),this.transitioning=!0,this.transitionTimer&&(clearTimeout(this.transitionTimer),this.transitionTimer=null),this.transitionTimer=setTimeout((()=>{this.transitioning=!1,this.transitionTimer=null,this.internalIndex=s,this.internalValue!==n&&this.$emit("update:modelValue",this.internalValue=n)}),100)}},render(){let t=[];return this.hasPlaceholder?t.push(e("div",{class:["vue-scroll-picker-item","vue-scroll-picker-item-placeholder",{"vue-scroll-picker-item-selected":-1===this.internalIndex}],ref:t=>t&&this.setRefItem(t)},i(this.$slots,"placeholder",{text:this.placeholder},(()=>[this.placeholder])))):0===this.internalOptions.length&&t.push(e("div",{class:["vue-scroll-picker-item","vue-scroll-picker-item-empty","vue-scroll-picker-item-selected"],ref:t=>t&&this.setRefItem(t)},i(this.$slots,"empty",{text:this.empty},(()=>[this.empty])))),t=t.concat(this.internalOptions.map(((t,s)=>e("div",{class:["vue-scroll-picker-item",{"vue-scroll-picker-item-selected":this.internalIndex===s}],key:t.value,ref:t=>t&&this.setRefItem(t)},i(this.$slots,"default",{option:t},(()=>[t.name])))))),e("div",{class:["vue-scroll-picker"]},[e("div",{ref:"rotator",class:["vue-scroll-picker-rotator",{"vue-scroll-picker-rotator-transition":this.transitioning}],style:"number"==typeof this.scroll?{top:`${this.scroll}px`}:{}},t),e("div",{class:["vue-scroll-picker-layer"]},[e("div",{class:["vue-scroll-picker-layer-top"],ref:"layerTop"}),e("div",{class:["vue-scroll-picker-layer-selection"],ref:"layerSelection"}),e("div",{class:["vue-scroll-picker-layer-bottom"],ref:"layerBottom"})])])}});function r(t){t.component("VueScrollPicker",o)}"undefined"!=typeof window&&window.Vue&&r(window.Vue);const h={install:r};export default h;export{o as VueScrollPicker,r as install}; //# sourceMappingURL=vue-scroll-picker.es.js.map