import{r as v,i as _,a as m,c as h,Z as y,R as k,u as b}from"./index-BzfmZ_Yq.js";function g(a,i,n){return new Promise((e,r)=>{const u=b(a);u.track("blueprints_run_started");const s=new Date().getTime();function t(l){var p,d;const o=(d=(p=l.payload)==null?void 0:p.mail)==null?void 0:d.some(w=>{var f;return((f=w.payload)==null?void 0:f.type)==="error"}),c={durationMs:new Date().getTime()-s};o?u.track("blueprints_run_failed",c):u.track("blueprints_run_succeeded",c),e(void 0)}a.forwardEvent(n?new CustomEvent("wf-run-blueprint-branch",{detail:{callback:t,handler:"run_blueprint_branch",payload:{branch_id:n}}}):new CustomEvent("wf-run-blueprint",{detail:{callback:t,handler:"run_blueprint_by_id",payload:{blueprint_id:i}}}),null,!0).catch(l=>{u.track("blueprints_run_failed",{error:String(l)}),r(l)})})}function R(a,i){return new Promise((n,e)=>{const r=b(a);r.track("blueprints_run_stopped"),a.forwardEvent(new CustomEvent("wf-stop-blueprint",{detail:{handler:"stop_blueprint_run",payload:{run_id:i}}}),null,!0).then(()=>n()).catch(u=>{r.track("blueprints_run_stop_failed",{error:String(u)}),e(u)})})}function T(a,i,n){const e=v(!1),r=_(m.socketTimeout);async function u(t){if(!e.value){e.value=!0,r&&(r.prevent.value=!0);try{await g(a,k(n),t)}finally{e.value=!1,r&&(r.prevent.value=!1)}}}async function s(){const t=i.activeBlueprintRunId.value;t&&await R(a,t)}return{isRunning:y(e),run:u,stop:s}}function E(a,i){const n=v([]),e=_(m.socketTimeout);async function r({blueprintId:t,branchId:l}){if(!n.value.includes(t))try{e&&(e.prevent.value=!0),n.value=[t,...n.value],await g(a,t,l)}finally{n.value=n.value.filter(o=>o!==t),e&&(e.prevent.value=!1)}}async function u(){await Promise.all(k(i).map(r))}const s=h(()=>n.value.length>0);return{run:u,isRunning:s,runningBlueprintIds:y(n)}}export{E as a,T as u};
