import{g as v,a as A,b as E}from"./vesselEquipmentDetails-CJeqdW1Z.js";import{d as $,V as P,r as d,ay as q,d9 as z,aE as B,l as r,c as R,o as M,W as U,aa as N,w as I,j as x,aD as H,ds as Q,aA as G,cB as O,ab as W,p as j,ac as J,v as X}from"./index-9PRMimni.js";const Y=$({__name:"VesselTree",props:{selectedVesselId:{},cardAttrs:{},treeAttrs:{},treeHeight:{}},emits:["handleSelect"],setup(w,{expose:S,emit:V}){const _=V,c=w,{t:T}=P(),i=d(),u=d(),f=d(),m=d([]),y=d([]),p=d([]),g=d([]);q(()=>c.selectedVesselId,a=>{(a||a===0)&&(k(),y.value=[],p.value=[],_("handleSelect",null))},{immediate:!0});const D=H(async a=>{if(a){u.value=!0;try{const e=await A({shipId:c.selectedVesselId,productName:a});m.value=e.map(s=>({...s,key:s.productCode+(s.model||"")+(s.serialNumber||"")})),u.value=!1}catch{m.value=[],u.value=!1}}},800);async function L(a,e){if(!a)return;let s=await E({shipId:e.shipId,productCode:e.productCode,model:e.model,serialNumber:e.serialNumber});if(s?.length){s=o(s),p.value=s.map(l=>l.key);const n=s[s.length-1];await J(),b([n.key],[n])}}function b(a,e){y.value=a,_("handleSelect",e?.[0])}function C(a){p.value=a}async function k(){try{i.value=!0;const a=await v({shipId:c.selectedVesselId,code:0,model:0,serialNumber:0});g.value=o(a),i.value=!1}catch(a){i.value=!1,console.log(a)}}function F(a){return new Promise(async(e,s)=>{try{const n=await v({shipId:a.shipId,code:a.code||0,model:a.model||0,serialNumber:a.serialNumber||0});n?.length?(a.children=o(n),e()):s()}catch{s()}})}function K(a){const e=o([r(a)])?.[0];(async function s(n){for(const l of n){if(l.key===e?.parentKey){const h=await v({shipId:l.shipId,code:l.code||0,model:l.model||0,serialNumber:l.serialNumber||0});if(h?.length){l.children=o(h);for(const t of l.children||[])if(t.key===e?.key){b([t.key],[t]);break}}else l.children=[];break}l.children?.length&&await s(l.children)}})(g.value)}function o(a){return a.forEach(e=>{e.isLeaf=!!e.ifLeaf,e.key=e.code+(e.model||"")+(e.serialNumber||""),e.parentKey=e.parentCode+(e.parentModel||"")+(e.parentSerialNumber||""),e.shipId=e.shipId||c.selectedVesselId}),a}return S({handleRefreshNode:K,handleGetTreeData:k}),(a,e)=>{const s=G,n=O,l=U,h=z("loading");return B((M(),R(l,N({"loading-background":"transparent","header-style":{paddingTop:"10px",paddingBottom:"10px"},"content-style":{padding:"10px"},bordered:!1,segmented:"",class:"ui-card-small"},a.cardAttrs),{header:I(()=>[W(a.$slots,"header",{},()=>[e[1]||(e[1]=j("Equipment Structure"))],!0)]),default:I(()=>[x(s,{value:r(f),"onUpdate:value":[e[0]||(e[0]=t=>Q(f)?f.value=t:null),L],filterable:"",options:r(m),loading:r(u),placeholder:r(T)("equipmentLibrary.equipmentMainLibrary.treeSearchPlaceholder"),labelField:"productName",valueField:"key",clearable:"",remote:"",onSearch:r(D),class:"mb-[5px]"},null,8,["value","options","loading","placeholder","onSearch"]),x(n,N({"block-line":"",data:r(g),"virtual-scroll":!0,cancelable:!1,"selected-keys":r(y),"expanded-keys":r(p),"on-load":F,"render-label":({option:t})=>`${t.code} - ${t.name}${t.model&&!["0","defaultModel","NA"].includes(t.model)?" - "+t.model:""}`,style:`max-height: ${a.treeHeight||650}px`,"key-field":"key","label-field":"name"},a.treeAttrs,{"onUpdate:expandedKeys":C,"onUpdate:selectedKeys":b}),null,16,["data","selected-keys","expanded-keys","render-label","style"])]),_:3},16)),[[h,r(i)]])}}}),ae=X(Y,[["__scopeId","data-v-399751fe"]]);export{ae as default};
