/**
 * 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 catalog_smart_projector_exports = {};
__export(catalog_smart_projector_exports, {
  buildCatalogItemOptionalFields: () => buildCatalogItemOptionalFields,
  buildFieldCatalogLightCandidate: () => buildFieldCatalogLightCandidate,
  expandFieldCatalogCandidate: () => expandFieldCatalogCandidate,
  projectCatalogItem: () => projectCatalogItem,
  projectCatalogNode: () => projectCatalogNode
});
module.exports = __toCommonJS(catalog_smart_projector_exports);
var import_lodash = __toESM(require("lodash"));
const LIGHT_CATALOG_ITEM_KEYS = [
  "key",
  "label",
  "use",
  "kind",
  "scope",
  "scene",
  "fieldUse",
  "wrapperUse",
  "associationPathName",
  "defaultTargetUid",
  "targetBlockUid",
  "requiredInitParams",
  "createSupported",
  "renderer",
  "type"
];
function pickLightCatalogItem(item) {
  var _a;
  const projected = import_lodash.default.pick(item, LIGHT_CATALOG_ITEM_KEYS);
  if ((_a = item.resourceBindings) == null ? void 0 : _a.length) {
    projected.resourceBindings = item.resourceBindings;
  }
  return import_lodash.default.pickBy(projected, (value) => !import_lodash.default.isUndefined(value));
}
function createNodeContractResolver(item, options) {
  let loaded = false;
  let cached;
  return () => {
    if (!loaded) {
      cached = options.getNodeContract(item.use);
      loaded = true;
    }
    return cached;
  };
}
function buildCatalogItemOptionalFields(use, options = {}, providers) {
  const projected = {};
  if (options.includeContracts) {
    const contract = providers.getNodeContract(use);
    projected.editableDomains = providers.getEditableDomains(use);
    projected.settingsSchema = providers.getSettingsSchema(use);
    projected.settingsContract = contract == null ? void 0 : contract.domains;
    projected.eventCapabilities = contract == null ? void 0 : contract.eventCapabilities;
    projected.layoutCapabilities = contract == null ? void 0 : contract.layoutCapabilities;
  }
  if (options.includeConfigureOptions) {
    projected.configureOptions = providers.getConfigureOptions(use);
  }
  return import_lodash.default.pickBy(projected, (value) => !import_lodash.default.isUndefined(value));
}
function buildFieldCatalogLightCandidate(item) {
  return pickLightCatalogItem({
    ...item,
    kind: "field"
  });
}
function projectCatalogItem(item, expand, options) {
  var _a, _b, _c, _d;
  const projected = pickLightCatalogItem(item);
  if (expand.includeItemAllowedContainerUses && ((_a = item.allowedContainerUses) == null ? void 0 : _a.length)) {
    projected.allowedContainerUses = item.allowedContainerUses;
  }
  if (expand.includeItemConfigureOptions) {
    projected.configureOptions = !import_lodash.default.isUndefined(item.configureOptions) ? item.configureOptions : options.getConfigureOptions(item);
  }
  if (expand.includeItemContracts) {
    const resolveContract = createNodeContractResolver(item, options);
    projected.editableDomains = !import_lodash.default.isUndefined(item.editableDomains) ? item.editableDomains : options.getEditableDomains(item.use);
    projected.settingsSchema = !import_lodash.default.isUndefined(item.settingsSchema) ? item.settingsSchema : options.getSettingsSchema(item.use);
    projected.settingsContract = !import_lodash.default.isUndefined(item.settingsContract) ? item.settingsContract : (_b = resolveContract()) == null ? void 0 : _b.domains;
    projected.eventCapabilities = !import_lodash.default.isUndefined(item.eventCapabilities) ? item.eventCapabilities : (_c = resolveContract()) == null ? void 0 : _c.eventCapabilities;
    projected.layoutCapabilities = !import_lodash.default.isUndefined(item.layoutCapabilities) ? item.layoutCapabilities : (_d = resolveContract()) == null ? void 0 : _d.layoutCapabilities;
  }
  return import_lodash.default.pickBy(projected, (value) => !import_lodash.default.isUndefined(value));
}
function expandFieldCatalogCandidate(item, expand, options) {
  return projectCatalogItem(buildFieldCatalogLightCandidate(item), expand, options);
}
function projectCatalogNode(node, resolved, expand, options) {
  var _a, _b, _c, _d, _e;
  const input = {
    node,
    resolved: resolved || null,
    expand,
    options
  };
  const projected = {
    editableDomains: input.options.getEditableDomains((_a = input.node) == null ? void 0 : _a.use),
    configureOptions: input.options.getConfigureOptionsForResolvedNode({
      kind: (_b = input.resolved) == null ? void 0 : _b.kind,
      use: (_c = input.node) == null ? void 0 : _c.use
    })
  };
  if (input.expand.includeNodeContracts) {
    const contract = input.options.getNodeContract((_d = input.node) == null ? void 0 : _d.use);
    projected.settingsSchema = input.options.getSettingsSchema((_e = input.node) == null ? void 0 : _e.use);
    projected.settingsContract = contract == null ? void 0 : contract.domains;
    projected.eventCapabilities = contract == null ? void 0 : contract.eventCapabilities;
    projected.layoutCapabilities = contract == null ? void 0 : contract.layoutCapabilities;
  }
  return projected;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildCatalogItemOptionalFields,
  buildFieldCatalogLightCandidate,
  expandFieldCatalogCandidate,
  projectCatalogItem,
  projectCatalogNode
});
