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, {
  init: () => init
});
module.exports = __toCommonJS(actions_exports);
var import_actions = __toESM(require("@tachybase/actions"));
var import__ = require("../..");
var import_constants = require("../common/constants");
var import_workflowNotice = require("./collections/workflowNotice");
var import_constants2 = require("./constants");
const workflows = {
  async listWorkflowNoticeFlows(context, next) {
    context.action.mergeParams({
      filter: {
        type: import_constants.NOTICE_INSTRUCTION_NAMESPACE,
        enabled: true
      }
    });
    return import_actions.default.list(context, next);
  }
};
const workflowNotice = {
  async listCentralized(context, next) {
    const centralizedNoticeFlow = await context.db.getRepository(import_workflowNotice.COLLECTION_WORKFLOWS_NAME).find({
      filter: {
        "config.centralized": true
      },
      fields: ["id"]
    });
    context.action.mergeParams({
      filter: {
        workflowId: centralizedNoticeFlow.map((item) => item.id)
      }
    });
    return import_actions.default.list(context, next);
  },
  async submit(context, next) {
    const repository = import_actions.utils.getRepositoryFromParams(context);
    const { filterByTk, values } = context.action.params;
    const { currentUser } = context.state;
    if (!currentUser) {
      return context.throw(401);
    }
    const workflowNotice2 = await repository.findOne({
      filterByTk,
      filter: {
        userId: currentUser == null ? void 0 : currentUser.id
      },
      appends: ["job", "node", "execution", "workflow"],
      context
    });
    if (!workflowNotice2) {
      return context.throw(404);
    }
    if (!workflowNotice2.workflow.enabled || workflowNotice2.execution.status || workflowNotice2.job.status || workflowNotice2.status !== import_constants2.NOTICE_ACTION_STATUS.PENDING || !(workflowNotice2.node.config.actions ?? []).includes(values.status)) {
      return context.throw(400);
    }
    await workflowNotice2.update({
      status: values.status
      //   comment: values.comment,
      //   snapshot: workflowNotice.approval.data,
      //   summary: workflowNotice.approval.summary,
      //   collectionName: workflowNotice.approval.collectionName,
    });
    context.body = workflowNotice2.get();
    context.status = 202;
    await next();
    workflowNotice2.execution.workflow = workflowNotice2.workflow;
    workflowNotice2.job.execution = workflowNotice2.execution;
    workflowNotice2.job.latestUserJob = workflowNotice2.get();
    const workflow = context.app.getPlugin(import__.PluginWorkflow);
    const processor = workflow.createProcessor(workflowNotice2.execution);
    processor.logger.info(
      `notice node (${workflowNotice2.nodeId}) action trigger execution (${workflowNotice2.execution.id}) to resume`
    );
    workflow.resume(workflowNotice2.job);
  }
};
function init({ app }) {
  app.actions({
    ...make(import_workflowNotice.COLLECTION_WORKFLOWS_NAME, workflows),
    ...make(import_constants.COLLECTION_NOTICE_NAME, workflowNotice)
  });
}
function make(name, mod) {
  return Object.keys(mod).reduce(
    (result, key) => ({
      ...result,
      [`${name}:${key}`]: mod[key]
    }),
    {}
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  init
});
