import{L as u,c as d,b as w,j as o,r as f}from"./index-D92x6h1h.js";async function E(e,n){const a=n.classField??"class",i=n.nameField??"name",c=n.nameSuffix??" (copy)",r=(await u("idnode/load",{uuid:e})).entries?.[0];if(!r?.params)throw new Error(`Failed to load source entity ${e}`);const s={};for(const v of r.params)s[v.id]=v.value;const l=typeof r.class=="string"&&r.class?r.class:s[a];if(typeof l!="string"||!l)throw new Error(`Source entity ${e} missing required class field "${a}"`);delete s[a],delete s.uuid;const m=s[i],y=((typeof m=="string"?m:"")||"unnamed")+c;s[i]=y;const g=await u(n.createEndpoint,{class:l,conf:JSON.stringify(s)});if(!g.uuid)throw new Error(`Create endpoint ${n.createEndpoint} returned no uuid`);return{uuid:g.uuid,name:y}}function C(e){const n=f(!1),a=d();async function i(){if(!e.selected.value||n.value)return;const c=e.selected.value;n.value=!0;try{const t=await E(c,{createEndpoint:e.createEndpoint});e.selected.value=t.uuid,a.success(o('Cloned as "{0}".',t.name))}catch(t){a.error(t instanceof Error?t.message:String(t),{summary:e.errorSummary??o("Clone failed")})}finally{n.value=!1}}return{inflight:n,run:i}}function k(e){const n=f(!1),a=d(),i=w();async function c(){if(!e.selected.value||n.value||!await i.ask(e.confirmText,{severity:"danger"}))return;const r=e.selected.value;n.value=!0;try{await u("idnode/delete",{uuid:r}),e.selected.value=null}catch(s){a.error(s instanceof Error?s.message:String(s),{summary:e.errorSummary??o("Delete failed")})}finally{n.value=!1}}return{inflight:n,run:c}}function A(e){const n=f(!1),a=d();function i(){n.value=!0}async function c(t){n.value=!1;try{const r=await u(e.createEndpoint,{class:t,conf:"{}"});typeof r.uuid=="string"&&r.uuid?(e.selected.value=r.uuid,a.success(e.successMessage)):a.error(e.noUuidErrorMessage??o("Server returned no uuid."),{summary:e.errorSummary??o("Add failed")})}catch(r){a.error(r instanceof Error?r.message:String(r),{summary:e.errorSummary??o("Add failed")})}}return{pickerVisible:n,onAddClick:i,onClassPicked:c}}export{C as a,k as b,A as u};
