import { importShared } from './__federation_fn_import-B0FfK6vm.js';
import { f as fileCategoryEntityType, a as createStoragePluginDefinitionSchema } from './plugin-definition-OkLTrrL5.js';

const CASE_SPLIT_PATTERN = /\p{Lu}?\p{Ll}+|[0-9]+|\p{Lu}+(?!\p{Ll})|\p{Emoji_Presentation}|\p{Extended_Pictographic}|\p{L}+/gu;
function words(str) {
    return Array.from(str.match(CASE_SPLIT_PATTERN) ?? []);
}

function constantCase(str) {
    const words$1 = words(str);
    return words$1.map(word => word.toUpperCase()).join('_');
}

const {modelTransformerEntityType} = await importShared('@baseplate-dev/project-builder-lib');
const STORAGE_PLUGIN_CONFIG_MIGRATIONS = [
  {
    name: "move-file-model",
    version: 1,
    migrate: (config) => {
      const typedConfig = config;
      return {
        updatedConfig: {
          ...typedConfig,
          storageFeatureRef: typedConfig.featureRef,
          fileModelRef: void 0,
          featureRef: void 0,
          modelRefs: {
            file: typedConfig.fileModelRef
          }
        }
      };
    }
  },
  {
    name: "move-categories-to-transformers",
    version: 2,
    migrate: (config) => {
      const typedConfig = config;
      return {
        updatedConfig: {
          ...typedConfig,
          categories: void 0
        },
        updateProjectDefinition: (draft) => {
          const typedProjectDefinition = draft;
          const fileModel = typedProjectDefinition.models.find(
            (model) => model.name === typedConfig.modelRefs.file
          );
          if (!fileModel) {
            throw new Error(
              `Could not find file model ${typedConfig.modelRefs.file}`
            );
          }
          for (const category of typedConfig.categories) {
            const model = typedProjectDefinition.models.find(
              (model2) => model2.model.relations?.some(
                (relation2) => relation2.foreignRelationName === category.usedByRelationRef && relation2.modelRef === fileModel.name
              )
            );
            if (!model) {
              throw new Error(
                `Could not find model for category ${category.name}`
              );
            }
            const relation = model.model.relations?.find(
              (relation2) => relation2.foreignRelationName === category.usedByRelationRef && relation2.modelRef === fileModel.name
            );
            if (!relation) {
              throw new Error(
                `Could not find relation for category ${category.name}`
              );
            }
            const transformer = model.service?.transformers?.find(
              (transformer2) => transformer2.fileRelationRef === relation.name
            );
            if (transformer) {
              transformer.category = {
                name: constantCase(category.name),
                maxFileSizeMb: category.maxFileSize,
                authorize: {
                  uploadRoles: category.uploadRoles
                },
                adapterRef: category.defaultAdapterRef
              };
            } else {
              model.service = {
                ...model.service,
                transformers: [
                  ...model.service?.transformers ?? [],
                  {
                    id: modelTransformerEntityType.generateNewId(),
                    type: "file",
                    fileRelationRef: relation.name,
                    category: {
                      name: constantCase(category.name),
                      maxFileSizeMb: category.maxFileSize,
                      authorize: {
                        uploadRoles: category.uploadRoles
                      },
                      adapterRef: category.defaultAdapterRef
                    }
                  }
                ]
              };
            }
          }
        }
      };
    }
  },
  {
    name: "extract-categories-to-plugin-level",
    version: 3,
    migrate: (config, projectDefinition) => {
      const typedConfig = config;
      const def = projectDefinition;
      const categoryMap = /* @__PURE__ */ new Map();
      for (const model of def.models) {
        const transformers = model.service?.transformers?.filter(
          (t) => t.type === "file" && t.category
        ) ?? [];
        for (const t of transformers) {
          if (!t.category) {
            continue;
          }
          const existing = categoryMap.get(t.category.name);
          if (existing) {
            if (existing.maxFileSizeMb !== t.category.maxFileSizeMb || existing.adapterRef !== t.category.adapterRef || JSON.stringify(existing.authorize) !== JSON.stringify(t.category.authorize)) {
              throw new Error(
                `Duplicate file category name "${t.category.name}" found with conflicting settings across models`
              );
            }
          } else {
            categoryMap.set(t.category.name, {
              id: fileCategoryEntityType.generateNewId(),
              name: t.category.name,
              maxFileSizeMb: t.category.maxFileSizeMb,
              authorize: t.category.authorize,
              adapterRef: t.category.adapterRef
            });
          }
        }
      }
      return {
        updatedConfig: {
          ...typedConfig,
          fileCategories: [...categoryMap.values()]
        },
        updateProjectDefinition: (draft) => {
          const draftDef = draft;
          for (const model of draftDef.models) {
            const transformers = model.service?.transformers?.filter(
              (t) => t.type === "file" && t.category
            ) ?? [];
            for (const t of transformers) {
              if (t.category) {
                t.categoryRef = t.category.name;
                t.category = void 0;
              }
            }
          }
        }
      };
    }
  }
];

const {createPluginModule,pluginConfigSpec} = await importShared('@baseplate-dev/project-builder-lib');
const common = createPluginModule({
  name: "common",
  dependencies: {
    pluginConfig: pluginConfigSpec
  },
  initialize: ({ pluginConfig }, { pluginKey }) => {
    pluginConfig.schemas.set(pluginKey, createStoragePluginDefinitionSchema);
    pluginConfig.migrations.set(pluginKey, STORAGE_PLUGIN_CONFIG_MIGRATIONS);
  }
});

export { common as default };
//# sourceMappingURL=__federation_expose_storageCoreCommon-B4eHaa9f.js.map
