/**
 * 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 event_flow_normalizer_exports = {};
__export(event_flow_normalizer_exports, {
  buildFlowSurfaceEmptyEventCondition: () => buildFlowSurfaceEmptyEventCondition,
  isFlowSurfaceBeforeAllEventFlow: () => isFlowSurfaceBeforeAllEventFlow,
  normalizeFlowSurfaceEventFlow: () => normalizeFlowSurfaceEventFlow,
  normalizeFlowSurfaceEventFlowOn: () => normalizeFlowSurfaceEventFlowOn,
  normalizeFlowSurfaceEventFlowRegistry: () => normalizeFlowSurfaceEventFlowRegistry
});
module.exports = __toCommonJS(event_flow_normalizer_exports);
var import_lodash = __toESM(require("lodash"));
var import_errors = require("./errors");
function asPlainRecord(value) {
  return import_lodash.default.isPlainObject(value) ? value : null;
}
function buildFlowSurfaceEmptyEventCondition() {
  return { logic: "$and", items: [] };
}
function normalizeFlowSurfaceEventFlowOn(on) {
  if (typeof on === "string") {
    const eventName2 = on.trim();
    if (!eventName2) {
      return on;
    }
    return {
      eventName: eventName2,
      defaultParams: {
        condition: buildFlowSurfaceEmptyEventCondition()
      }
    };
  }
  const onRecord = asPlainRecord(on);
  if (!onRecord) {
    return on;
  }
  const next = import_lodash.default.cloneDeep(onRecord);
  const eventName = String(next.eventName || "").trim();
  if (eventName) {
    next.eventName = eventName;
  }
  const phase = String(next.phase || "").trim();
  if (!phase || phase === "beforeAllFlows") {
    delete next.phase;
  } else {
    next.phase = phase;
  }
  const defaultParams = asPlainRecord(next.defaultParams) || {};
  if (!asPlainRecord(defaultParams.condition)) {
    defaultParams.condition = buildFlowSurfaceEmptyEventCondition();
  }
  next.defaultParams = defaultParams;
  return next;
}
function normalizeFlowSurfaceEventFlow(actionName, key, flowInput) {
  const flowRecord = asPlainRecord(flowInput);
  if (!flowRecord) {
    (0, import_errors.throwBadRequest)(`flowSurfaces ${actionName} flow '${key}' must be an object`);
  }
  const flow = import_lodash.default.cloneDeep(flowRecord);
  flow.key = key;
  if (asPlainRecord(flow.on) || typeof flow.on === "string") {
    flow.on = normalizeFlowSurfaceEventFlowOn(flow.on);
  }
  if (import_lodash.default.isUndefined(flow.steps)) {
    flow.steps = {};
  }
  return flow;
}
function normalizeFlowSurfaceEventFlowRegistry(actionName, flowRegistry) {
  const registryRecord = asPlainRecord(flowRegistry);
  if (!registryRecord) {
    return flowRegistry;
  }
  return Object.fromEntries(
    Object.entries(registryRecord).map(([key, flow]) => [key, normalizeFlowSurfaceEventFlow(actionName, key, flow)])
  );
}
function isFlowSurfaceBeforeAllEventFlow(flow) {
  const flowRecord = asPlainRecord(flow);
  if (!flowRecord) {
    return false;
  }
  const onRecord = asPlainRecord(normalizeFlowSurfaceEventFlowOn(flowRecord.on));
  return Boolean(onRecord) && typeof (onRecord == null ? void 0 : onRecord.eventName) === "string" && (typeof onRecord.phase === "undefined" || onRecord.phase === "beforeAllFlows");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildFlowSurfaceEmptyEventCondition,
  isFlowSurfaceBeforeAllEventFlow,
  normalizeFlowSurfaceEventFlow,
  normalizeFlowSurfaceEventFlowOn,
  normalizeFlowSurfaceEventFlowRegistry
});
