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 tools_exports = {};
__export(tools_exports, {
  NoticeJobStatusMap: () => NoticeJobStatusMap,
  getNegotiationMode: () => getNegotiationMode,
  getSummary: () => getSummary,
  parsePerson: () => parsePerson
});
module.exports = __toCommonJS(tools_exports);
var import_lodash = __toESM(require("lodash"));
var import_constants = require("../../constants");
var import_constants2 = require("./constants");
function getSummary(params) {
  const { summaryConfig = [], data } = params;
  const result = summaryConfig.reduce((summary, key) => {
    const value = import_lodash.default.get(data, key);
    return {
      ...summary,
      [key]: value
    };
  }, {});
  return result;
}
async function parsePerson(node, processor) {
  const configPerson = processor.getParsedValue(node.config.notifiedPerson ?? [], node.id).flat().filter(Boolean);
  const notifiedPerson = /* @__PURE__ */ new Set();
  const UserRepo = processor.options.plugin.app.db.getRepository("users");
  for (const item of configPerson) {
    if (typeof item === "object") {
      const result = await UserRepo.find({
        ...item,
        fields: ["id"],
        transaction: processor.transaction
      });
      result.forEach((item2) => notifiedPerson.add(item2.id));
    } else {
      notifiedPerson.add(item);
    }
  }
  return [...notifiedPerson];
}
const NEGOTIATION_MODE = {
  SINGLE: Symbol("single"),
  ALL: Symbol("all"),
  ANY: Symbol("any"),
  PERCENTAGE: Symbol("percentage")
};
const JobStatusMap = {
  [import_constants2.NOTICE_ACTION_STATUS.PENDING]: import_constants.JOB_STATUS.PENDING,
  [import_constants2.NOTICE_ACTION_STATUS.APPROVED]: import_constants.JOB_STATUS.RESOLVED,
  [import_constants2.NOTICE_ACTION_STATUS.REJECTED]: import_constants.JOB_STATUS.REJECTED,
  [import_constants2.NOTICE_ACTION_STATUS.RETURNED]: import_constants.JOB_STATUS.RETRY_NEEDED
};
const Modes = {
  [NEGOTIATION_MODE.SINGLE]: {
    getStatus(distribution, assignees, mode) {
      const done = distribution.find((item) => item.status !== import_constants2.NOTICE_ACTION_STATUS.PENDING && item.count > 0);
      return done ? JobStatusMap[done.status] : null;
    }
  },
  [NEGOTIATION_MODE.ALL]: {
    getStatus(distribution, assignees, mode) {
      const rejected = distribution.find(
        (item) => [import_constants2.NOTICE_ACTION_STATUS.REJECTED, import_constants2.NOTICE_ACTION_STATUS.RETURNED].includes(item.status)
      );
      if (rejected && rejected.count) {
        return JobStatusMap[rejected.status];
      }
      const approved = distribution.find((item) => item.status === import_constants2.NOTICE_ACTION_STATUS.APPROVED);
      if (approved && approved.count === assignees.length) {
        return import_constants.JOB_STATUS.RESOLVED;
      }
      return null;
    }
  },
  [NEGOTIATION_MODE.ANY]: {
    getStatus(distribution, assignees, mode) {
      const returned = distribution.find((item) => item.status === import_constants2.NOTICE_ACTION_STATUS.RETURNED);
      if (returned && returned.count) {
        return import_constants.JOB_STATUS.RETRY_NEEDED;
      }
      const approved = distribution.find((item) => item.status === import_constants2.NOTICE_ACTION_STATUS.APPROVED);
      if (approved && approved.count) {
        return import_constants2.NOTICE_ACTION_STATUS.APPROVED;
      }
      const rejectedCount = distribution.reduce(
        (count, item) => item.status === import_constants2.NOTICE_ACTION_STATUS.REJECTED ? count + item.count : count,
        0
      );
      if (rejectedCount === assignees.length) {
        return import_constants.JOB_STATUS.REJECTED;
      }
      return null;
    }
  },
  [NEGOTIATION_MODE.PERCENTAGE]: {
    getStatus(distribution, assignees, mode) {
      const returned = distribution.find((item) => item.status === import_constants2.NOTICE_ACTION_STATUS.RETURNED);
      if (returned && returned.count) {
        return import_constants.JOB_STATUS.RETRY_NEEDED;
      }
      const approved = distribution.find((item) => item.status === import_constants2.NOTICE_ACTION_STATUS.APPROVED);
      if (approved && approved.count / assignees.length > mode) {
        return import_constants.JOB_STATUS.RESOLVED;
      }
      const rejected = distribution.find((item) => item.status === import_constants2.NOTICE_ACTION_STATUS.REJECTED);
      if (rejected && rejected.count / assignees.length >= 1 - mode) {
        return import_constants.JOB_STATUS.REJECTED;
      }
      return null;
    }
  }
};
const NoticeJobStatusMap = {
  [import_constants.JOB_STATUS.PENDING]: import_constants2.NOTICE_ACTION_STATUS.PENDING,
  [import_constants.JOB_STATUS.RESOLVED]: import_constants2.NOTICE_ACTION_STATUS.APPROVED,
  [import_constants.JOB_STATUS.REJECTED]: import_constants2.NOTICE_ACTION_STATUS.REJECTED,
  [import_constants.JOB_STATUS.RETRY_NEEDED]: import_constants2.NOTICE_ACTION_STATUS.RETURNED,
  [import_constants.JOB_STATUS.CANCELED]: import_constants2.NOTICE_ACTION_STATUS.CANCELED
};
function getNegotiationMode(mode) {
  switch (true) {
    case mode === 1:
      return Modes[NEGOTIATION_MODE.ALL];
    case (0 < mode && mode < 1):
      return Modes[NEGOTIATION_MODE.PERCENTAGE];
    default:
      return Modes[NEGOTIATION_MODE.SINGLE];
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  NoticeJobStatusMap,
  getNegotiationMode,
  getSummary,
  parsePerson
});
