import{n as e}from"./rolldown-runtime-C0FnF6B9.js";import{t}from"./react-C21x__mS.js";import{o as n,s as r}from"./odata-BPGjAOCV.js";import{S as i,y as a}from"./formatters-CVj-3Vn0-BFaHJqZL.js";function o(e){return e?.length?[...new Set(e)].sort():[]}function s(e,t,n){let r=[];for(let i of Object.keys(t).sort()){if(i===e)continue;let t=o(n?.[i]);t.length&&r.push(`${i}:${t.join(p)}`)}return r.join(f)}function c(e){let t=new Map;if(!e)return t;for(let n of e.split(f)){let e=n.indexOf(`:`);if(e<0)continue;let r=n.slice(0,e),i=n.slice(e+1).split(p).filter(Boolean);r&&i.length&&t.set(r,i)}return t}function l({odata:e,segment:t,model:r,value:a,dependencies:o}){let[l,u]=(0,d.useState)(``),[f,p]=(0,d.useState)(``),{codeKey:m,searchDebounceDelay:h,minSearchCodeLength:g,minSearchTextLength:_}=r,{data:v,textKey:y,isLoading:b,isError:x,getItems:S,separatedItems:C,findSourceItemsByKeys:w,setFilteredItems:T,debounce:E}=n({odata:e,model:r}),D=C.length,O=t.selectText&&y?y:m,k=(0,d.useMemo)(()=>v?s(m,v.keyPairsMap,o):``,[v,m,o]);(0,d.useEffect)(()=>{if(!v)return;let e=v.chain.findIndex(e=>e.codeKey===m),t=c(k),n=e=>{let n=v.chain[e].codeKey,r=t.get(n);if(r?.length)return w(n,r)};for(let t=v.chain.length-1;t>e;t--){let e=n(t);if(e){T(e);return}}for(let t=0;t<e;t++){let e=n(t);if(e){T(e);return}}T(void 0)},[v,k,m,w,T]),(0,d.useEffect)(()=>{E(`search`,()=>{p(l.trim().toLowerCase())},h)},[E,l,h]);let A=(0,d.useMemo)(()=>{if(!a?.length||!O)return[];let e=new Set(a);return C.filter(t=>e.has(t[O]))},[C,O,a]);return{query:l,setQuery:u,debouncedQuery:f,selectedItems:A,filteredItems:(0,d.useMemo)(()=>{if(!v)return[];if(!(f.length>=_))return S(void 0,A);let e=f.length>=_,n=!t.hideCode&&f.length>=g&&!Number.isNaN(Number(f));return S((t,r)=>!!(t&&r&&(n&&i(t,f)||e&&r.toLowerCase().includes(f))),A)},[f,S,g,_,v,t.hideCode,A]),itemsCount:D,isLoading:b,isError:x,codeKey:m,textKey:y,selectionKey:O,placeholder:t.placeholder}}function u(e,t){return e.map(e=>e[t])}var d,f,p;function m(){return(m=e((()=>{d=t(),r(),a(),f=``,p=``})))()}export{u as n,l as r,m as t};