import{r as t,c as s,h as i,H as h}from"./p-5a5e7c99.js";const e=class{constructor(i){t(this,i),this.dense=!1,this.showSuggestions=!1,this.selectedIndex=-1,this.change=s(this,"change",7)}componentDidLoad(){this.watchValue(),this.textElement.addEventListener("keydown",t=>this.navigateSuggestions(t))}componentWillLoad(){this.watchValue()}watchValue(){this.value?this.value.label=this.value.label?this.value.label:this.value.value:this.value={value:null,label:null}}navigateSuggestions(t){if(this.suggestions&&this.suggestions.size>0){let s;const i=Array.from(this.suggestions.keys());switch(t.key){case"ArrowDown":s=null!==this.selectedIndex?this.selectedIndex+1:0,s>=this.suggestions.size&&(s=0),this.selectedIndex=s;break;case"ArrowUp":s=null!==this.selectedIndex?this.selectedIndex-1:this.suggestions.size-1,s<0&&(s=this.suggestions.size-1),this.selectedIndex=s;break;case"Enter":return null===this.selectedIndex&&(this.selectedIndex=0),this.selectSuggestion(i[this.selectedIndex]),this.change.emit(this.value),void this.clearSuggestions();default:return}t.preventDefault()}}clearSuggestions(){this.selectedIndex=-1,this.suggestions=null}selectSuggestion(t){if(this.showSuggestions=!1,this.suggestions){const s={value:t,label:this.suggestions.get(t)};this.value=Object.assign({},s)}}execAutocomplete(t){this.value.label=t.target.value,this.autocomplete(t.target.value).then(t=>this.suggestions=t),this.showSuggestions=this.suggestions&&this.suggestions.size>0}handleChange(){this.textElement.value?this.change.emit(this.value):(this.value=null,this.change.emit(this.value))}render(){return i(h,{style:{position:"relative"},onBlur:()=>{this.change.emit(this.value),this.showSuggestions=!1}},i("materials-text-field",{ref:t=>this.textElement=t,"trailing-icon":this.trailingIcon,dense:this.dense,label:this.label,focused:!!this.value.label,value:this.value.label,onInput:t=>this.execAutocomplete(t),onChange:t=>{t.stopPropagation(),t.preventDefault(),this.handleChange()}}),this.showSuggestions&&i("materials-list",{style:{position:"absolute",top:this.textElement.getBoundingClientRect().bottom-this.textElement.getBoundingClientRect().top+"px"}},Array.from(this.suggestions.keys()).map((t,s)=>s<this.maxSuggestions?i("materials-list-item",{selected:s===this.selectedIndex,onClick:()=>{this.selectSuggestion(t),this.change.emit(this.value),this.clearSuggestions()},label:this.suggestions.get(t)}):null)))}static get watchers(){return{value:["watchValue"]}}static get style(){return":host{display:block;}materials-list{z-index:1;background-color:#fff;width:100%;overflow-y:visible;-webkit-box-shadow:0 2px 4px 0px #AAA;box-shadow:0 2px 4px 0px #AAA;}"}};export{e as materials_autocomplete};