/**
 * 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 template_compatibility_exports = {};
__export(template_compatibility_exports, {
  areFlowTemplatePopupUsesCompatible: () => areFlowTemplatePopupUsesCompatible,
  areFlowTemplateRootUsesCompatible: () => areFlowTemplateRootUsesCompatible,
  buildTemplateAssociationMismatchReason: () => buildTemplateAssociationMismatchReason,
  buildTemplateCollectionMismatchReason: () => buildTemplateCollectionMismatchReason,
  buildTemplateDataSourceMismatchReason: () => buildTemplateDataSourceMismatchReason,
  buildTemplateMissingContextReason: () => buildTemplateMissingContextReason,
  buildTemplateMissingResourceReason: () => buildTemplateMissingResourceReason,
  getFlowTemplatePopupUseFamily: () => getFlowTemplatePopupUseFamily,
  getFlowTemplateRootUseFamily: () => getFlowTemplateRootUseFamily,
  getTemplateResourceCompatibilityDisabledReason: () => getTemplateResourceCompatibilityDisabledReason,
  resolveAssociationTargetResourceInfo: () => resolveAssociationTargetResourceInfo,
  resolveBlockTemplateExpectedResourceInfo: () => resolveBlockTemplateExpectedResourceInfo,
  resolveTemplateResourceInfo: () => resolveTemplateResourceInfo
});
module.exports = __toCommonJS(template_compatibility_exports);
var import_lodash = __toESM(require("lodash"));
var import_service_helpers = require("./service-helpers");
const FLOW_TEMPLATE_ROOT_USE_FAMILIES = {
  CreateFormModel: "crudForm",
  EditFormModel: "crudForm",
  ApplyFormModel: "approvalForm",
  ProcessFormModel: "approvalForm"
};
const FLOW_TEMPLATE_POPUP_USE_FAMILIES = {
  DetailsBlockModel: "popupDetails",
  EditFormModel: "popupForm",
  CreateFormModel: "popupForm",
  TableBlockModel: "popupTable",
  ListBlockModel: "popupTable",
  GridCardBlockModel: "popupTable"
};
function getFlowTemplateRootUseFamily(use) {
  const normalizedUse = String(use || "").trim();
  return FLOW_TEMPLATE_ROOT_USE_FAMILIES[normalizedUse] || normalizedUse;
}
function areFlowTemplateRootUsesCompatible(hostUse, templateUse) {
  const normalizedHostUse = String(hostUse || "").trim();
  const normalizedTemplateUse = String(templateUse || "").trim();
  if (!normalizedHostUse || !normalizedTemplateUse) {
    return true;
  }
  return getFlowTemplateRootUseFamily(normalizedHostUse) === getFlowTemplateRootUseFamily(normalizedTemplateUse);
}
function getFlowTemplatePopupUseFamily(use) {
  const normalizedUse = String(use || "").trim();
  return FLOW_TEMPLATE_POPUP_USE_FAMILIES[normalizedUse] || normalizedUse;
}
function areFlowTemplatePopupUsesCompatible(hostUse, templateUse) {
  const normalizedHostUse = String(hostUse || "").trim();
  const normalizedTemplateUse = String(templateUse || "").trim();
  if (!normalizedHostUse || !normalizedTemplateUse) {
    return true;
  }
  return getFlowTemplatePopupUseFamily(normalizedHostUse) === getFlowTemplatePopupUseFamily(normalizedTemplateUse);
}
function buildTemplateMissingResourceReason() {
  return "missing data source or collection information";
}
function buildTemplateDataSourceMismatchReason(expectedDataSourceKey, actualDataSourceKey) {
  return `data source mismatch: expected '${expectedDataSourceKey}', got '${actualDataSourceKey}'`;
}
function buildTemplateCollectionMismatchReason(expectedCollectionName, actualCollectionName) {
  return `collection mismatch: expected '${expectedCollectionName}', got '${actualCollectionName}'`;
}
function buildTemplateAssociationMismatchReason(expectedAssociationName, actualAssociationName) {
  return `association mismatch: expected '${expectedAssociationName || "(none)"}', got '${actualAssociationName || "(none)"}'`;
}
function buildTemplateMissingContextReason(param) {
  return `cannot resolve template parameter '${param}'`;
}
function resolveAssociationTargetResourceInfo(input, options) {
  var _a;
  const dataSourceKey = String((input == null ? void 0 : input.dataSourceKey) || "").trim() || void 0;
  const collectionName = String((input == null ? void 0 : input.collectionName) || "").trim() || void 0;
  const associationName = String((input == null ? void 0 : input.associationName) || "").trim() || void 0;
  if (!associationName) {
    return {
      dataSourceKey,
      collectionName
    };
  }
  const parts = associationName.split(".").filter(Boolean);
  const baseCollectionName = (parts.length > 1 ? parts[0] : collectionName) || void 0;
  const fieldPath = (parts.length > 1 ? parts.slice(1).join(".") : associationName) || void 0;
  if (!dataSourceKey || !baseCollectionName || !fieldPath) {
    return {
      dataSourceKey,
      collectionName,
      associationName
    };
  }
  const baseCollection = options.getCollection(dataSourceKey, baseCollectionName);
  const associationField = (0, import_service_helpers.resolveFieldFromCollection)(baseCollection, fieldPath);
  const targetCollection = (0, import_service_helpers.resolveFieldTargetCollection)(
    associationField,
    dataSourceKey,
    (resolvedDsKey, targetCollectionName) => options.getCollection(resolvedDsKey, targetCollectionName)
  );
  return {
    dataSourceKey: (targetCollection == null ? void 0 : targetCollection.dataSourceKey) || ((_a = targetCollection == null ? void 0 : targetCollection.options) == null ? void 0 : _a.dataSourceKey) || dataSourceKey,
    collectionName: (0, import_service_helpers.getCollectionName)(targetCollection) || (0, import_service_helpers.getFieldTarget)(associationField) || collectionName || baseCollectionName,
    associationName
  };
}
function resolveTemplateResourceInfo(input, options) {
  return resolveAssociationTargetResourceInfo(input, options);
}
function resolveBlockTemplateExpectedResourceInfo(node, options) {
  const init = import_lodash.default.get(node, ["stepParams", "resourceSettings", "init"]) || {};
  return options.resolveTemplateResourceInfo({
    dataSourceKey: String(init.dataSourceKey || "").trim() || void 0,
    collectionName: String(init.collectionName || "").trim() || void 0,
    associationName: String(init.associationName || "").trim() || void 0
  });
}
function getTemplateResourceCompatibilityDisabledReason(template, expected, options) {
  const associationMatch = options.associationMatch || "none";
  const checkResource = options.checkResource !== false;
  const templateAssociationName = String((template == null ? void 0 : template.associationName) || "").trim() || void 0;
  const expectedAssociationName = String((expected == null ? void 0 : expected.associationName) || "").trim() || void 0;
  const getAssociationMismatchReason = () => {
    if (associationMatch === "none") {
      return void 0;
    }
    if (associationMatch === "exact") {
      if (templateAssociationName === expectedAssociationName) {
        return void 0;
      }
      return buildTemplateAssociationMismatchReason(expectedAssociationName, templateAssociationName);
    }
    if (associationMatch === "exactIfTemplateHasAssociationName") {
      if (!templateAssociationName || templateAssociationName === expectedAssociationName) {
        return void 0;
      }
      return buildTemplateAssociationMismatchReason(expectedAssociationName, templateAssociationName);
    }
    const toAssociationResource = (value) => value && value.includes(".") ? value : void 0;
    const templateAssociationResource = toAssociationResource(templateAssociationName);
    if (!templateAssociationResource) {
      return void 0;
    }
    const expectedAssociationResource = toAssociationResource(expectedAssociationName);
    if (templateAssociationResource === expectedAssociationResource) {
      return void 0;
    }
    return buildTemplateAssociationMismatchReason(expectedAssociationResource, templateAssociationResource);
  };
  if (!checkResource) {
    return getAssociationMismatchReason();
  }
  const templateResource = options.resolveTemplateResourceInfo(template || {});
  const expectedResource = options.resolveTemplateResourceInfo(expected || {});
  if (expectedResource.dataSourceKey && expectedResource.collectionName) {
    if (!templateResource.dataSourceKey || !templateResource.collectionName) {
      return buildTemplateMissingResourceReason();
    }
    if (templateResource.dataSourceKey !== expectedResource.dataSourceKey) {
      return buildTemplateDataSourceMismatchReason(expectedResource.dataSourceKey, templateResource.dataSourceKey);
    }
    if (templateResource.collectionName !== expectedResource.collectionName) {
      return buildTemplateCollectionMismatchReason(expectedResource.collectionName, templateResource.collectionName);
    }
  }
  return getAssociationMismatchReason();
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  areFlowTemplatePopupUsesCompatible,
  areFlowTemplateRootUsesCompatible,
  buildTemplateAssociationMismatchReason,
  buildTemplateCollectionMismatchReason,
  buildTemplateDataSourceMismatchReason,
  buildTemplateMissingContextReason,
  buildTemplateMissingResourceReason,
  getFlowTemplatePopupUseFamily,
  getFlowTemplateRootUseFamily,
  getTemplateResourceCompatibilityDisabledReason,
  resolveAssociationTargetResourceInfo,
  resolveBlockTemplateExpectedResourceInfo,
  resolveTemplateResourceInfo
});
