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