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

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

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

const {InputFieldController,MultiComboboxFieldController,SelectFieldController} = await importShared('@baseplate-dev/ui-components');
function FileTransformerForm({
  name,
  formProps: { control },
  originalModel,
  pluginKey
}) {
  const prefix = name;
  const controlTyped = control;
  const { definition, pluginContainer, 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 roleOptions = pluginContainer.getPluginSpec(authConfigSpec).getAuthRoles(definition).map((role) => ({
    label: role.name,
    value: role.id
  }));
  const adapterOptions = storageConfig.s3Adapters.map((adapter) => ({
    label: adapter.name,
    value: adapter.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.jsxs("div", { className: "storage:space-y-4 storage:rounded-lg storage:border storage:p-4", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx("h3", { className: "storage:text-lg storage:font-medium storage:text-foreground", children: "File Category Configuration" }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "sm:storage:grid-cols-2 storage:grid storage:grid-cols-1 storage:gap-4", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          InputFieldController,
          {
            className: "storage:w-full",
            control: controlTyped,
            label: "Category Name",
            name: `${prefix}.category.name`,
            placeholder: "e.g., USER_PROFILE_AVATAR",
            description: "Must be CONSTANT_CASE format"
          }
        ),
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          InputFieldController,
          {
            className: "storage:w-full",
            control: controlTyped,
            label: "Max File Size (MB)",
            name: `${prefix}.category.maxFileSizeMb`,
            type: "number",
            placeholder: "e.g., 10",
            description: "Maximum file size in megabytes",
            registerOptions: {
              valueAsNumber: true
            }
          }
        )
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "sm:storage:grid-cols-2 storage:grid storage:grid-cols-1 storage:gap-4", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          MultiComboboxFieldController,
          {
            className: "storage:w-full",
            control: controlTyped,
            label: "Upload Roles",
            name: `${prefix}.category.authorize.uploadRoles`,
            options: roleOptions,
            placeholder: "Select roles that can upload...",
            description: "User roles authorized to upload files"
          }
        ),
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          SelectFieldController,
          {
            className: "storage:w-full",
            control: controlTyped,
            label: "Storage Adapter",
            name: `${prefix}.category.adapterRef`,
            options: adapterOptions,
            placeholder: "Select storage adapter...",
            description: "Where files will be stored"
          }
        )
      ] })
    ] })
  ] });
}

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

const {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 = createPlatformPluginExport({
  dependencies: {
    transformerWeb: modelTransformerWebSpec
  },
  exports: {},
  initialize: ({ transformerWeb }, { pluginKey }) => {
    transformerWeb.registerTransformerWebConfig({
      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 fileRelationId = fileRelationIds[0];
        const relation = modelConfig.model.relations?.find(
          (r) => r.id === fileRelationId
        );
        if (!relation) {
          throw new Error(`Could not find relation ${fileRelationId}`);
        }
        const storageDefinition = PluginUtils.configByKeyOrThrow(
          definition,
          pluginKey
        );
        return {
          id: modelTransformerEntityType.generateNewId(),
          type: "file",
          fileRelationRef: fileRelationIds[0],
          category: {
            name: constantCase(relation.foreignRelationName),
            maxFileSizeMb: 20,
            authorize: {
              uploadRoles: ["user"]
            },
            adapterRef: storageDefinition.s3Adapters[0]?.id ?? ""
          }
        };
      },
      getSummary: (definition, definitionContainer) => [
        {
          label: "File Relation",
          description: definitionContainer.nameFromId(
            definition.fileRelationRef
          )
        }
      ]
    });
    return {};
  }
});

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