/**
 * 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 defaults_exports = {};
__export(defaults_exports, {
  FLOW_SURFACE_APPLY_BLUEPRINT_POPUP_DEFAULTS_KEY: () => FLOW_SURFACE_APPLY_BLUEPRINT_POPUP_DEFAULTS_KEY,
  attachFlowSurfaceApplyBlueprintPopupDefaults: () => attachFlowSurfaceApplyBlueprintPopupDefaults,
  buildFlowSurfaceApplyBlueprintPopupDefaultsMetadata: () => buildFlowSurfaceApplyBlueprintPopupDefaultsMetadata,
  getFlowSurfaceApplyBlueprintDefaultCollection: () => getFlowSurfaceApplyBlueprintDefaultCollection,
  getFlowSurfaceApplyBlueprintDefaultFormBehavior: () => getFlowSurfaceApplyBlueprintDefaultFormBehavior,
  getFlowSurfaceDefaultFieldGroupRelationTitleFieldOverride: () => getFlowSurfaceDefaultFieldGroupRelationTitleFieldOverride,
  hasFlowSurfaceApplyBlueprintDefaults: () => hasFlowSurfaceApplyBlueprintDefaults,
  normalizeFlowSurfaceApplyBlueprintDataSourceKey: () => normalizeFlowSurfaceApplyBlueprintDataSourceKey,
  readFlowSurfaceApplyBlueprintPopupDefaultsMetadata: () => readFlowSurfaceApplyBlueprintPopupDefaultsMetadata,
  resolveFlowSurfaceApplyBlueprintDefaultCollection: () => resolveFlowSurfaceApplyBlueprintDefaultCollection,
  resolveFlowSurfaceApplyBlueprintDefaultPopupMetadata: () => resolveFlowSurfaceApplyBlueprintDefaultPopupMetadata
});
module.exports = __toCommonJS(defaults_exports);
var import_lodash = __toESM(require("lodash"));
const FLOW_SURFACE_APPLY_BLUEPRINT_POPUP_DEFAULTS_KEY = "__flowSurfaceApplyBlueprintPopupDefaults";
function hasFlowSurfaceApplyBlueprintDefaults(defaults) {
  return import_lodash.default.isPlainObject(defaults == null ? void 0 : defaults.collections) && Object.keys((defaults == null ? void 0 : defaults.collections) || {}).length > 0 || import_lodash.default.isPlainObject(defaults == null ? void 0 : defaults.dataSources) && Object.keys((defaults == null ? void 0 : defaults.dataSources) || {}).length > 0;
}
function buildFlowSurfaceApplyBlueprintPopupDefaultsMetadata(defaults) {
  if (!hasFlowSurfaceApplyBlueprintDefaults(defaults)) {
    return void 0;
  }
  return {
    collections: defaults == null ? void 0 : defaults.collections,
    dataSources: defaults == null ? void 0 : defaults.dataSources
  };
}
function attachFlowSurfaceApplyBlueprintPopupDefaults(popup, metadata) {
  if (!metadata) {
    return popup;
  }
  return {
    ...import_lodash.default.isPlainObject(popup) ? import_lodash.default.cloneDeep(popup) : {},
    [FLOW_SURFACE_APPLY_BLUEPRINT_POPUP_DEFAULTS_KEY]: import_lodash.default.cloneDeep(metadata)
  };
}
function readFlowSurfaceApplyBlueprintPopupDefaultsMetadata(value) {
  const metadata = value == null ? void 0 : value[FLOW_SURFACE_APPLY_BLUEPRINT_POPUP_DEFAULTS_KEY];
  if (!import_lodash.default.isPlainObject(metadata)) {
    return void 0;
  }
  const hasCollections = import_lodash.default.isPlainObject(metadata.collections);
  const hasDataSources = import_lodash.default.isPlainObject(metadata.dataSources);
  if (!hasCollections && !hasDataSources) {
    return void 0;
  }
  return {
    ...hasCollections ? { collections: metadata.collections } : {},
    ...hasDataSources ? { dataSources: metadata.dataSources } : {}
  };
}
function normalizeFlowSurfaceApplyBlueprintDataSourceKey(dataSourceKey) {
  return String(dataSourceKey || "").trim() || "main";
}
function getFlowSurfaceApplyBlueprintDefaultCollectionPath(dataSourceKey, collectionName) {
  return dataSourceKey === "main" ? `$.defaults.collections.${collectionName}.fieldGroups` : `$.defaults.dataSources.${dataSourceKey}.collections.${collectionName}.fieldGroups`;
}
function getFlowSurfaceApplyBlueprintDataSourceDefaultCollectionPath(dataSourceKey, collectionName) {
  return `$.defaults.dataSources.${dataSourceKey}.collections.${collectionName}.fieldGroups`;
}
function resolveFlowSurfaceApplyBlueprintDefaultCollection(input) {
  var _a, _b, _c, _d, _e, _f, _g, _h;
  const normalizedCollectionName = String(input.collectionName || "").trim();
  const normalizedDataSourceKey = normalizeFlowSurfaceApplyBlueprintDataSourceKey(input.dataSourceKey);
  if (!normalizedCollectionName) {
    return {
      dataSourceKey: normalizedDataSourceKey
    };
  }
  const dataSourceDefaults = import_lodash.default.isPlainObject((_b = (_a = input.metadata) == null ? void 0 : _a.dataSources) == null ? void 0 : _b[normalizedDataSourceKey]) ? (_d = (_c = input.metadata) == null ? void 0 : _c.dataSources) == null ? void 0 : _d[normalizedDataSourceKey] : void 0;
  const dataSourceCollections = import_lodash.default.isPlainObject(dataSourceDefaults == null ? void 0 : dataSourceDefaults.collections) ? dataSourceDefaults == null ? void 0 : dataSourceDefaults.collections : void 0;
  const hasDataSourceCollectionDefaults = Object.prototype.hasOwnProperty.call(
    dataSourceCollections || {},
    normalizedCollectionName
  );
  if (hasDataSourceCollectionDefaults) {
    const dataSourceCollectionDefaults = import_lodash.default.isPlainObject(dataSourceCollections == null ? void 0 : dataSourceCollections[normalizedCollectionName]) ? dataSourceCollections == null ? void 0 : dataSourceCollections[normalizedCollectionName] : void 0;
    return {
      collectionDefaults: dataSourceCollectionDefaults,
      dataSourceKey: normalizedDataSourceKey,
      collectionName: normalizedCollectionName,
      path: getFlowSurfaceApplyBlueprintDataSourceDefaultCollectionPath(
        normalizedDataSourceKey,
        normalizedCollectionName
      )
    };
  }
  if (normalizedDataSourceKey === "main") {
    const collectionDefaults = import_lodash.default.isPlainObject((_f = (_e = input.metadata) == null ? void 0 : _e.collections) == null ? void 0 : _f[normalizedCollectionName]) ? (_h = (_g = input.metadata) == null ? void 0 : _g.collections) == null ? void 0 : _h[normalizedCollectionName] : void 0;
    if (collectionDefaults) {
      return {
        collectionDefaults,
        dataSourceKey: normalizedDataSourceKey,
        collectionName: normalizedCollectionName,
        path: getFlowSurfaceApplyBlueprintDefaultCollectionPath(normalizedDataSourceKey, normalizedCollectionName)
      };
    }
  }
  return {
    dataSourceKey: normalizedDataSourceKey,
    collectionName: normalizedCollectionName,
    path: getFlowSurfaceApplyBlueprintDefaultCollectionPath(normalizedDataSourceKey, normalizedCollectionName)
  };
}
function getFlowSurfaceApplyBlueprintDefaultCollection(metadata, collectionName, dataSourceKey) {
  return resolveFlowSurfaceApplyBlueprintDefaultCollection({
    metadata,
    dataSourceKey,
    collectionName
  }).collectionDefaults;
}
function getFlowSurfaceApplyBlueprintDefaultFormBehavior(metadata, collectionName, actionType, dataSourceKey) {
  var _a, _b;
  if (actionType !== "addNew" && actionType !== "edit") {
    return void 0;
  }
  const scene = (_b = (_a = getFlowSurfaceApplyBlueprintDefaultCollection(metadata, collectionName, dataSourceKey)) == null ? void 0 : _a.formBehavior) == null ? void 0 : _b[actionType];
  return import_lodash.default.isPlainObject(scene) ? import_lodash.default.cloneDeep(scene) : void 0;
}
function getFlowSurfaceDefaultFieldGroupRelationTitleFieldOverride(fieldGroups, fieldPath) {
  const normalizedFieldPath = String(fieldPath || "").trim();
  if (!normalizedFieldPath) {
    return void 0;
  }
  let result;
  import_lodash.default.castArray(fieldGroups || []).forEach((group) => {
    import_lodash.default.castArray((group == null ? void 0 : group.fields) || []).forEach((field) => {
      if (result || !import_lodash.default.isPlainObject(field)) {
        return;
      }
      const currentFieldPath = String(field.field || field.fieldPath || "").trim();
      const titleField = String(field.titleField || "").trim();
      if (currentFieldPath === normalizedFieldPath && titleField) {
        result = titleField;
      }
    });
  });
  return result;
}
function readPopupMetadata(popupMap, actionType) {
  var _a, _b;
  if (!popupMap || !actionType) {
    return void 0;
  }
  const normalizedName = String(((_a = popupMap[actionType]) == null ? void 0 : _a.name) || "").trim();
  const normalizedDescription = String(((_b = popupMap[actionType]) == null ? void 0 : _b.description) || "").trim();
  if (!normalizedName || !normalizedDescription) {
    return void 0;
  }
  return {
    name: normalizedName,
    description: normalizedDescription
  };
}
function resolveFlowSurfaceApplyBlueprintDefaultPopupMetadata(input) {
  var _a;
  const actionType = input.actionType;
  const sourceCollectionDefaults = getFlowSurfaceApplyBlueprintDefaultCollection(
    input.metadata,
    input.sourceCollectionName,
    input.dataSourceKey
  );
  const associationField = String(input.associationField || "").trim();
  if (((_a = sourceCollectionDefaults == null ? void 0 : sourceCollectionDefaults.popups) == null ? void 0 : _a.associations) && associationField) {
    const associationPopupMetadata = readPopupMetadata(
      sourceCollectionDefaults.popups.associations[associationField],
      actionType
    );
    if (associationPopupMetadata) {
      return associationPopupMetadata;
    }
  }
  const targetCollectionDefaults = getFlowSurfaceApplyBlueprintDefaultCollection(
    input.metadata,
    input.targetCollectionName,
    input.dataSourceKey
  );
  return readPopupMetadata(targetCollectionDefaults == null ? void 0 : targetCollectionDefaults.popups, actionType);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  FLOW_SURFACE_APPLY_BLUEPRINT_POPUP_DEFAULTS_KEY,
  attachFlowSurfaceApplyBlueprintPopupDefaults,
  buildFlowSurfaceApplyBlueprintPopupDefaultsMetadata,
  getFlowSurfaceApplyBlueprintDefaultCollection,
  getFlowSurfaceApplyBlueprintDefaultFormBehavior,
  getFlowSurfaceDefaultFieldGroupRelationTitleFieldOverride,
  hasFlowSurfaceApplyBlueprintDefaults,
  normalizeFlowSurfaceApplyBlueprintDataSourceKey,
  readFlowSurfaceApplyBlueprintPopupDefaultsMetadata,
  resolveFlowSurfaceApplyBlueprintDefaultCollection,
  resolveFlowSurfaceApplyBlueprintDefaultPopupMetadata
});
