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);
var LoopInstruction_exports = {};
__export(LoopInstruction_exports, {
  default: () => LoopInstruction_default
});
module.exports = __toCommonJS(LoopInstruction_exports);
var import__ = require("../..");
function getTargetLength(target) {
  let length = 0;
  if (typeof target === "number") {
    if (target < 0) {
      throw new Error("Loop target in number type must be greater than 0");
    }
    length = Math.floor(target);
  } else {
    const targets = (Array.isArray(target) ? target : [target]).filter((t) => t != null);
    length = targets.length;
  }
  return length;
}
class LoopInstruction_default extends import__.Instruction {
  async run(node, prevJob, processor) {
    const [branch] = processor.getBranches(node);
    const target = processor.getParsedValue(node.config.target, node.id);
    const length = getTargetLength(target);
    if (!branch || !length) {
      return {
        status: import__.JOB_STATUS.RESOLVED,
        result: 0
      };
    }
    const job = await processor.saveJob({
      status: import__.JOB_STATUS.PENDING,
      // save loop index
      result: 0,
      nodeId: node.id,
      nodeKey: node.key,
      upstreamId: (prevJob == null ? void 0 : prevJob.id) ?? null
    });
    await processor.run(branch, job);
    return null;
  }
  async resume(node, branchJob, processor) {
    const job = processor.findBranchParentJob(branchJob, node);
    const loop = processor.nodesMap.get(job.nodeId);
    const [branch] = processor.getBranches(node);
    const { result, status } = job;
    if (status !== import__.JOB_STATUS.PENDING) {
      return processor.exit();
    }
    const nextIndex = result + 1;
    const target = processor.getParsedValue(loop.config.target, node.id);
    if (branchJob.status > import__.JOB_STATUS.PENDING) {
      job.set({ result: nextIndex });
      const length = getTargetLength(target);
      if (nextIndex < length) {
        await processor.saveJob(job);
        await processor.run(branch, job);
        return null;
      }
    }
    job.set({
      status: branchJob.status
    });
    return job;
  }
  getScope(node, index, processor) {
    var _a, _b, _c;
    const target = processor.getParsedValue(node.config.target, node.id);
    const targets = (Array.isArray(target) ? target : [target]).filter((t) => t != null);
    const length = getTargetLength(target);
    const item = typeof target === "number" ? index : targets[index];
    const params = (_c = (_b = (_a = processor.options) == null ? void 0 : _a.httpContext) == null ? void 0 : _b.action) == null ? void 0 : _c.params;
    const loopData = (params == null ? void 0 : params.loopLength) ? {
      loopLength: params.loopLength,
      loopIndex: Number(params.loopIndex) + 1
    } : null;
    const result = {
      item,
      index,
      length,
      loopData
    };
    this.workflow.noticeManager.notify("workflow:regular", {
      msg: "progress",
      current: index,
      total: length,
      loopData
    });
    return result;
  }
}
