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 { B as BETTER_AUTH_MODELS, c as createBetterAuthPluginDefinitionSchema } from './plugin-definition-CXnKUJ1l.js';

function createBetterAuthPartialDefinition(authFeatureName) {
  return {
    models: [
      {
        name: BETTER_AUTH_MODELS.user,
        featureRef: authFeatureName,
        model: {
          fields: [
            {
              name: "id",
              type: "uuid",
              options: { genUuid: true }
            },
            {
              name: "name",
              type: "string",
              isOptional: false
            },
            {
              name: "email",
              type: "string",
              isOptional: false
            },
            {
              name: "emailVerified",
              type: "boolean",
              options: { default: "false" }
            },
            {
              name: "image",
              type: "string",
              isOptional: true
            },
            {
              name: "updatedAt",
              type: "dateTime",
              options: { defaultToNow: true, updatedAt: true }
            },
            {
              name: "createdAt",
              type: "dateTime",
              options: { defaultToNow: true }
            }
          ],
          primaryKeyFieldRefs: ["id"],
          uniqueConstraints: [
            {
              fields: [{ fieldRef: "email" }]
            }
          ]
        },
        graphql: {
          objectType: {
            enabled: true,
            fields: [{ ref: "id" }, { ref: "email" }]
          }
        }
      },
      {
        name: BETTER_AUTH_MODELS.session,
        featureRef: authFeatureName,
        model: {
          fields: [
            {
              name: "id",
              type: "uuid",
              options: { genUuid: true }
            },
            {
              name: "expiresAt",
              type: "dateTime"
            },
            {
              name: "token",
              type: "string"
            },
            {
              name: "ipAddress",
              type: "string",
              isOptional: true
            },
            {
              name: "userAgent",
              type: "string",
              isOptional: true
            },
            {
              name: "userId",
              type: "uuid"
            },
            {
              name: "createdAt",
              type: "dateTime",
              options: { defaultToNow: true }
            },
            {
              name: "updatedAt",
              type: "dateTime",
              options: { defaultToNow: true, updatedAt: true }
            }
          ],
          primaryKeyFieldRefs: ["id"],
          uniqueConstraints: [
            {
              fields: [{ fieldRef: "token" }]
            }
          ],
          relations: [
            {
              name: "user",
              references: [{ localRef: "userId", foreignRef: "id" }],
              modelRef: BETTER_AUTH_MODELS.user,
              foreignRelationName: "sessions",
              onDelete: "Cascade",
              onUpdate: "Restrict"
            }
          ]
        }
      },
      {
        name: BETTER_AUTH_MODELS.account,
        featureRef: authFeatureName,
        model: {
          fields: [
            {
              name: "id",
              type: "uuid",
              options: { genUuid: true }
            },
            {
              name: "accountId",
              type: "string"
            },
            {
              name: "providerId",
              type: "string"
            },
            {
              name: "userId",
              type: "uuid"
            },
            {
              name: "accessToken",
              type: "string",
              isOptional: true
            },
            {
              name: "refreshToken",
              type: "string",
              isOptional: true
            },
            {
              name: "idToken",
              type: "string",
              isOptional: true
            },
            {
              name: "accessTokenExpiresAt",
              type: "dateTime",
              isOptional: true
            },
            {
              name: "refreshTokenExpiresAt",
              type: "dateTime",
              isOptional: true
            },
            {
              name: "scope",
              type: "string",
              isOptional: true
            },
            {
              name: "password",
              type: "string",
              isOptional: true
            },
            {
              name: "createdAt",
              type: "dateTime",
              options: { defaultToNow: true }
            },
            {
              name: "updatedAt",
              type: "dateTime",
              options: { defaultToNow: true, updatedAt: true }
            }
          ],
          primaryKeyFieldRefs: ["id"],
          relations: [
            {
              name: "user",
              references: [{ localRef: "userId", foreignRef: "id" }],
              modelRef: BETTER_AUTH_MODELS.user,
              foreignRelationName: "accounts",
              onDelete: "Cascade",
              onUpdate: "Restrict"
            }
          ]
        }
      },
      {
        name: BETTER_AUTH_MODELS.verification,
        featureRef: authFeatureName,
        model: {
          fields: [
            {
              name: "id",
              type: "uuid",
              options: { genUuid: true }
            },
            {
              name: "identifier",
              type: "string"
            },
            {
              name: "value",
              type: "string"
            },
            {
              name: "expiresAt",
              type: "dateTime"
            },
            {
              name: "createdAt",
              type: "dateTime",
              options: { defaultToNow: true }
            },
            {
              name: "updatedAt",
              type: "dateTime",
              options: { defaultToNow: true, updatedAt: true }
            }
          ],
          primaryKeyFieldRefs: ["id"]
        }
      },
      {
        name: BETTER_AUTH_MODELS.userRole,
        featureRef: authFeatureName,
        model: {
          fields: [
            {
              name: "userId",
              type: "uuid"
            },
            {
              name: "role",
              type: "string"
            },
            {
              name: "updatedAt",
              type: "dateTime",
              options: { defaultToNow: true, updatedAt: true }
            },
            {
              name: "createdAt",
              type: "dateTime",
              options: { defaultToNow: true }
            }
          ],
          primaryKeyFieldRefs: ["userId", "role"],
          relations: [
            {
              name: "user",
              references: [{ localRef: "userId", foreignRef: "id" }],
              modelRef: BETTER_AUTH_MODELS.user,
              foreignRelationName: "roles",
              onDelete: "Cascade",
              onUpdate: "Restrict"
            }
          ]
        }
      }
    ]
  };
}

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 BetterAuthDefinitionEditor({
  definition: pluginMetadata,
  metadata,
  onSave
}) {
  const { definition, definitionContainer, saveDefinitionWithFeedback } = useProjectDefinition();
  const betterAuthPluginDefinitionSchema = useDefinitionSchema(
    createBetterAuthPluginDefinitionSchema
  );
  const defaultValues = useMemo(() => {
    if (pluginMetadata?.config) {
      return pluginMetadata.config;
    }
    return {};
  }, [pluginMetadata?.config]);
  const form = useResettableForm({
    resolver: a(betterAuthPluginDefinitionSchema),
    defaultValues
  });
  const { control, reset, handleSubmit } = form;
  const authDefinition = getAuthPluginDefinition(definition);
  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(
    () => createBetterAuthPartialDefinition(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 Better 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: "Better 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, BetterAuthDefinitionEditor);
  }
});

export { web as default };
//# sourceMappingURL=__federation_expose_better-authCoreWeb-CMhPrXPA.js.map
