"use strict";var q=(e,o,s)=>new Promise((v,p)=>{var l=u=>{try{i(s.next(u))}catch(a){p(a)}},c=u=>{try{i(s.throw(u))}catch(a){p(a)}},i=u=>u.done?v(u.value):Promise.resolve(u.value).then(l,c);i((s=s.apply(e,o)).next())});const t=require("vue"),K=require("./CdxChipInput.cjs"),N=require("./CdxMenu.cjs"),D=require("./useFieldData.cjs"),L=require("./useFloatingMenu.cjs"),O=require("./useGeneratedId.cjs"),S=require("./useModelWrapper.cjs"),T=require("./useOptionalModelWrapper.js"),z=require("./useSplitAttributes.cjs"),b=require("./constants.js"),E=require("./_plugin-vue_export-helper.js"),H=b.makeStringTypeValidator(b.ValidationStatusTypes),P=t.defineComponent({name:"CdxMultiselectLookup",components:{CdxChipInput:K,CdxMenu:N},props:{inputChips:{type:Array,required:!0},selected:{type:[Array],required:!0},menuItems:{type:Array,required:!0},inputValue:{type:[String,Number],default:null},separateInput:{type:Boolean,default:!1},disabled:{type:Boolean,default:!1},status:{type:String,default:"default",validator:H},menuConfig:{type:Object,default:()=>({})}},emits:["update:input-chips","update:selected","update:input-value","load-more","input","change","focus","blur"],setup:(e,{emit:o,attrs:s,slots:v})=>{const p=t.ref(),l=t.ref(),c=O("multiselect-lookup-menu"),i=t.computed(()=>{var n,r;return(r=(n=l.value)==null?void 0:n.getHighlightedMenuItem())==null?void 0:r.id}),u=t.ref(!1),a=t.ref(!1),h=t.ref(!1);t.provide(b.AllowArbitraryKey,t.ref(!1));const{computedDisabled:g,computedStatus:V}=D(t.toRef(e,"disabled"),t.toRef(e,"status")),k=t.computed(()=>({"cdx-multiselect-lookup--disabled":g.value,"cdx-multiselect-lookup--pending":u.value})),{rootClasses:w,rootStyle:M,otherAttrs:A}=z(s,k);L(p,l);const I=S(t.toRef(e,"selected"),o,"update:selected"),f=S(t.toRef(e,"inputChips"),o,"update:input-chips"),R=t.ref(""),y=T.useOptionalModelWrapper(R,t.toRef(e,"inputValue"),o,"update:input-value"),C=t.computed(()=>y.value.toString().length>0&&v["no-results"]);function W(n){return q(this,null,function*(){yield t.nextTick(),u.value=n!==null&&n!=="",o("input",n)})}function B(n){h.value=!0,(e.menuItems.length>0||C.value)&&(a.value=!0),o("focus",n)}function U(n){h.value=!1,a.value=!1,o("blur",n)}function $(n){!l.value||g.value||e.menuItems.length===0&&!C.value||n.key===" "||l.value.delegateKeyNavigation(n)}return t.watch(t.toRef(e,"selected"),n=>{const r=n.filter(m=>f.value.find(d=>m===d.value)===void 0);r.length>0&&(r.forEach(m=>{const d=e.menuItems.find(F=>F.value===m);d&&f.value.push(d)}),y.value="",o("input","")),f.value=f.value.filter(m=>n.find(d=>m.value===d)!==void 0)}),t.watch(t.toRef(e,"inputChips"),n=>{n.length<I.value.length&&(I.value=n.map(r=>r.value))}),t.watch(t.toRef(e,"menuItems"),n=>{n.length===0&&!C.value?a.value=!1:h.value&&u.value&&(a.value=!0),u.value=!1}),{chipInput:p,menu:l,menuId:c,highlightedId:i,expanded:a,computedDisabled:g,computedStatus:V,rootClasses:w,rootStyle:M,otherAttrs:A,selectedWrapper:I,inputChipsWrapper:f,computedInputValue:y,onUpdateInputValue:W,onInputBlur:U,onInputFocus:B,onKeydown:$}}});function j(e,o,s,v,p,l){const c=t.resolveComponent("cdx-chip-input"),i=t.resolveComponent("cdx-menu");return t.openBlock(),t.createElementBlock("div",{class:t.normalizeClass(["cdx-multiselect-lookup",e.rootClasses]),style:t.normalizeStyle(e.rootStyle)},[t.createVNode(c,t.mergeProps({ref:"chipInput","input-chips":e.inputChipsWrapper,"onUpdate:inputChips":o[0]||(o[0]=u=>e.inputChipsWrapper=u),"input-value":e.computedInputValue,"onUpdate:inputValue":o[1]||(o[1]=u=>e.computedInputValue=u)},e.otherAttrs,{class:"cdx-multiselect-lookup__chip-input",role:"combobox",autocomplete:"off","aria-autocomplete":"list","aria-controls":e.menuId,"aria-expanded":e.expanded,"aria-activedescendant":e.highlightedId,"separate-input":e.separateInput,disabled:e.computedDisabled,status:e.computedStatus,"disallow-arbitrary":!0,"onUpdate:inputValue":e.onUpdateInputValue,onFocus:e.onInputFocus,onBlur:e.onInputBlur,onKeydown:e.onKeydown}),null,16,["input-chips","input-value","aria-controls","aria-expanded","aria-activedescendant","separate-input","disabled","status","onUpdate:inputValue","onFocus","onBlur","onKeydown"]),t.createVNode(i,t.mergeProps({id:e.menuId,ref:"menu",selected:e.selectedWrapper,"onUpdate:selected":o[2]||(o[2]=u=>e.selectedWrapper=u),expanded:e.expanded,"onUpdate:expanded":o[3]||(o[3]=u=>e.expanded=u),"menu-items":e.menuItems},e.menuConfig,{onLoadMore:o[4]||(o[4]=u=>e.$emit("load-more"))}),{default:t.withCtx(({menuItem:u})=>[t.renderSlot(e.$slots,"menu-item",{menuItem:u})]),"no-results":t.withCtx(()=>[t.renderSlot(e.$slots,"no-results")]),_:3},16,["id","selected","expanded","menu-items"])],6)}const x=E._export_sfc(P,[["render",j]]);module.exports=x;
