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 B,h as Ve,b as C,t as j,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 g,y as Qe,B as ue,q as m}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/DVl4ZBgx.js";import{p as et}from"../chunks/CJwphPxi.js";import{g as fe}from"../chunks/CPCsbdkz.js";import{U as tt}from"../chunks/XzGG0o_q.js";import{D as ge}from"../chunks/kJjQhR0J.js";import{E as at,P as nt,a as ot}from"../chunks/CaVdfWt-.js";import{D as rt,I as it}from"../chunks/0ZGtv6cq.js";import{g as me}from"../chunks/cjRLNre3.js";import{e as S}from"../chunks/BZiHL9L3.js";import{W as st}from"../chunks/Clig3Vwb.js";import{C as lt}from"../chunks/BRFhz4VJ.js";import{w as G}from"../chunks/BuuPrWMc.js";import{t as I}from"../chunks/BZUCTtPY.js";var dt=B('
'),ct=B(''),ut=B(" ",1),ft=B(' ',1);function Tt(pe,ve){qe(ve,!1);const[ye,he]=Xe(),R=()=>Ye(et,"$page",ye),w=l();let a=l(null),c=l([]),p=l([]),F=l(!0),O=l(""),D=l(!1),P=l(!1),E=l(!1),T=l(!1),u=l(null),M=null,b=l();async function V(){if(e(w))try{o(F,!0),o(O,"");const[t,n,r]=await Promise.all([_.getOrganization(e(w)),_.listOrganizationPools(e(w)).catch(()=>[]),_.listOrganizationInstances(e(w)).catch(()=>[])]);o(a,t),o(c,n),o(p,r)}catch(t){o(O,S(t))}finally{o(F,!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(),I.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=S(t);I.error("Delete Failed",n)}o(P,!1)}}async function $e(){if(e(u))try{await _.deleteInstance(e(u).name),I.success("Instance Deleted",`Instance ${e(u).name} has been deleted successfully.`),o(E,!1),o(u,null)}catch(t){const n=S(t);I.error("Delete Failed",n),o(E,!1),o(u,null)}}function xe(t){o(u,t),o(E,!0)}function Ie(){o(T,!0)}async function we(t){try{if(!e(a))return;await _.createOrganizationPool(e(a).id,t.detail),I.success("Pool Created",`Pool has been created successfully for organization ${e(a).name}.`),o(T,!1)}catch(n){const r=S(n);I.error("Pool Creation Failed",r)}}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(W=>W.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(w,R().params.id)}),Re(),We();var K=ft();Ve(t=>{j(()=>Ke.title=`${e(a),i(()=>e(a)?`${e(a).name} - Organization Details`:"Organization Details")??""} - GARM`)});var U=C(K),L=f(U),Q=f(L),N=f(Q),Pe=f(N);g(N);var X=d(N,2),Y=f(X),Z=d(f(Y),2),Te=f(Z,!0);g(Z),g(Y),g(X),g(Q),g(L);var Me=d(L,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($),q=f(k,!0);g(k),g($),j(()=>ce(q,e(O))),x(z,$)},W=z=>{var $=ue(),k=C($);{var q=H=>{var ne=ut(),oe=C(ne);{let v=m(()=>(e(a),i(()=>e(a).name||"Organization"))),y=m(()=>(e(a),i(()=>e(a).endpoint?.name))),Ne=m(()=>(de(me),e(a),i(()=>me(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(P,!0)})}var re=d(oe,2);at(re,{get entity(){return e(a)},entityType:"organization"});var ie=d(re,2);{let v=m(()=>(e(a),i(()=>e(a).id||""))),y=m(()=>(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=m(()=>(e(a),i(()=>e(a).id||""))),y=m(()=>(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=m(()=>(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(H,ne)};h(k,H=>{e(a)&&H(q)},!0)}x(z,$)};h(r,z=>{e(O)?z(s):z(W,!1)},!0)}x(t,n)};h(Me,t=>{e(F)?t(ke):t(Ce,!1)})}g(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=>{ge(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(P,!1),confirm:ze}})};h(te,t=>{e(P)&&e(a)&&t(Se)})}var ae=d(te,2);{var Be=t=>{ge(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(E,!1),o(u,null)},confirm:$e}})};h(ae,t=>{e(E)&&e(u)&&t(Be)})}var Fe=d(ae,2);{var Ue=t=>{{let n=m(()=>(e(a),i(()=>e(a).id||"")));lt(t,{initialEntityType:"organization",get initialEntityId(){return e(n)},$$events:{close:()=>o(T,!1),submit:we}})}};h(Fe,t=>{e(T)&&e(a)&&t(Ue)})}j(t=>{Ze(Pe,"href",t),ce(Te,(e(a),i(()=>e(a)?e(a).name:"Loading...")))},[()=>(de(A),i(()=>A("/organizations")))]),x(pe,K),Je(),he()}export{Tt as component};