/**
 * This file is part of the NocoBase (R) project.
 * Copyright (c) 2020-2024 NocoBase Co., Ltd.
 * Authors: NocoBase Team.
 *
 * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
 * For more information, please refer to: https://www.nocobase.com/agreement.
 */

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 blueprint_exports = {};
__export(blueprint_exports, {
  FLOW_SURFACE_APPROVAL_BLUEPRINT_SURFACES: () => FLOW_SURFACE_APPROVAL_BLUEPRINT_SURFACES,
  prepareFlowSurfaceApplyApprovalBlueprintDocument: () => prepareFlowSurfaceApplyApprovalBlueprintDocument
});
module.exports = __toCommonJS(blueprint_exports);
var import_lodash = __toESM(require("lodash"));
var import_errors = require("../errors");
const FLOW_SURFACE_APPROVAL_BLUEPRINT_SURFACES = ["initiator", "approver", "taskCard"];
function normalizeApprovalBlueprintVersion(value) {
  const normalized = import_lodash.default.isUndefined(value) ? "1" : String(value || "").trim();
  if (normalized !== "1") {
    (0, import_errors.throwBadRequest)(`flowSurfaces applyApprovalBlueprint only supports version '1'`);
  }
  return "1";
}
function normalizeApprovalBlueprintMode(value) {
  const normalized = import_lodash.default.isUndefined(value) ? "replace" : String(value || "").trim();
  if (normalized !== "replace") {
    (0, import_errors.throwBadRequest)(`flowSurfaces applyApprovalBlueprint only supports mode 'replace'`);
  }
  return "replace";
}
function normalizeApprovalBlueprintSurface(value) {
  const normalized = String(value || "").trim();
  if (!FLOW_SURFACE_APPROVAL_BLUEPRINT_SURFACES.includes(normalized)) {
    (0, import_errors.throwBadRequest)(
      `flowSurfaces applyApprovalBlueprint surface must be one of ${FLOW_SURFACE_APPROVAL_BLUEPRINT_SURFACES.map(
        (item) => `'${item}'`
      ).join(", ")}`
    );
  }
  return normalized;
}
function normalizeOptionalBindingId(value, fieldName) {
  if (import_lodash.default.isUndefined(value) || import_lodash.default.isNull(value) || String(value).trim() === "") {
    return void 0;
  }
  if (typeof value === "number" && Number.isFinite(value)) {
    return value;
  }
  const normalized = String(value).trim();
  if (!normalized) {
    return void 0;
  }
  if (/^\d+$/.test(normalized)) {
    return Number(normalized);
  }
  (0, import_errors.throwBadRequest)(`flowSurfaces applyApprovalBlueprint ${fieldName} must be an integer identifier`);
}
function normalizeBlocks(input) {
  if (import_lodash.default.isUndefined(input)) {
    return [];
  }
  if (!Array.isArray(input)) {
    (0, import_errors.throwBadRequest)(`flowSurfaces applyApprovalBlueprint blocks must be an array`);
  }
  const normalized = import_lodash.default.cloneDeep(input);
  normalized.forEach((block, index) => {
    if (!import_lodash.default.isPlainObject(block)) {
      (0, import_errors.throwBadRequest)(`flowSurfaces applyApprovalBlueprint blocks[${index}] must be an object`);
    }
  });
  return normalized;
}
function normalizeFields(input) {
  if (import_lodash.default.isUndefined(input)) {
    return [];
  }
  if (!Array.isArray(input)) {
    (0, import_errors.throwBadRequest)(`flowSurfaces applyApprovalBlueprint fields must be an array`);
  }
  return import_lodash.default.cloneDeep(input);
}
function normalizeLayout(input) {
  if (import_lodash.default.isUndefined(input)) {
    return void 0;
  }
  if (!import_lodash.default.isPlainObject(input)) {
    (0, import_errors.throwBadRequest)(`flowSurfaces applyApprovalBlueprint layout must be an object`);
  }
  return import_lodash.default.cloneDeep(input);
}
function prepareFlowSurfaceApplyApprovalBlueprintDocument(values) {
  const version = normalizeApprovalBlueprintVersion(values == null ? void 0 : values.version);
  const mode = normalizeApprovalBlueprintMode(values == null ? void 0 : values.mode);
  const surface = normalizeApprovalBlueprintSurface(values == null ? void 0 : values.surface);
  const workflowId = normalizeOptionalBindingId(values == null ? void 0 : values.workflowId, "workflowId");
  const nodeId = normalizeOptionalBindingId(values == null ? void 0 : values.nodeId, "nodeId");
  const layout = normalizeLayout(values == null ? void 0 : values.layout);
  if (surface === "initiator") {
    if (import_lodash.default.isUndefined(workflowId)) {
      (0, import_errors.throwBadRequest)(`flowSurfaces applyApprovalBlueprint surface 'initiator' requires workflowId`);
    }
    if (!import_lodash.default.isUndefined(nodeId)) {
      (0, import_errors.throwBadRequest)(`flowSurfaces applyApprovalBlueprint surface 'initiator' does not accept nodeId`);
    }
    if (!import_lodash.default.isUndefined(values == null ? void 0 : values.fields)) {
      (0, import_errors.throwBadRequest)(`flowSurfaces applyApprovalBlueprint surface 'initiator' does not accept fields`);
    }
    return {
      version,
      mode,
      surface,
      workflowId,
      blocks: normalizeBlocks(values == null ? void 0 : values.blocks),
      layout
    };
  }
  if (surface === "approver") {
    if (import_lodash.default.isUndefined(nodeId)) {
      (0, import_errors.throwBadRequest)(`flowSurfaces applyApprovalBlueprint surface 'approver' requires nodeId`);
    }
    if (!import_lodash.default.isUndefined(workflowId)) {
      (0, import_errors.throwBadRequest)(`flowSurfaces applyApprovalBlueprint surface 'approver' does not accept workflowId`);
    }
    if (!import_lodash.default.isUndefined(values == null ? void 0 : values.fields)) {
      (0, import_errors.throwBadRequest)(`flowSurfaces applyApprovalBlueprint surface 'approver' does not accept fields`);
    }
    return {
      version,
      mode,
      surface,
      nodeId,
      blocks: normalizeBlocks(values == null ? void 0 : values.blocks),
      layout
    };
  }
  if (import_lodash.default.isUndefined(workflowId) === import_lodash.default.isUndefined(nodeId)) {
    (0, import_errors.throwBadRequest)(
      `flowSurfaces applyApprovalBlueprint surface 'taskCard' requires exactly one of workflowId or nodeId`
    );
  }
  if (!import_lodash.default.isUndefined(values == null ? void 0 : values.blocks)) {
    (0, import_errors.throwBadRequest)(`flowSurfaces applyApprovalBlueprint surface 'taskCard' does not accept blocks`);
  }
  return {
    version,
    mode,
    surface,
    ...typeof workflowId !== "undefined" ? { workflowId } : {},
    ...typeof nodeId !== "undefined" ? { nodeId } : {},
    fields: normalizeFields(values == null ? void 0 : values.fields),
    layout
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  FLOW_SURFACE_APPROVAL_BLUEPRINT_SURFACES,
  prepareFlowSurfaceApplyApprovalBlueprintDocument
});
