import"../chunks/DsnmJJEf.js";import{i as We}from"../chunks/zNh6Oe5P.js";import{p as qe,o as He,A as je,l as Ge,a as Re,f as S,h as Ve,b as C,t as H,c as x,d as Je,u as i,n as de,v as ce,g as e,m as l,j as f,k as d,s as o,$ as Ke,r as m,y as Qe,B as ue,q as g}from"../chunks/sWNKMed7.js";import{i as h,s as Xe,a as Ye}from"../chunks/Ccl3fNd2.js";import{d as A,c as Ze,g as _}from"../chunks/D30EsFKH.js";import{p as et}from"../chunks/DubtptHG.js";import{g as fe}from"../chunks/CeO1pnaq.js";import{U as tt}from"../chunks/DAUYzF7s.js";import{D as me}from"../chunks/B6BdCO3z.js";import{E as at,P as nt,a as ot}from"../chunks/B-C7pZX9.js";import{D as rt,I as it}from"../chunks/MZdleLKl.js";import{g as ge}from"../chunks/DyvUHRqW.js";import{e as j}from"../chunks/BZiHL9L3.js";import{W as st}from"../chunks/CHldIK8x.js";import{C as lt}from"../chunks/CLrK9xMi.js";import{w as G}from"../chunks/BuuPrWMc.js";import{t as E}from"../chunks/BZUCTtPY.js";var dt=S('

Loading organization...

'),ct=S('

'),ut=S(" ",1),ft=S('
',1);function Pt(pe,ve){qe(ve,!1);const[ye,he]=Xe(),R=()=>Ye(et,"$page",ye),I=l();let a=l(null),c=l([]),p=l([]),B=l(!0),O=l(""),D=l(!1),T=l(!1),w=l(!1),P=l(!1),u=l(null),M=null,b=l();async function V(){if(e(I))try{o(B,!0),o(O,"");const[t,n,r]=await Promise.all([_.getOrganization(e(I)),_.listOrganizationPools(e(I)).catch(()=>[]),_.listOrganizationInstances(e(I)).catch(()=>[])]);o(a,t),o(c,n),o(p,r)}catch(t){o(O,j(t))}finally{o(B,!1)}}function _e(t,n){const{events:r}=t;return{...n,events:r}}async function be(t){if(e(a))try{await _.updateOrganization(e(a).id,t),await V(),E.success("Organization Updated",`Organization ${e(a).name} has been updated successfully.`),o(D,!1)}catch(n){throw n}}async function ze(){if(e(a)){try{await _.deleteOrganization(e(a).id),fe(A("/organizations"))}catch(t){const n=j(t);E.error("Delete Failed",n)}o(T,!1)}}async function $e(){if(e(u))try{await _.deleteInstance(e(u).name),E.success("Instance Deleted",`Instance ${e(u).name} has been deleted successfully.`),o(w,!1),o(u,null)}catch(t){const n=j(t);E.error("Delete Failed",n),o(w,!1),o(u,null)}}function xe(t){o(u,t),o(w,!0)}function Ie(){o(P,!0)}async function we(t){try{if(!e(a))return;await _.createOrganizationPool(e(a).id,t.detail),E.success("Pool Created",`Pool has been created successfully for organization ${e(a).name}.`),o(P,!1)}catch(n){throw n}}function J(){e(b)&&Qe(b,e(b).scrollTop=e(b).scrollHeight)}function Ee(t){if(t.operation==="update"){const n=t.payload;if(e(a)&&n.id===e(a).id){const r=e(a).events?.length||0,s=n.events?.length||0;o(a,_e(e(a),n)),s>r&&setTimeout(()=>{J()},100)}}else if(t.operation==="delete"){const n=t.payload.id||t.payload;e(a)&&e(a).id===n&&fe(A("/organizations"))}}function Oe(t){if(!e(a))return;const n=t.payload;if(n.org_id===e(a).id){if(t.operation==="create")o(c,[...e(c),n]);else if(t.operation==="update")o(c,e(c).map(r=>r.id===n.id?n:r));else if(t.operation==="delete"){const r=n.id||n;o(c,e(c).filter(s=>s.id!==r))}}}function De(t){if(!e(a)||!e(c))return;const n=t.payload;if(e(c).some(s=>s.id===n.pool_id)){if(t.operation==="create")o(p,[...e(p),n]);else if(t.operation==="update")o(p,e(p).map(s=>s.id===n.id?n:s));else if(t.operation==="delete"){const s=n.id||n;o(p,e(p).filter(N=>N.id!==s))}}}He(()=>{V().then(()=>{e(a)?.events?.length&&setTimeout(()=>{J()},100)});const t=G.subscribeToEntity("organization",["update","delete"],Ee),n=G.subscribeToEntity("pool",["create","update","delete"],Oe),r=G.subscribeToEntity("instance",["create","update","delete"],De);M=()=>{t(),n(),r()}}),je(()=>{M&&(M(),M=null)}),Ge(()=>R(),()=>{o(I,R().params.id)}),Re(),We();var K=ft();Ve(t=>{H(()=>Ke.title=`${e(a),i(()=>e(a)?`${e(a).name} - Organization Details`:"Organization Details")??""} - GARM`)});var U=C(K),F=f(U),Q=f(F),L=f(Q),Te=f(L);m(L);var X=d(L,2),Y=f(X),Z=d(f(Y),2),Pe=f(Z,!0);m(Z),m(Y),m(X),m(Q),m(F);var Me=d(F,2);{var ke=t=>{var n=dt();x(t,n)},Ce=t=>{var n=ue(),r=C(n);{var s=z=>{var $=ct(),k=f($),W=f(k,!0);m(k),m($),H(()=>ce(W,e(O))),x(z,$)},N=z=>{var $=ue(),k=C($);{var W=q=>{var ne=ut(),oe=C(ne);{let v=g(()=>(e(a),i(()=>e(a).name||"Organization"))),y=g(()=>(e(a),i(()=>e(a).endpoint?.name))),Ne=g(()=>(de(ge),e(a),i(()=>ge(e(a).endpoint?.endpoint_type||"unknown"))));rt(oe,{get title(){return e(v)},get subtitle(){return`Endpoint: ${e(y)??""}`},get forgeIcon(){return e(Ne)},onEdit:()=>o(D,!0),onDelete:()=>o(T,!0)})}var re=d(oe,2);at(re,{get entity(){return e(a)},entityType:"organization"});var ie=d(re,2);{let v=g(()=>(e(a),i(()=>e(a).id||""))),y=g(()=>(e(a),i(()=>e(a).name||"")));st(ie,{entityType:"organization",get entityId(){return e(v)},get entityName(){return e(y)}})}var se=d(ie,2);{let v=g(()=>(e(a),i(()=>e(a).id||""))),y=g(()=>(e(a),i(()=>e(a).name||"")));nt(se,{get pools(){return e(c)},entityType:"organization",get entityId(){return e(v)},get entityName(){return e(y)},$$events:{addPool:Ie}})}var le=d(se,2);it(le,{get instances(){return e(p)},entityType:"organization",onDeleteInstance:xe});var Le=d(le,2);{let v=g(()=>(e(a),i(()=>e(a)?.events)));ot(Le,{get events(){return e(v)},get eventsContainer(){return e(b)},set eventsContainer(y){o(b,y)},$$legacy:!0})}x(q,ne)};h(k,q=>{e(a)&&q(W)},!0)}x(z,$)};h(r,z=>{e(O)?z(s):z(N,!1)},!0)}x(t,n)};h(Me,t=>{e(B)?t(ke):t(Ce,!1)})}m(U);var ee=d(U,2);{var Ae=t=>{tt(t,{get entity(){return e(a)},entityType:"organization",$$events:{close:()=>o(D,!1),submit:n=>be(n.detail)}})};h(ee,t=>{e(D)&&e(a)&&t(Ae)})}var te=d(ee,2);{var Se=t=>{me(t,{title:"Delete Organization",message:"Are you sure you want to delete this organization? This action cannot be undone and will remove all associated pools and instances.",get itemName(){return e(a),i(()=>e(a).name)},$$events:{close:()=>o(T,!1),confirm:ze}})};h(te,t=>{e(T)&&e(a)&&t(Se)})}var ae=d(te,2);{var Be=t=>{me(t,{title:"Delete Instance",message:"Are you sure you want to delete this instance? This action cannot be undone.",get itemName(){return e(u),i(()=>e(u).name)},$$events:{close:()=>{o(w,!1),o(u,null)},confirm:$e}})};h(ae,t=>{e(w)&&e(u)&&t(Be)})}var Ue=d(ae,2);{var Fe=t=>{{let n=g(()=>(e(a),i(()=>e(a).id||"")));lt(t,{initialEntityType:"organization",get initialEntityId(){return e(n)},$$events:{close:()=>o(P,!1),submit:we}})}};h(Ue,t=>{e(P)&&e(a)&&t(Fe)})}H(t=>{Ze(Te,"href",t),ce(Pe,(e(a),i(()=>e(a)?e(a).name:"Loading...")))},[()=>(de(A),i(()=>A("/organizations")))]),x(pe,K),Je(),he()}export{Pt as component};