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