"use strict";const t=require("vue"),N=require("./CdxMenu.cjs"),W=require("./CdxTextInput.cjs"),K=require("./useGeneratedId.cjs"),R=require("./useModelWrapper.cjs"),w=require("./useOptionalModelWrapper.js"),L=require("./useSplitAttributes.cjs"),D=require("./useFieldData.cjs"),E=require("./useFloatingMenu.cjs"),V=require("./constants.js"),O=require("./_plugin-vue_export-helper.js"),P=V.makeStringTypeValidator(V.ValidationStatusTypes),T=t.defineComponent({name:"CdxLookup",components:{CdxMenu:N,CdxTextInput:W},inheritAttrs:!1,props:{selected:{type:[String,Number,null],required:!0},menuItems:{type:Array,required:!0},inputValue:{type:[String,Number],default:null},initialInputValue:{type:[String,Number],default:"",validator:e=>(e&&console.warn("CdxLookup: prop initialInputValue is deprecated. Use inputValue instead."),!0)},disabled:{type:Boolean,default:!1},menuConfig:{type:Object,default:()=>({})},status:{type:String,default:"default",validator:P}},emits:["update:selected","update:input-value","load-more","input","change","focus","blur"],setup:(e,{emit:u,attrs:g,slots:i})=>{const y=t.ref(),p=t.ref(),o=t.ref(),m=K("lookup-menu"),l=t.ref(!1),s=t.ref(!1),c=t.ref(!1),b=t.ref(e.menuItems),{computedDisabled:f,computedStatus:C}=D(t.toRef(e,"disabled"),t.toRef(e,"status")),h=t.toRef(e,"selected"),v=R(h,u,"update:selected"),d=t.computed(()=>e.menuItems.find(n=>n.value===e.selected)),S=t.computed(()=>{var n,a;return(a=(n=o.value)==null?void 0:n.getHighlightedMenuItem())==null?void 0:a.id}),q=t.ref(e.initialInputValue),r=w.useOptionalModelWrapper(q,t.toRef(e,"inputValue"),u,"update:input-value"),k=t.computed(()=>({"cdx-lookup--disabled":f.value,"cdx-lookup--pending":l.value})),{rootClasses:M,rootStyle:$,otherAttrs:x}=L(g,k);function U(n){d.value?d.value.label!==n&&d.value.value!==n&&(v.value=null):e.selected!==null&&e.selected!==n&&(v.value=null),n===""&&b.value.length===0?(s.value=!1,l.value=!1):l.value=!0,u("input",n)}function B(n){c.value=!0;const a=r.value!==null&&r.value!=="";!!(e.menuItems.length>0||i["no-results"])&&(a||b.value.length>0)&&(s.value=!0),u("focus",n)}function A(n){c.value=!1,s.value=!1,u("blur",n)}function F(n){!o.value||f.value||e.menuItems.length===0&&!i["no-results"]||n.key===" "||o.value.delegateKeyNavigation(n)}return E(p,o),t.watch(h,n=>{var a;if(n!==null){const I=d.value?(a=d.value.label)!=null?a:d.value.value:"";r.value!==I&&(r.value=I,u("input",r.value))}}),t.watch(t.toRef(e,"menuItems"),n=>{c.value&&l.value&&(n.length>0||i["no-results"])&&(s.value=!0),n.length===0&&!i["no-results"]&&(s.value=!1),l.value=!1}),{rootElement:y,textInput:p,menu:o,menuId:m,highlightedId:S,computedInputValue:r,modelWrapper:v,expanded:s,computedDisabled:f,computedStatus:C,onInputBlur:A,rootClasses:M,rootStyle:$,otherAttrs:x,onUpdateInput:U,onInputFocus:B,onKeydown:F}}});function z(e,u,g,i,y,p){const o=t.resolveComponent("cdx-text-input"),m=t.resolveComponent("cdx-menu");return t.openBlock(),t.createElementBlock("div",{ref:"rootElement",class:t.normalizeClass(["cdx-lookup",e.rootClasses]),style:t.normalizeStyle(e.rootStyle)},[t.createVNode(o,t.mergeProps({ref:"textInput",modelValue:e.computedInputValue,"onUpdate:modelValue":u[0]||(u[0]=l=>e.computedInputValue=l)},e.otherAttrs,{class:"cdx-lookup__input",role:"combobox",autocomplete:"off","aria-autocomplete":"list","aria-controls":e.menuId,"aria-expanded":e.expanded,"aria-activedescendant":e.highlightedId,disabled:e.computedDisabled,status:e.computedStatus,"onUpdate:modelValue":e.onUpdateInput,onChange:u[1]||(u[1]=l=>e.$emit("change",l)),onFocus:e.onInputFocus,onBlur:e.onInputBlur,onKeydown:e.onKeydown}),null,16,["modelValue","aria-controls","aria-expanded","aria-activedescendant","disabled","status","onUpdate:modelValue","onFocus","onBlur","onKeydown"]),t.createVNode(m,t.mergeProps({id:e.menuId,ref:"menu",selected:e.modelWrapper,"onUpdate:selected":u[2]||(u[2]=l=>e.modelWrapper=l),expanded:e.expanded,"onUpdate:expanded":u[3]||(u[3]=l=>e.expanded=l),"menu-items":e.menuItems},e.menuConfig,{onLoadMore:u[4]||(u[4]=l=>e.$emit("load-more"))}),{default:t.withCtx(({menuItem:l})=>[t.renderSlot(e.$slots,"menu-item",{menuItem:l})]),"no-results":t.withCtx(()=>[t.renderSlot(e.$slots,"no-results")]),_:3},16,["id","selected","expanded","menu-items"])],6)}const H=O._export_sfc(T,[["render",z]]);module.exports=H;
