import{n as e}from"./rolldown-runtime-C0FnF6B9.js";import{t}from"./react-C21x__mS.js";import{a as n,i as r,o as i,r as a,s as o,t as s}from"./odata-BtfB5Aje.js";import{D as c,E as l}from"./formatters--mhLir82-v41eaI4a.js";function u(e,t,n){return e?`${e}/${t}:${n}`:`${t}:${n}`}function d(e,t,n){let r=n[t];return r&&e[r]||e[t]}function f(e){return[...new Set(e.filter(Boolean))].join(` `)}function p(e){return{id:u(e.parentId,e.codeKey,e.value),codeKey:e.codeKey,value:e.value,label:e.label,code:e.code,searchText:f([e.value,e.label,e.code]),sortCode:e.sortCode,children:[],childByBranchKey:new Map}}function m(e,t){e.sort((e,n)=>{let r=t?e.sortCode:e.label,i=t?n.sortCode:n.label;return c(r,i)||c(e.sortCode,n.sortCode)});for(let n of e)m(n.children,t)}function h(e){return{id:e.id,codeKey:e.codeKey,value:e.value,label:e.label,code:e.code,searchText:e.searchText,children:e.children.length>0?e.children.map(h):void 0}}function g({items:e,orderedCodeKeys:t,keyPairsMap:n,hiddenCodeKeys:r,textValueCodeKeys:i,sortByCode:a=!0}){if(t.length===0||e.length===0)return[];let o=[],s=new Map;for(let a of e){let e;for(let c of t){let t=String(a[c]??``);if(!t)break;let l=String(d(a,c,n)??``),u=i?.has(c)?l:t,f=`${c}:${u}`,m=e?e.childByBranchKey:s,h=m.get(f);h||(h=p({parentId:e?.id,codeKey:c,value:u,label:l,code:r.has(c)?void 0:t,sortCode:t}),m.set(f,h),e?e.children.push(h):o.push(h)),e=h}}return m(o,a),o.map(h)}function _(){return(_=e((()=>{l()})))()}function v(e,t){return n(e,t)}function y(){return(y=e((()=>{o()})))()}function b(e,t,n){return r([{odata:e,segments:t,model:n}])}function x(e){return e[0]?.id}function S({odata:e,segments:t,model:n}){let r=`${e.service}.${e.target}`,o=(0,C.useMemo)(()=>b(e,t,n),[e,t,n]),c=s([e]),l=(0,C.useMemo)(()=>v(o,c),[o,c]),u=(0,C.useMemo)(()=>l.map(e=>e.id),[l]),d=x(l)??Object.keys(t)[0],f=a({codeKey:d,...n}),p=i({odata:e,model:f}),{hiddenCodeKeys:m,textValueCodeKeys:h}=(0,C.useMemo)(()=>{let t=new Set,n=new Set;for(let r of l)(r.segment.hideCode??e.hideCode)&&t.add(r.id),r.segment.selectText&&n.add(r.id);return{hiddenCodeKeys:t,textValueCodeKeys:n}},[e.hideCode,l]),_=(0,C.useMemo)(()=>g({items:p.data?.items??[],orderedCodeKeys:u,keyPairsMap:p.data?.keyPairsMap??{},hiddenCodeKeys:m,textValueCodeKeys:h,sortByCode:e.sortByCode}),[p.data?.items,p.data?.keyPairsMap,m,e.sortByCode,u,h]),y=l[0]?.segment.placeholder??`Выберите значение`,S=!!c[r]?.length;return{nodes:_,orderedSegmentItems:l,orderedCodeKeys:u,isLoading:p.isLoading,isError:p.isError,placeholder:y,hasResolvedChain:S}}var C;function w(){return(w=e((()=>{C=t(),o(),_(),y()})))()}export{S as n,w as t};