"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/scripts/workflow.ts
var workflow_exports = {};
__export(workflow_exports, {
  ScriptWorkflow: () => ScriptWorkflow
});
module.exports = __toCommonJS(workflow_exports);
var import_workflow = require("@temporalio/workflow");

// src/scripts/serialization.ts
var mooseJsonEncode = (data) => {
  return JSON.stringify(data, (_, value) => {
    if (value instanceof Map) {
      return {
        __type: "Map",
        value: Array.from(value.entries())
      };
    }
    return value;
  });
};

// src/scripts/workflow.ts
var { getDmv2Workflow, getTaskForWorkflow } = (0, import_workflow.proxyActivities)({
  startToCloseTimeout: "1 minutes",
  retry: {
    maximumAttempts: 1
  }
});
async function ScriptWorkflow(request, inputData) {
  const state = {
    completedSteps: [],
    currentStep: null,
    failedStep: null,
    scriptPath: null,
    inputData: null
  };
  const results = [];
  const workflowName = request.workflow_name;
  let currentData = inputData?.data || inputData || {};
  import_workflow.log.info(
    `Starting workflow: ${workflowName} (mode: ${request.execution_mode}) with data: ${JSON.stringify(currentData)}`
  );
  try {
    currentData = JSON.parse(mooseJsonEncode(currentData));
    const workflow = await getDmv2Workflow(workflowName);
    const task = request.execution_mode === "start" ? workflow.config.startingTask : await getTaskForWorkflow(workflowName, request.continue_from_task);
    const result = await handleDmv2Task(workflow, task, currentData);
    results.push(...result);
    return results;
  } catch (error) {
    state.failedStep = workflowName;
    throw error;
  }
}
async function handleDmv2Task(workflow, task, inputData) {
  const configTimeout = task.config.timeout;
  let taskTimeout;
  if (!configTimeout) {
    taskTimeout = "1h";
  } else if (configTimeout === "never") {
    taskTimeout = void 0;
  } else {
    taskTimeout = configTimeout;
  }
  const taskRetries = task.config.retries ?? 3;
  const timeoutMessage = taskTimeout ? `with timeout ${taskTimeout}` : "with no timeout (unlimited)";
  import_workflow.log.info(
    `Handling task ${task.name} ${timeoutMessage} and retries ${taskRetries}`
  );
  const activityOptions = {
    heartbeatTimeout: "10s",
    retry: {
      maximumAttempts: taskRetries
    }
  };
  if (taskTimeout) {
    activityOptions.startToCloseTimeout = taskTimeout;
  } else {
    activityOptions.scheduleToCloseTimeout = "87600h";
  }
  const { executeDmv2Task } = (0, import_workflow.proxyActivities)(activityOptions);
  let taskCompleted = false;
  const monitorTask = async () => {
    import_workflow.log.info(`Monitor task starting for ${task.name}`);
    while (!taskCompleted) {
      const info = (0, import_workflow.workflowInfo)();
      if (info.continueAsNewSuggested) {
        import_workflow.log.info(`ContinueAsNew suggested by Temporal`);
        return await (0, import_workflow.continueAsNew)({
          workflow_name: workflow.name,
          execution_mode: "continue_as_new",
          continue_from_task: task.name
        });
      }
      await (0, import_workflow.sleep)(100);
    }
    import_workflow.log.info(`Monitor task exiting because main task completed`);
  };
  const result = await Promise.race([
    executeDmv2Task(workflow, task, inputData).then((taskResult) => {
      return {
        type: "task_completed",
        data: taskResult
      };
    }).finally(() => {
      taskCompleted = true;
    }),
    monitorTask().then(() => {
      return { type: "continue_as_new", data: void 0 };
    })
  ]);
  if (result.type !== "task_completed") {
    return [];
  }
  const results = [result.data];
  if (!task.config.onComplete?.length) {
    return results;
  }
  for (const childTask of task.config.onComplete) {
    const childResult = await handleDmv2Task(workflow, childTask, result.data);
    results.push(...childResult);
  }
  if (task.name.endsWith("_extract") && result && typeof result === "object" && result.data && typeof result.data === "object" && "hasMore" in result.data && result.data.hasMore === true) {
    import_workflow.log.info(`Extract task ${task.name} has more data, restarting chain...`);
    const nextBatchResults = await handleDmv2Task(workflow, task, null);
    results.push(...nextBatchResults);
  }
  return results;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ScriptWorkflow
});
//# sourceMappingURL=workflow.js.map