var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var actions_exports = {};
__export(actions_exports, {
  submit: () => submit
});
module.exports = __toCommonJS(actions_exports);
var import_server = require("@tego/server");
var import__ = __toESM(require("../.."));
async function submit(context, next) {
  var _a, _b, _c;
  const repository = import_server.utils.getRepositoryFromParams(context);
  const { filterByTk, values } = context.action.params;
  const { currentUser } = context.state;
  if (!currentUser) {
    return context.throw(401);
  }
  const plugin = context.app.getPlugin(import__.default);
  const instruction = plugin.instructions.get("manual");
  const userJob = await repository.findOne({
    filterByTk,
    // filter: {
    //   userId: currentUser?.id
    // },
    appends: ["job", "node", "execution", "workflow"],
    context
  });
  if (!userJob) {
    return context.throw(404);
  }
  const { forms = {} } = userJob.node.config;
  const [formKey] = Object.keys(values.result ?? {}).filter((key) => key !== "_");
  const actionKey = (_a = values.result) == null ? void 0 : _a._;
  const actionItem = (_c = (_b = forms[formKey]) == null ? void 0 : _b.actions) == null ? void 0 : _c.find((item) => item.key === actionKey);
  if (userJob.status !== import__.JOB_STATUS.PENDING || userJob.job.status !== import__.JOB_STATUS.PENDING || userJob.execution.status !== import__.EXECUTION_STATUS.STARTED || !userJob.workflow.enabled || !actionKey || (actionItem == null ? void 0 : actionItem.status) == null) {
    return context.throw(400);
  }
  userJob.execution.workflow = userJob.workflow;
  const processor = plugin.createProcessor(userJob.execution);
  await processor.prepare();
  const assignees = processor.getParsedValue(userJob.node.config.assignees ?? [], userJob.nodeId).flat().filter(Boolean);
  if (!assignees.includes(currentUser.id) || userJob.userId !== currentUser.id) {
    return context.throw(403);
  }
  const presetValues = processor.getParsedValue(actionItem.values ?? {}, userJob.nodeId, {
    // @deprecated
    currentUser,
    // @deprecated
    currentRecord: values.result[formKey],
    // @deprecated
    currentTime: /* @__PURE__ */ new Date(),
    $user: currentUser,
    $nForm: values.result[formKey],
    $nDate: {
      now: /* @__PURE__ */ new Date()
    }
  });
  userJob.set({
    status: actionItem.status,
    result: actionItem.status > import__.JOB_STATUS.PENDING ? { [formKey]: Object.assign(values.result[formKey], presetValues), _: actionKey } : Object.assign(userJob.result ?? {}, values.result)
  });
  const handler = instruction.formTypes.get(forms[formKey].type);
  if (handler && userJob.status) {
    await handler.call(instruction, userJob, forms[formKey], processor);
  }
  await userJob.save();
  await processor.exit();
  context.body = userJob;
  context.status = 202;
  await next();
  userJob.job.execution = userJob.execution;
  userJob.job.latestUserJob = userJob;
  processor.logger.info(`manual node (${userJob.nodeId}) action trigger execution (${userJob.execution.id}) to resume`);
  plugin.resume(userJob.job);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  submit
});
