/**
 * 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 __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
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 catalog_smart_exports = {};
__export(catalog_smart_exports, {
  analyzeCatalogTarget: () => analyzeCatalogTarget,
  buildCatalogExpandFlags: () => buildCatalogExpandFlags,
  buildCatalogItemOptionalFields: () => import_catalog_smart.buildCatalogItemOptionalFields,
  buildFieldCatalogLightCandidate: () => import_catalog_smart.buildFieldCatalogLightCandidate,
  expandFieldCatalogCandidate: () => import_catalog_smart.expandFieldCatalogCandidate,
  normalizeCatalogExpand: () => normalizeCatalogExpand,
  normalizeCatalogSections: () => normalizeCatalogSections,
  projectCatalogItem: () => import_catalog_smart.projectCatalogItem,
  projectCatalogNode: () => import_catalog_smart.projectCatalogNode,
  scenario: () => scenario,
  selectedSections: () => selectedSections
});
module.exports = __toCommonJS(catalog_smart_exports);
var import_lodash = __toESM(require("lodash"));
var import_action_scope = require("./action-scope");
var import_errors = require("./errors");
var import_field_semantics = require("./field-semantics");
__reExport(catalog_smart_exports, require("./catalog-smart.types"), module.exports);
var import_catalog_smart = require("./catalog-smart.projector");
const FLOW_SURFACE_CATALOG_SECTION_SET = /* @__PURE__ */ new Set([
  "blocks",
  "fields",
  "actions",
  "recordActions",
  "node"
]);
const FLOW_SURFACE_CATALOG_EXPAND_SET = /* @__PURE__ */ new Set([
  "item.configureOptions",
  "item.contracts",
  "item.allowedContainerUses",
  "node.contracts"
]);
function resolveCatalogPopupScenario(input) {
  if (!(input == null ? void 0 : input.isPopupSurface)) {
    return void 0;
  }
  return {
    kind: input.popupKind || "plainPopup",
    scene: input.scene || "generic",
    hasCurrentRecord: !!input.hasCurrentRecord,
    hasAssociationContext: !!input.hasAssociationContext
  };
}
function resolveCatalogFieldContainerScenario(input) {
  const kind = (0, import_field_semantics.normalizeFieldContainerKind)(input.ownerUse || void 0);
  if (!kind) {
    return void 0;
  }
  if (kind === "filter-form") {
    return {
      kind,
      targetMode: (input.filterFormTargetCount || 0) > 1 ? "multiple" : "single"
    };
  }
  return { kind };
}
function resolveCatalogActionContainerScenario(input) {
  const ownerUse = input.ownerUse || void 0;
  const scope = (0, import_action_scope.getActionContainerScope)(ownerUse);
  if (!scope) {
    return void 0;
  }
  return import_lodash.default.pickBy(
    {
      scope,
      ownerUse,
      recordActionContainerUse: input.recordActionContainerUse || void 0
    },
    (value) => !import_lodash.default.isUndefined(value)
  );
}
function normalizeCatalogSections(actionName, input) {
  if (import_lodash.default.isUndefined(input)) {
    return [];
  }
  if (!Array.isArray(input)) {
    (0, import_errors.throwBadRequest)(`flowSurfaces ${actionName} sections must be an array`);
  }
  const seen = /* @__PURE__ */ new Set();
  return input.map((value, index) => {
    const normalized = String(value || "").trim();
    if (!FLOW_SURFACE_CATALOG_SECTION_SET.has(normalized)) {
      (0, import_errors.throwBadRequest)(`flowSurfaces ${actionName} sections[${index}] '${String(value || "")}' is not supported`);
    }
    if (seen.has(normalized)) {
      return null;
    }
    seen.add(normalized);
    return normalized;
  }).filter(Boolean);
}
function normalizeCatalogExpand(actionName, input) {
  if (import_lodash.default.isUndefined(input)) {
    return [];
  }
  if (!Array.isArray(input)) {
    (0, import_errors.throwBadRequest)(`flowSurfaces ${actionName} expand must be an array`);
  }
  const seen = /* @__PURE__ */ new Set();
  return input.map((value, index) => {
    const normalized = String(value || "").trim();
    if (!FLOW_SURFACE_CATALOG_EXPAND_SET.has(normalized)) {
      (0, import_errors.throwBadRequest)(`flowSurfaces ${actionName} expand[${index}] '${String(value || "")}' is not supported`);
    }
    if (seen.has(normalized)) {
      return null;
    }
    seen.add(normalized);
    return normalized;
  }).filter(Boolean);
}
function buildCatalogExpandFlags(expands) {
  const expandSet = new Set(expands);
  return {
    includeItemConfigureOptions: expandSet.has("item.configureOptions"),
    includeItemContracts: expandSet.has("item.contracts"),
    includeItemAllowedContainerUses: expandSet.has("item.allowedContainerUses"),
    includeNodeContracts: expandSet.has("node.contracts")
  };
}
function scenario(input) {
  return import_lodash.default.pickBy(
    {
      surfaceKind: input.surfaceKind,
      popup: input.popup,
      fieldContainer: input.fieldContainer,
      actionContainer: input.actionContainer
    },
    (value) => !import_lodash.default.isUndefined(value)
  );
}
function selectedSections(input) {
  if (!import_lodash.default.isUndefined(input.requestedSections)) {
    return input.requestedSections;
  }
  if (!input.hasTarget) {
    return ["blocks", "actions", "recordActions"];
  }
  const sections = [];
  if (input.hasBlockSurface) {
    sections.push("blocks");
  }
  if (input.hasFieldContainer) {
    sections.push("fields");
  }
  if (input.hasActionContainer) {
    sections.push("actions");
  }
  if (input.hasRecordActions) {
    sections.push("recordActions");
  }
  sections.push("node");
  return sections;
}
function analyzeCatalogTarget(input) {
  var _a, _b, _c;
  const recordActionContainerUse = input.recordActionContainerUse || null;
  const popup = resolveCatalogPopupScenario(input.popupProfile);
  const fieldContainer = resolveCatalogFieldContainerScenario({
    ownerUse: (_a = input.fieldContainer) == null ? void 0 : _a.ownerUse,
    filterFormTargetCount: input.filterFormTargetCount
  });
  const actionContainer = resolveCatalogActionContainerScenario({
    ownerUse: (_b = input.actionContainer) == null ? void 0 : _b.ownerUse,
    recordActionContainerUse
  });
  const hasRecordActions = import_lodash.default.isBoolean(input.hasRecordActions) ? input.hasRecordActions : !!recordActionContainerUse || !input.hasTarget;
  return {
    scenario: scenario({
      surfaceKind: input.surfaceKind || ((_c = input.resolved) == null ? void 0 : _c.kind) || "global",
      popup,
      fieldContainer,
      actionContainer
    }),
    selectedSections: selectedSections({
      requestedSections: input.requestedSections,
      hasTarget: input.hasTarget,
      hasBlockSurface: input.hasBlockSurface,
      hasFieldContainer: !!fieldContainer,
      hasActionContainer: !!actionContainer,
      hasRecordActions
    }),
    recordActionContainerUse
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  analyzeCatalogTarget,
  buildCatalogExpandFlags,
  buildCatalogItemOptionalFields,
  buildFieldCatalogLightCandidate,
  expandFieldCatalogCandidate,
  normalizeCatalogExpand,
  normalizeCatalogSections,
  projectCatalogItem,
  projectCatalogNode,
  scenario,
  selectedSections,
  ...require("./catalog-smart.types")
});
