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