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 flownode_check_exports = {};
__export(flownode_check_exports, {
  flownodeCheck: () => flownodeCheck
});
module.exports = __toCommonJS(flownode_check_exports);
async function checkFlowNodeLoop(ctx, currentWorkflow, rootKey, includeRoot = true) {
  var _a;
  if (includeRoot && currentWorkflow.key === rootKey) {
    return true;
  }
  const workflowNodes = currentWorkflow.nodes.filter((node) => node.type === "trigger-instruction");
  if (!workflowNodes.length) {
    return false;
  }
  if (workflowNodes.some((node) => {
    var _a2;
    return ((_a2 = node == null ? void 0 : node.config) == null ? void 0 : _a2.workflowKey) === rootKey;
  })) {
    return true;
  }
  for (const node of workflowNodes) {
    if (!((_a = node == null ? void 0 : node.config) == null ? void 0 : _a.workflowKey)) {
      continue;
    }
    const nodeWorkflow = await ctx.db.getRepository("workflows").findOne({
      filter: {
        key: node.config.workflowKey,
        enabled: true
      },
      appends: ["nodes"]
    });
    if (!nodeWorkflow) {
      continue;
    }
    const loop = await checkFlowNodeLoop(ctx, nodeWorkflow.toJSON(), rootKey);
    if (loop) {
      return true;
    }
  }
}
async function flownodeCheck(ctx, next) {
  const { resourceName, actionName } = ctx.action;
  if (resourceName === "flow_nodes" && actionName === "update") {
    const { filterByTk, values } = ctx.action.params;
    if (!values) {
      return next();
    }
    const { config } = values;
    if (!(config == null ? void 0 : config.workflowKey)) {
      return next();
    }
    const node = await ctx.db.getRepository("flow_nodes").findOne({
      filterByTk,
      appends: ["workflow"]
    });
    if (!node) {
      return next();
    }
    const currentWorkflow = await ctx.db.getRepository("workflows").findOne({
      filter: {
        key: config.workflowKey,
        enabled: true
      },
      appends: ["nodes"]
    });
    if (!currentWorkflow) {
      return next();
    }
    const loop = await checkFlowNodeLoop(ctx, currentWorkflow, node.workflow.key);
    if (loop) {
      ctx.throw(400, ctx.t("workflow loop!", { ns: "workflow" }));
    }
  } else if (resourceName === "workflows" && actionName === "update") {
    const { filterByTk, values } = ctx.action.params;
    if (!values) {
      return next();
    }
    const { enabled } = values;
    if (!enabled) {
      return next();
    }
    const workflow = await ctx.db.getRepository("workflows").findOne({
      filterByTk,
      appends: ["nodes"]
    });
    const currentWorkflow = workflow.toJSON();
    const loop = await checkFlowNodeLoop(ctx, currentWorkflow, currentWorkflow.key, false);
    if (loop) {
      ctx.throw(400, ctx.t("workflow loop!", { ns: "workflow" }));
    }
  }
  return next();
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  flownodeCheck
});
