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