/**
*
* @authors yutent (yutent.io@gmail.com)
* @date 2022-09-12 18:13:11
* @version v1.0.6
*
*/
var g=Object.defineProperty;var x=(n,l,e)=>l in n?g(n,l,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[l]=e;var d=(n,l,e)=>(x(n,typeof l!="symbol"?l+"":l,e),e);import"../scroll/index.js";import"../icon/index.js";import r from"../utils.js";class p extends HTMLElement{constructor(){super();d(this,"props",{value:"",placeholder:"",maxlength:0,minlength:0,clearable:!1,autofocus:!1,readonly:!1,disabled:!1,lazy:0});d(this,"state",{list:[],mvidx:null});Object.defineProperty(this,"root",{value:this.attachShadow({mode:"open"}),writable:!0,enumerable:!1,configurable:!0}),this.root.innerHTML='
',this.__OUTER__=this.root.children[1],this.__PREPEND__=this.__OUTER__.children[0],this.__INPUT__=this.__OUTER__.children[1],this.__CLOSE__=this.__OUTER__.children[2],this.__APPEND__=this.__OUTER__.children[3],this.__LIST__=this.__OUTER__.children[4]}static get observedAttributes(){return["value","placeholder","maxlength","minlength","clearable","autofocus","readonly","disabled","lazy"]}get readOnly(){return this.props.readonly}set readOnly(e){var s=typeof e;e!==this.props.readonly&&(s==="boolean"&&e||s!=="boolean"?(this.props.readonly=!0,this.setAttribute("readonly",""),this.__INPUT__.setAttribute("readonly","")):(this.props.readonly=!1,this.removeAttribute("readonly"),this.__INPUT__.removeAttribute("readonly")))}get disabled(){return this.props.disabled}set disabled(e){var s=typeof e;e!==this.props.disabled&&(s==="boolean"&&e||s!=="boolean"?(this.props.disabled=!0,this.setAttribute("disabled",""),this.__INPUT__.setAttribute("disabled","")):(this.props.disabled=!1,this.removeAttribute("disabled"),this.__INPUT__.removeAttribute("disabled")))}get value(){return this.__INPUT__.value}set value(e){this.__INPUT__.value=e}_moveSelect(e){var{list:s}=this.state;if(s&&s.length){e.preventDefault();var t=e.keyCode===38?-1:1,i=Array.from(this.__LIST__.firstElementChild.firstElementChild.children);this.state.mvidx===null?this.state.mvidx=0:this.state.mvidx+=t,this.state.mvidx<0?this.state.mvidx=0:this.state.mvidx>i.length-1&&(this.state.mvidx=i.length-1),i.forEach((o,a)=>{a===this.state.mvidx?(this.__LIST__.firstElementChild.scrollTop=o.offsetTop-150,o.setAttribute("focus","")):o.removeAttribute("focus")})}}_fetchSelect(e,s){var t=this.state.list[e];this.value=t.value,this.dispatchEvent(new CustomEvent("select",{detail:t})),this._handleChange(s),this.__LIST__.classList.remove("show"),this.state.mvidx=null}connectedCallback(){var e=this.__PREPEND__.assignedNodes(),s=this.__APPEND__.assignedNodes();for(this.stamp=0;e.length>1;)this.removeChild(e.pop());for(;s.length>1;)this.removeChild(s.pop());e.length&&this.__OUTER__.setAttribute("prepend",""),s.length&&this.__OUTER__.setAttribute("append",""),this._handleSubmit=r.catch(this.__INPUT__,"keydown",t=>{let{minlength:i,lazy:o}=this.props,a=this.value;if(!(this.disabled||this.readOnly)){if(t.keyCode===38||t.keyCode===40)return this._moveSelect(t);if(t.keyCode===13){t.preventDefault();let h=Date.now();if(this.state.mvidx!==null)return this._fetchSelect(this.state.mvidx,t);if(o&&h-this.stamp0&&a.length{let i=Date.now(),{lazy:o}=this.props;o&&i-this.stamp{this.state.list=a,this._parseSuggestion()}}})))}),this._parseSuggestion=r.bind(this.__INPUT__,"click",t=>{var{list:i}=this.state;let{x:o,y:a,width:h,height:c}=this.getBoundingClientRect();if(i&&i.length){var u=i.map((_,b)=>`${_.value}`).join("");this.__LIST__.firstElementChild.firstElementChild.innerHTML=u,this.__LIST__.classList.toggle("show",!0),this.__LIST__.style.cssText=`left:${o}px;top:${a+c+5}px;width:${h}px;`}else this.__LIST__.classList.toggle("show",!1)}),this._inactiveFn=r.outside(this,t=>{this.__LIST__.classList.remove("show")}),this._handleSelect=r.bind(this.__LIST__,"click",t=>{t.target.tagName==="LI"&&(this._fetchSelect(t.target.dataset.idx,t),this.dispatchEvent(new CustomEvent("input")))}),this._clearFn=r.bind(this.__CLOSE__,"click",t=>{this.value="",this.dispatchEvent(new CustomEvent("input"))})}disconnectedCallback(){r.unbind(this.__INPUT__,"keydown",this._handleSubmit),r.unbind(this.__INPUT__,"input",this._handleChange),r.unbind(this.__INPUT__,"click",this._parseSuggestion),r.unbind(this.__LIST__,"click",this._handleSelect),r.unbind(this.__CLOSE__,"click",this._clearFn),r.clearOutside(this._inactiveFn)}attributeChangedCallback(e,s,t){if(s!==t)switch(e){case"autofocus":this.__INPUT__.setAttribute("autofocus",""),setTimeout(o=>{this.__INPUT__.focus()},10);break;case"placeholder":this.__INPUT__.setAttribute("placeholder",t);break;case"value":this.value=t||"";break;case"maxlength":if(t===null)this.__INPUT__.removeAttribute(e);else{let o=+t;o>0?this.__INPUT__.setAttribute(e,o):this.removeAttribute(e)}break;case"lazy":this.props.lazy=t>>0;break;case"readonly":case"disabled":var i=e;i==="readonly"&&(i="readOnly"),this[i]=t!==null;break}}}customElements.get("wc-input")||customElements.define("wc-input",p);export{p as default};