/**
 * 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 private_utils_exports = {};
__export(private_utils_exports, {
  assertNonEmptyString: () => assertNonEmptyString,
  assertOnlyAllowedKeys: () => assertOnlyAllowedKeys,
  assertPlainObject: () => assertPlainObject,
  buildApplyBlueprintTabPublicPath: () => buildApplyBlueprintTabPublicPath,
  buildScopedKey: () => buildScopedKey,
  cloneOptionalPlainObject: () => cloneOptionalPlainObject,
  normalizeApplyBlueprintToken: () => normalizeApplyBlueprintToken,
  normalizeAssetRegistry: () => normalizeAssetRegistry,
  normalizeBlueprintLocalKey: () => normalizeBlueprintLocalKey,
  normalizeTabLocalKey: () => normalizeTabLocalKey,
  readBoolean: () => readBoolean,
  readOptionalArray: () => readOptionalArray,
  readOptionalString: () => readOptionalString,
  readString: () => readString
});
module.exports = __toCommonJS(private_utils_exports);
var import_lodash = __toESM(require("lodash"));
var import_errors = require("../errors");
var import_service_utils = require("../service-utils");
function assertPlainObject(value, context) {
  if (!import_lodash.default.isPlainObject(value)) {
    (0, import_errors.throwBadRequest)(`${context} must be an object`);
  }
}
function assertOnlyAllowedKeys(input, context, allowedKeys) {
  const unsupportedKeys = Object.keys(input || {}).filter((key) => !allowedKeys.includes(key));
  if (!unsupportedKeys.length) {
    return;
  }
  (0, import_errors.throwBadRequest)(
    `${context} only accepts keys ${allowedKeys.join(", ")}; unsupported keys: ${unsupportedKeys.join(", ")}`
  );
}
function normalizeApplyBlueprintToken(value, fallback = "item") {
  const normalized = String(value || "").trim().replace(/[.[\](){}]+/g, "_").replace(/[^a-zA-Z0-9_]+/g, "_").replace(/_+/g, "_").replace(/^_+|_+$/g, "");
  return normalized || fallback;
}
function readString(value) {
  return typeof value === "string" ? value.trim() : "";
}
function readOptionalString(value) {
  const normalized = readString(value);
  return normalized || void 0;
}
function readBoolean(value, context) {
  if (import_lodash.default.isUndefined(value)) {
    return void 0;
  }
  if (typeof value !== "boolean") {
    (0, import_errors.throwBadRequest)(`${context} must be a boolean`);
  }
  return value;
}
function assertNonEmptyString(value, context) {
  const normalized = readString(value);
  if (!normalized) {
    (0, import_errors.throwBadRequest)(`${context} must be a non-empty string`);
  }
  return normalized;
}
function readOptionalArray(value, context) {
  if (import_lodash.default.isUndefined(value)) {
    return void 0;
  }
  if (!Array.isArray(value)) {
    (0, import_errors.throwBadRequest)(`${context} must be an array`);
  }
  return value;
}
function cloneOptionalPlainObject(value, context) {
  if (import_lodash.default.isUndefined(value)) {
    return void 0;
  }
  assertPlainObject(value, context);
  return import_lodash.default.cloneDeep(value);
}
function buildApplyBlueprintTabPublicPath(index) {
  return `flowSurfaces applyBlueprint tabs[${index}]`;
}
function buildScopedKey(scopePrefix, localKey) {
  return scopePrefix ? `${scopePrefix}.${localKey}` : localKey;
}
function normalizeBlueprintLocalKey(rawValue, fallback, context) {
  const explicit = readString(rawValue);
  if (explicit) {
    return (0, import_service_utils.normalizeFlowSurfaceComposeKey)(explicit, context);
  }
  return (0, import_service_utils.normalizeFlowSurfaceComposeKey)(normalizeApplyBlueprintToken(fallback, fallback), context);
}
function normalizeTabLocalKey(rawKey, fallback, index, usedKeys, options) {
  const normalizedBase = normalizeBlueprintLocalKey(rawKey, fallback, `flowSurfaces applyBlueprint tabs[${index}].key`);
  if (!usedKeys.has(normalizedBase)) {
    usedKeys.add(normalizedBase);
    return normalizedBase;
  }
  if (options.explicit) {
    (0, import_errors.throwBadRequest)(`flowSurfaces applyBlueprint tabs[${index}].key '${normalizedBase}' is duplicated`);
  }
  let suffix = 2;
  let candidate = `${normalizedBase}_${suffix}`;
  while (usedKeys.has(candidate)) {
    suffix += 1;
    candidate = `${normalizedBase}_${suffix}`;
  }
  usedKeys.add(candidate);
  return candidate;
}
function normalizeAssetRegistry(input, context) {
  if (import_lodash.default.isUndefined(input)) {
    return {};
  }
  if (!import_lodash.default.isPlainObject(input)) {
    (0, import_errors.throwBadRequest)(`${context} must be an object map`);
  }
  return Object.fromEntries(
    Object.entries(input).map(([key, value]) => {
      const normalizedKey = assertNonEmptyString(key, `${context} key`);
      if (!import_lodash.default.isPlainObject(value)) {
        (0, import_errors.throwBadRequest)(`${context}['${normalizedKey}'] must be an object`);
      }
      return [normalizedKey, import_lodash.default.cloneDeep(value)];
    })
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  assertNonEmptyString,
  assertOnlyAllowedKeys,
  assertPlainObject,
  buildApplyBlueprintTabPublicPath,
  buildScopedKey,
  cloneOptionalPlainObject,
  normalizeApplyBlueprintToken,
  normalizeAssetRegistry,
  normalizeBlueprintLocalKey,
  normalizeTabLocalKey,
  readBoolean,
  readOptionalArray,
  readOptionalString,
  readString
});
