import { importShared } from './__federation_fn_import-BLFDCY-V.js';
import { j as jsxRuntimeExports } from './jsx-runtime-XI9uIe3W.js';
import { a, A as AuthConfigTabs } from './styles-tkrWV-ZE.js';
import { g as getAuthPluginDefinition } from './get-auth-plugin-definition-BL50g0cJ.js';
import { P as PLACEHOLDER_AUTH_MODELS, c as createPlaceholderAuthPluginDefinitionSchema } from './plugin-definition-D26rR8AB.js';

function createPlaceholderAuthPartialDefinition(authFeatureName) {
  return {
    models: [
      {
        name: PLACEHOLDER_AUTH_MODELS.user,
        featureRef: authFeatureName,
        model: {
          fields: [
            {
              name: "id",
              type: "uuid",
              options: { genUuid: true }
            },
            {
              name: "email",
              type: "string",
              isOptional: true
            }
          ],
          primaryKeyFieldRefs: ["id"],
          uniqueConstraints: [
            {
              fields: [{ fieldRef: "email" }]
            }
          ]
        },
        graphql: {
          objectType: {
            enabled: true,
            fields: [{ ref: "id" }, { ref: "email" }]
          }
        }
      }
    ]
  };
}

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

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

const {FormActionBar,SectionList,SectionListSection,SectionListSectionContent,SectionListSectionDescription,SectionListSectionHeader,SectionListSectionTitle} = await importShared('@baseplate-dev/ui-components');
const {useMemo} = await importShared('react');
function PlaceholderAuthDefinitionEditor({
  definition: pluginMetadata,
  metadata,
  onSave
}) {
  const { definition, definitionContainer, saveDefinitionWithFeedback } = useProjectDefinition();
  const authDefinition = getAuthPluginDefinition(definition);
  const authPluginDefinitionSchema = useDefinitionSchema(
    createPlaceholderAuthPluginDefinitionSchema
  );
  const defaultValues = useMemo(() => {
    if (pluginMetadata?.config) {
      return pluginMetadata.config;
    }
    return {};
  }, [pluginMetadata?.config]);
  const form = useResettableForm({
    resolver: a(authPluginDefinitionSchema),
    defaultValues
  });
  const { control, reset, handleSubmit } = form;
  const authFeature = definition.features.find(
    (f) => f.id === authDefinition.authFeatureRef
  );
  if (!authFeature) {
    throw new Error(
      `Auth feature not found for ref: ${authDefinition.authFeatureRef}`
    );
  }
  const partialDef = useMemo(
    () => createPlaceholderAuthPartialDefinition(authFeature.name),
    [authFeature.name]
  );
  const diff = useMemo(
    () => diffDefinition(
      definitionContainer.schema,
      definitionContainer.definition,
      partialDef
    ),
    [definitionContainer, partialDef]
  );
  const onSubmit = handleSubmit(
    (data) => saveDefinitionWithFeedback(
      (draftConfig) => {
        const updatedData = {
          ...data
        };
        applyMergedDefinition(definitionContainer, partialDef)(draftConfig);
        PluginUtils.setPluginConfig(
          draftConfig,
          metadata,
          updatedData,
          definitionContainer
        );
      },
      {
        successMessage: "Successfully saved placeholder auth plugin!",
        onSuccess: () => {
          onSave();
        }
      }
    )
  );
  useBlockUnsavedChangesNavigate({ control, reset, onSubmit });
  return /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "auth:relative auth:flex auth:h-full auth:flex-1 auth:flex-col auth:gap-4 auth:overflow-hidden", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(AuthConfigTabs, {}),
    /* @__PURE__ */ jsxRuntimeExports.jsx(
      "div",
      {
        className: "auth:mb-[--action-bar-height] auth:flex auth:flex-1 auth:overflow-y-auto",
        style: {
          "--action-bar-height": "52px"
        },
        children: /* @__PURE__ */ jsxRuntimeExports.jsxs("form", { onSubmit, className: "auth:max-w-6xl auth:flex-1", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "auth:pb-16", children: /* @__PURE__ */ jsxRuntimeExports.jsx(SectionList, { children: /* @__PURE__ */ jsxRuntimeExports.jsxs(SectionListSection, { children: [
            /* @__PURE__ */ jsxRuntimeExports.jsxs(SectionListSectionHeader, { children: [
              /* @__PURE__ */ jsxRuntimeExports.jsx(SectionListSectionTitle, { children: "Placeholder Auth Configuration" }),
              /* @__PURE__ */ jsxRuntimeExports.jsx(SectionListSectionDescription, { children: "The plugin will automatically configure the models it needs." })
            ] }),
            /* @__PURE__ */ jsxRuntimeExports.jsx(SectionListSectionContent, { className: "auth:space-y-6", children: /* @__PURE__ */ jsxRuntimeExports.jsx(
              DefinitionDiffAlert,
              {
                diff,
                upToDateMessage: "All required models are already configured correctly. No changes needed."
              }
            ) })
          ] }) }) }),
          /* @__PURE__ */ jsxRuntimeExports.jsx(
            FormActionBar,
            {
              form,
              allowSaveWithoutDirty: !pluginMetadata || diff.hasChanges
            }
          )
        ] })
      }
    )
  ] });
}

const {createPluginModule,webConfigSpec} = await importShared('@baseplate-dev/project-builder-lib');
const web = createPluginModule({
  name: "web",
  dependencies: {
    webConfig: webConfigSpec
  },
  initialize: ({ webConfig }, { pluginKey }) => {
    webConfig.components.set(pluginKey, PlaceholderAuthDefinitionEditor);
  }
});

export { web as default };
//# sourceMappingURL=__federation_expose_placeholder-authCoreWeb-BaV-Ko2C.js.map
