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