/**
 * 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 __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var relation_backing_foreign_key_exports = {};
__export(relation_backing_foreign_key_exports, {
  collectRelationBackingForeignKeyNames: () => collectRelationBackingForeignKeyNames,
  isRelationBackingForeignKeyField: () => isRelationBackingForeignKeyField
});
module.exports = __toCommonJS(relation_backing_foreign_key_exports);
var import_service_helpers = require("./service-helpers");
const CURRENT_COLLECTION_FOREIGN_KEY_RELATION_TYPES = /* @__PURE__ */ new Set(["belongsto"]);
const CURRENT_COLLECTION_FOREIGN_KEY_RELATION_INTERFACES = /* @__PURE__ */ new Set(["m2o"]);
const CONDITIONAL_CURRENT_COLLECTION_FOREIGN_KEY_RELATION_INTERFACES = /* @__PURE__ */ new Set(["o2o"]);
const RELATION_INTERFACES_WITH_FOREIGN_KEYS_OUTSIDE_CURRENT_COLLECTION = /* @__PURE__ */ new Set(["o2m", "m2m", "mbm"]);
const RELATION_TYPES_WITH_FOREIGN_KEYS_OUTSIDE_CURRENT_COLLECTION = /* @__PURE__ */ new Set(["hasone", "hasmany", "belongstomany"]);
function collectRelationBackingForeignKeyNames(collection) {
  const fields = (0, import_service_helpers.getCollectionFields)(collection);
  const scalarFieldNames = new Set(
    fields.filter((field) => !(0, import_service_helpers.isAssociationField)(field)).map((field) => String((0, import_service_helpers.getFieldName)(field) || "").trim()).filter(Boolean)
  );
  const names = /* @__PURE__ */ new Set();
  fields.forEach((field) => {
    const foreignKey = getCurrentCollectionRelationForeignKey(field);
    if (foreignKey && scalarFieldNames.has(foreignKey)) {
      names.add(foreignKey);
    }
  });
  return names;
}
function isRelationBackingForeignKeyField(collection, fieldOrFieldName) {
  const fieldName = typeof fieldOrFieldName === "string" ? fieldOrFieldName.trim() : String((0, import_service_helpers.getFieldName)(fieldOrFieldName) || "").trim();
  return !!fieldName && collectRelationBackingForeignKeyNames(collection).has(fieldName);
}
function getCurrentCollectionRelationForeignKey(field) {
  var _a;
  if (!(0, import_service_helpers.isAssociationField)(field)) {
    return "";
  }
  if (!relationCanStoreForeignKeyInCurrentCollection(field)) {
    return "";
  }
  return String((field == null ? void 0 : field.foreignKey) || ((_a = field == null ? void 0 : field.options) == null ? void 0 : _a.foreignKey) || "").trim();
}
function relationCanStoreForeignKeyInCurrentCollection(field) {
  const type = String((0, import_service_helpers.getFieldType)(field) || "").trim().toLowerCase();
  const fieldInterface = String((0, import_service_helpers.getFieldInterface)(field) || "").trim().toLowerCase();
  if (RELATION_TYPES_WITH_FOREIGN_KEYS_OUTSIDE_CURRENT_COLLECTION.has(type) || RELATION_INTERFACES_WITH_FOREIGN_KEYS_OUTSIDE_CURRENT_COLLECTION.has(fieldInterface)) {
    return false;
  }
  return CURRENT_COLLECTION_FOREIGN_KEY_RELATION_TYPES.has(type) || CURRENT_COLLECTION_FOREIGN_KEY_RELATION_INTERFACES.has(fieldInterface) || CONDITIONAL_CURRENT_COLLECTION_FOREIGN_KEY_RELATION_INTERFACES.has(fieldInterface);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  collectRelationBackingForeignKeyNames,
  isRelationBackingForeignKeyField
});
