import { importShared } from './__federation_fn_import-BBEx2jc4.js';
import { c as constantCase } from './constantCase-C7UHP383.js';
import { c as createStoragePluginDefinitionSchema } from './plugin-definition-BODH3hW9.js';

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, _projectDefinition) => {
      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
                    }
                  }
                ]
              };
            }
          }
        }
      };
    }
  }
];

const {createPlatformPluginExport,pluginConfigSpec} = await importShared('@baseplate-dev/project-builder-lib');
const common = createPlatformPluginExport({
  dependencies: {
    config: pluginConfigSpec
  },
  exports: {},
  initialize: ({ config }, { pluginKey }) => {
    config.registerSchemaCreator(
      pluginKey,
      createStoragePluginDefinitionSchema
    );
    config.registerMigrations(pluginKey, STORAGE_PLUGIN_CONFIG_MIGRATIONS);
    return {};
  }
});

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