import { importShared } from './__federation_fn_import-C_QCk5FX.js';
import { j as jsxRuntimeExports } from './jsx-runtime-XI9uIe3W.js';
import { a, A as AuthConfigTabs } from './styles-DqLt1Blm.js';
import { g as getAuthPluginDefinition } from './get-auth-plugin-definition-D2XMaoy7.js';
import { A as AUTH0_MODELS, c as createAuth0PluginDefinitionSchema } from './plugin-definition-rpMj4pr8.js';

function createAuth0Models({
  authFeatureRef
}) {
  return {
    user: {
      name: AUTH0_MODELS.user,
      featureRef: authFeatureRef,
      model: {
        fields: [
          {
            name: "id",
            type: "uuid",
            options: { genUuid: true }
          },
          {
            name: "email",
            type: "string"
          },
          {
            name: "auth0Id",
            type: "string",
            isOptional: true
          }
        ],
        primaryKeyFieldRefs: ["id"],
        uniqueConstraints: [
          {
            fields: [{ fieldRef: "auth0Id" }]
          }
        ]
      },
      graphql: {
        objectType: {
          enabled: true,
          fields: ["id", "email"]
        }
      }
    }
  };
}

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

const {ModelMergerResultAlert,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 Auth0DefinitionEditor({
  definition: pluginMetadata,
  metadata,
  onSave
}) {
  const { definition, definitionContainer, saveDefinitionWithFeedback } = useProjectDefinition();
  const auth0PluginDefinitionSchema = useDefinitionSchema(
    createAuth0PluginDefinitionSchema
  );
  const defaultValues = useMemo(() => {
    if (pluginMetadata?.config) {
      return pluginMetadata.config;
    }
    return {};
  }, [pluginMetadata?.config]);
  const form = useResettableForm({
    resolver: a(auth0PluginDefinitionSchema),
    defaultValues
  });
  const { control, reset, handleSubmit } = form;
  const authDefinition = getAuthPluginDefinition(definition);
  const pendingModelChanges = useMemo(() => {
    const desiredModels = createAuth0Models(authDefinition);
    return createModelMergerResults(
      AUTH0_MODELS,
      desiredModels,
      definitionContainer
    );
  }, [definitionContainer, authDefinition]);
  const onSubmit = handleSubmit(
    (data) => saveDefinitionWithFeedback(
      (draftConfig) => {
        const updatedData = {
          ...data
        };
        createAndApplyModelMergerResults(
          draftConfig,
          AUTH0_MODELS,
          createAuth0Models(authDefinition),
          definitionContainer
        );
        PluginUtils.setPluginConfig(
          draftConfig,
          metadata,
          updatedData,
          definitionContainer
        );
      },
      {
        successMessage: "Successfully saved Auth0 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: "Auth0 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(
              ModelMergerResultAlert,
              {
                pendingModelChanges
              }
            ) })
          ] }) }) }),
          /* @__PURE__ */ jsxRuntimeExports.jsx(
            FormActionBar,
            {
              form,
              allowSaveWithoutDirty: !pluginMetadata || doesModelMergerResultsHaveChanges(pendingModelChanges)
            }
          )
        ] })
      }
    )
  ] });
}

const {createPlatformPluginExport,webConfigSpec} = await importShared('@baseplate-dev/project-builder-lib');
const web = createPlatformPluginExport({
  dependencies: {
    webConfig: webConfigSpec
  },
  exports: {},
  initialize: ({ webConfig }, { pluginKey }) => {
    webConfig.registerWebConfigComponent(pluginKey, Auth0DefinitionEditor);
    return {};
  }
});

export { web as default };
//# sourceMappingURL=__federation_expose_auth0CoreWeb-CZueDXPK.js.map
