import { importShared } from './__federation_fn_import-B0FfK6vm.js';
import { S as STORAGE_MODELS } from './model-names-BRVtMdoR.js';
import { j as jsxRuntimeExports } from './jsx-runtime-XI9uIe3W.js';
/* empty css                */

const {PluginUtils: PluginUtils$1} = await importShared('@baseplate-dev/project-builder-lib');

const {useProjectDefinition} = await importShared('@baseplate-dev/project-builder-lib/web');

const {SelectFieldController} = await importShared('@baseplate-dev/ui-components');
function FileTransformerForm({
  name,
  formProps: { control },
  originalModel,
  pluginKey
}) {
  const prefix = name;
  const controlTyped = control;
  const { definition, definitionContainer } = useProjectDefinition();
  const storageConfig = PluginUtils$1.configByKeyOrThrow(
    definition,
    pluginKey ?? ""
  );
  const fileRelations = originalModel.model.relations?.filter(
    (relation) => definitionContainer.nameFromId(relation.modelRef) === STORAGE_MODELS.file
  ) ?? [];
  const relationOptions = fileRelations.map((relation) => ({
    label: relation.name,
    value: relation.id
  }));
  const categoryOptions = storageConfig.fileCategories.map((category) => ({
    label: category.name,
    value: category.id
  }));
  return /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "storage:space-y-6", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(
      SelectFieldController,
      {
        className: "storage:w-full",
        control: controlTyped,
        label: "File Relation",
        name: `${prefix}.fileRelationRef`,
        options: relationOptions,
        placeholder: "Select a file relation..."
      }
    ),
    /* @__PURE__ */ jsxRuntimeExports.jsx(
      SelectFieldController,
      {
        className: "storage:w-full",
        control: controlTyped,
        label: "File Category",
        name: `${prefix}.categoryRef`,
        options: categoryOptions,
        placeholder: "Select a file category...",
        description: "The file category that defines upload constraints and authorization for this relation"
      }
    )
  ] });
}

const {createPluginModule,modelTransformerEntityType,ModelUtils,PluginUtils} = await importShared('@baseplate-dev/project-builder-lib');

const {createModelTransformerWebConfig,modelTransformerWebSpec} = await importShared('@baseplate-dev/project-builder-lib/web');
function findNonTransformedFileRelations(projectContainer, modelConfig) {
  const { transformers } = modelConfig.service ?? {};
  const fileTransformers = transformers?.filter(
    (transformer) => transformer.type === "file"
  );
  return modelConfig.model.relations?.filter(
    (relation) => ModelUtils.byIdOrThrow(projectContainer.definition, relation.modelRef).name === STORAGE_MODELS.file && !fileTransformers?.some(
      (transformer) => transformer.fileRelationRef === relation.id
    )
  ).map((r) => r.id) ?? [];
}
const web = createPluginModule({
  name: "web",
  dependencies: {
    transformerWeb: modelTransformerWebSpec
  },
  initialize: ({ transformerWeb }, { pluginKey }) => {
    transformerWeb.transformers.add(
      createModelTransformerWebConfig({
        name: "file",
        label: "File",
        description: "Validates and associates file ID to field",
        instructions: "Select a file relation to transform",
        pluginKey,
        Form: FileTransformerForm,
        allowNewTransformer(projectContainer, modelConfig) {
          return findNonTransformedFileRelations(projectContainer, modelConfig).length > 0;
        },
        getNewTransformer: (projectContainer, modelConfig) => {
          const { definition } = projectContainer;
          const fileRelationIds = findNonTransformedFileRelations(
            projectContainer,
            modelConfig
          );
          const storageDefinition = PluginUtils.configByKeyOrThrow(
            definition,
            pluginKey
          );
          return {
            id: modelTransformerEntityType.generateNewId(),
            type: "file",
            fileRelationRef: fileRelationIds[0],
            categoryRef: storageDefinition.fileCategories[0]?.id ?? ""
          };
        },
        getSummary: (definition, definitionContainer) => [
          {
            label: "File Relation",
            description: definitionContainer.nameFromId(
              definition.fileRelationRef
            )
          },
          {
            label: "Category",
            description: definitionContainer.nameFromId(definition.categoryRef)
          }
        ]
      })
    );
  }
});

export { web as default };
//# sourceMappingURL=__federation_expose_storageTransformersWeb-Cr77O5xA.js.map
