var{defineProperty:J,getOwnPropertyNames:R,getOwnPropertyDescriptor:T}=Object,U=Object.prototype.hasOwnProperty;function V(j){return this[j]}var _=(j)=>{var q=(P??=new WeakMap).get(j),z;if(q)return q;if(q=J({},"__esModule",{value:!0}),j&&typeof j==="object"||typeof j==="function"){for(var B of R(j))if(!U.call(q,B))J(q,B,{get:V.bind(j,B),enumerable:!(z=T(j,B))||z.enumerable})}return P.set(j,q),q},P;var X=(j)=>j;function Y(j,q){this[j]=X.bind(null,q)}var $=(j,q)=>{for(var z in q)J(j,z,{get:q[z],enumerable:!0,configurable:!0,set:Y.bind(q,z)})};var A=(j,q)=>()=>(j&&(q=j(j=0)),q);class Z extends Error{errorType;workflowId;details;constructor(j,q,z,B){super(z);this.name="WorkflowContextError",this.errorType=j,this.workflowId=q,this.details=B}}class Q{state;spec;constructor(j,q){this.state=j;this.spec=q}get workflowId(){return this.state.workflowId}get workflowKey(){return this.state.workflowName}get workflowVersion(){return this.state.workflowVersion}get currentStep(){return this.state.currentStep}get status(){return this.state.status}get data(){return this.state.data}get history(){return this.state.history}getState(){return this.state}getData(j){return this.state.data[j]}isTerminal(){return this.state.status==="completed"||this.state.status==="failed"||this.state.status==="cancelled"}isRunning(){return this.state.status==="running"}getCurrentStepDef(){return this.spec.definition.steps.find((j)=>j.id===this.state.currentStep)}getRetryCount(j){let q=j??this.state.currentStep;return this.state.retryCounts?.[q]??0}canTransition(j){return this.spec.definition.transitions.filter((z)=>z.from===this.state.currentStep&&z.to===j).length>0}getAvailableTransitions(){return this.spec.definition.transitions.filter((j)=>j.from===this.state.currentStep).map((j)=>({from:j.from,to:j.to,label:j.label,condition:j.condition}))}hasNextStep(){return this.spec.definition.transitions.some((j)=>j.from===this.state.currentStep)}getRemainingTime(j){let q=this.spec.definition.sla;if(!q)return null;let z=Date.now();if(j==="totalDuration"){if(!q.totalDurationMs)return null;let F=z-this.state.createdAt.getTime();return Math.max(0,q.totalDurationMs-F)}let B=q.stepDurationMs?.[j];if(!B)return null;let D=this.state.history.find((F)=>F.stepId===j&&F.status==="running");if(!D)return B;let H=z-D.startedAt.getTime();return Math.max(0,B-H)}isSlaViolated(j){let q=this.getRemainingTime(j);return q!==null&&q<=0}getAllSlaStatuses(){let j=this.spec.definition.sla;if(!j)return[];let q=[],z=Date.now();if(j.totalDurationMs){let B=z-this.state.createdAt.getTime(),D=Math.max(0,j.totalDurationMs-B);q.push({key:"totalDuration",type:"workflow",limitMs:j.totalDurationMs,elapsedMs:B,remainingMs:D,violated:D<=0,percentUsed:B/j.totalDurationMs*100})}if(j.stepDurationMs)for(let[B,D]of Object.entries(j.stepDurationMs)){let H=this.state.history.find((O)=>O.stepId===B&&O.status==="running"),F=H?z-H.startedAt.getTime():0,N=Math.max(0,D-F);q.push({key:B,type:"step",stepId:B,limitMs:D,elapsedMs:F,remainingMs:N,violated:H!==void 0&&N<=0,percentUsed:F/D*100})}return q}hasCompensation(){return this.spec.definition.compensation!==void 0}getCompensableSteps(){let j=this.spec.definition.compensation;if(!j)return[];return j.steps.map((q)=>q.stepId)}getEvents(){let j=[];for(let q of this.state.history)if(j.push({timestamp:q.startedAt,type:"step_started",stepId:q.stepId,input:q.input}),q.completedAt){if(q.status==="completed")j.push({timestamp:q.completedAt,type:"step_completed",stepId:q.stepId,output:q.output});else if(q.status==="failed")j.push({timestamp:q.completedAt,type:"step_failed",stepId:q.stepId,error:q.error})}return j.sort((q,z)=>q.timestamp.getTime()-z.timestamp.getTime()),j}}function b(j,q){return new Q(j,q)}function C(j){let q=j.getState(),z=new Set(q.history.filter((D)=>D.status==="completed").map((D)=>D.stepId)),B=z.size+(j.isTerminal()?0:1);if(j.status==="completed")return 100;if(j.status==="failed"||j.status==="cancelled")return z.size>0?Math.min(99,z.size*20):0;return Math.min(99,z.size*20)}function L(j){let q=j.getState();return(j.isTerminal()?q.updatedAt.getTime():Date.now())-q.createdAt.getTime()}function M(j){let q=j.history.filter((B)=>B.status==="completed"&&B.completedAt);if(q.length===0)return 0;return q.reduce((B,D)=>{if(!D.completedAt)return B;let H=D.completedAt.getTime()-D.startedAt.getTime();return B+H},0)/q.length}export{L as getWorkflowDuration,M as getAverageStepDuration,b as createWorkflowContext,C as calculateWorkflowProgress,Z as WorkflowContextError};
