/**
 * 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 reference_guards_exports = {};
__export(reference_guards_exports, {
  assertNoFlowSurfaceLegacyRef: () => assertNoFlowSurfaceLegacyRef,
  isFlowSurfacePlanStepValue: () => isFlowSurfacePlanStepValue,
  normalizeFlowSurfacePlanStepValue: () => normalizeFlowSurfacePlanStepValue,
  throwUnsupportedFlowSurfaceDollarRef: () => throwUnsupportedFlowSurfaceDollarRef,
  throwUnsupportedFlowSurfaceRef: () => throwUnsupportedFlowSurfaceRef
});
module.exports = __toCommonJS(reference_guards_exports);
var import_lodash = __toESM(require("lodash"));
var import_errors = require("./errors");
function isFlowSurfacePlanStepValue(input) {
  return import_lodash.default.isPlainObject(input) && Object.keys(input).every((key) => ["step", "path"].includes(key)) && typeof input.step === "string" && !!input.step.trim();
}
function normalizeFlowSurfacePlanStepValue(actionName, input, fieldName) {
  if (!import_lodash.default.isPlainObject(input)) {
    (0, import_errors.throwBadRequest)(`flowSurfaces ${actionName} ${fieldName} must be an object`);
  }
  if (Object.keys(input).some((key) => !["step", "path"].includes(key))) {
    (0, import_errors.throwBadRequest)(`flowSurfaces ${actionName} ${fieldName} only supports step/path`);
  }
  const step = String(input.step || "").trim();
  if (!step) {
    (0, import_errors.throwBadRequest)(`flowSurfaces ${actionName} ${fieldName}.step cannot be empty`);
  }
  const path = import_lodash.default.isUndefined(input.path) ? void 0 : String(input.path || "").trim();
  if (import_lodash.default.has(input, "path") && !path) {
    (0, import_errors.throwBadRequest)(`flowSurfaces ${actionName} ${fieldName}.path cannot be empty`);
  }
  return {
    step,
    ...path ? { path } : {}
  };
}
function throwUnsupportedFlowSurfaceDollarRef(actionName, path, allowed) {
  (0, import_errors.throwBadRequest)(`flowSurfaces ${actionName} ${path} must use ${allowed}; "$ref" is not supported`);
}
function throwUnsupportedFlowSurfaceRef(actionName, path, allowed) {
  (0, import_errors.throwBadRequest)(`flowSurfaces ${actionName} ${path} does not support { ref }; use ${allowed}`);
}
function assertNoFlowSurfaceLegacyRef(input, options) {
  if (!import_lodash.default.isPlainObject(input)) {
    return;
  }
  if (Object.prototype.hasOwnProperty.call(input, "$ref")) {
    throwUnsupportedFlowSurfaceDollarRef(
      options.actionName,
      options.path,
      options.dollarRefAllowed || options.refAllowed || "key"
    );
  }
  if (Object.prototype.hasOwnProperty.call(input, "ref")) {
    throwUnsupportedFlowSurfaceRef(
      options.actionName,
      options.path,
      options.refAllowed || options.dollarRefAllowed || "key"
    );
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  assertNoFlowSurfaceLegacyRef,
  isFlowSurfacePlanStepValue,
  normalizeFlowSurfacePlanStepValue,
  throwUnsupportedFlowSurfaceDollarRef,
  throwUnsupportedFlowSurfaceRef
});
