{"version":3,"file":"index.cjs","names":[],"sources":["../../src/workflows/state-reader.ts"],"sourcesContent":["import type {\n  WorkflowResumeLabel,\n  WorkflowState,\n  WorkflowStateSingleStepResult,\n  WorkflowStateStepResult,\n} from './types';\n\nexport type WorkflowSuspendedStep = {\n  stepId: string;\n  path: string[];\n  executionPath?: number[];\n  step?: WorkflowStateStepResult;\n  payload?: any;\n  suspendPayload?: any;\n  suspendOutput?: any;\n  resumeLabels: Record<string, WorkflowResumeLabel>;\n};\n\nexport type WorkflowStateReader = {\n  getStatus: () => WorkflowState['status'];\n  getResult: () => WorkflowState['result'];\n  getError: () => WorkflowState['error'];\n  getStepOutput: <T = any>(stepId: string) => T | Array<T | undefined> | undefined;\n  getStepPayload: <T = any>(stepId: string) => T | Array<T | undefined> | undefined;\n  getSuspendedStep: () => WorkflowSuspendedStep | undefined;\n  getSuspendedSteps: () => WorkflowSuspendedStep[];\n  getResumeLabel: (label: string) => WorkflowResumeLabel | undefined;\n  getResumeLabels: () => Record<string, WorkflowResumeLabel>;\n};\n\nconst getStep = (state: WorkflowState, stepId: string) => state.steps?.[stepId];\n\nconst getFirstStepResult = (step?: WorkflowStateStepResult): WorkflowStateSingleStepResult | undefined => {\n  return Array.isArray(step) ? (step.find(result => result?.status === 'suspended') ?? step[0]) : step;\n};\n\nconst getNestedSuspendPath = (step?: WorkflowStateStepResult): string[] => {\n  const path = getFirstStepResult(step)?.suspendPayload?.__workflow_meta?.path;\n  return Array.isArray(path) ? path.filter((part): part is string => typeof part === 'string') : [];\n};\n\nexport function getWorkflowStepOutput<T = any>(\n  state: WorkflowState,\n  stepId: string,\n): T | Array<T | undefined> | undefined {\n  const step = getStep(state, stepId);\n  return Array.isArray(step) ? step.map(result => result?.output as T | undefined) : (step?.output as T | undefined);\n}\n\nexport function getWorkflowStepPayload<T = any>(\n  state: WorkflowState,\n  stepId: string,\n): T | Array<T | undefined> | undefined {\n  const step = getStep(state, stepId);\n  return Array.isArray(step) ? step.map(result => result?.payload as T | undefined) : (step?.payload as T | undefined);\n}\n\nexport function getWorkflowResumeLabel(state: WorkflowState, label: string) {\n  const resumeLabel = state.resumeLabels?.[label];\n  return resumeLabel ? { ...resumeLabel } : undefined;\n}\n\nexport function getWorkflowResumeLabels(state: WorkflowState): Record<string, WorkflowResumeLabel> {\n  return Object.entries(state.resumeLabels ?? {}).reduce<Record<string, WorkflowResumeLabel>>(\n    (labels, [label, value]) => {\n      labels[label] = { ...value };\n      return labels;\n    },\n    {},\n  );\n}\n\nexport function getWorkflowSuspendedSteps(state: WorkflowState): WorkflowSuspendedStep[] {\n  return Object.entries(state.suspendedPaths ?? {}).map(([stepId, executionPath]) => {\n    const step = getStep(state, stepId);\n    const firstStepResult = getFirstStepResult(step);\n    const nestedPath = getNestedSuspendPath(step);\n    const path = nestedPath.length > 0 ? (nestedPath[0] === stepId ? nestedPath : [stepId, ...nestedPath]) : [stepId];\n    const resumeLabels = Object.entries(state.resumeLabels ?? {}).reduce<Record<string, WorkflowResumeLabel>>(\n      (labels, [label, value]) => {\n        if (value.stepId === stepId) {\n          labels[label] = { ...value };\n        }\n        return labels;\n      },\n      {},\n    );\n\n    return {\n      stepId,\n      path,\n      executionPath,\n      step,\n      payload: Array.isArray(step) ? step.map(result => result?.payload) : step?.payload,\n      suspendPayload: firstStepResult?.suspendPayload,\n      suspendOutput: firstStepResult?.suspendOutput,\n      resumeLabels,\n    };\n  });\n}\n\nexport function getWorkflowSuspendedStep(state: WorkflowState): WorkflowSuspendedStep | undefined {\n  return getWorkflowSuspendedSteps(state)[0];\n}\n\nexport function createWorkflowStateReader(state: WorkflowState): WorkflowStateReader {\n  return {\n    getStatus: () => state.status,\n    getResult: () => state.result,\n    getError: () => state.error,\n    getStepOutput: stepId => getWorkflowStepOutput(state, stepId),\n    getStepPayload: stepId => getWorkflowStepPayload(state, stepId),\n    getSuspendedStep: () => getWorkflowSuspendedStep(state),\n    getSuspendedSteps: () => getWorkflowSuspendedSteps(state),\n    getResumeLabel: label => getWorkflowRes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