import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { FeatureUtils, getManagedPluginsForPlugin, PluginUtils, } from '@baseplate-dev/project-builder-lib';
import { FeatureComboboxFieldController, useBlockUnsavedChangesNavigate, useDefinitionSchema, useProjectDefinition, useResettableForm, } from '@baseplate-dev/project-builder-lib/web';
import { Alert, AlertDescription, AlertTitle, FormActionBar, SectionList, SectionListSection, SectionListSectionContent, SectionListSectionDescription, SectionListSectionHeader, SectionListSectionTitle, SelectFieldController, } from '@baseplate-dev/ui-components';
import { zodResolver } from '@hookform/resolvers/zod';
import { Link, useNavigate } from '@tanstack/react-router';
import { useMemo } from 'react';
import { AuthConfigTabs } from '#src/common/components/auth-config-tabs.js';
import { createAuthPluginDefinitionSchema } from '../schema/plugin-definition.js';
import { createDefaultAuthRoles } from '../schema/roles/constants.js';
import { RoleEditorForm } from './role-editor-form/role-editor-form.js';
import '#src/styles.css';
export function AuthDefinitionEditor({ definition: pluginMetadata, metadata, onSave, }) {
    const { definition, definitionContainer, schemaParserContext, saveDefinitionWithFeedback, } = useProjectDefinition();
    const authPluginDefinitionSchema = useDefinitionSchema(createAuthPluginDefinitionSchema);
    const availableImplementations = getManagedPluginsForPlugin(schemaParserContext.pluginStore, metadata.key);
    // Use local auth by default
    const defaultImplementation = availableImplementations.find((i) => i.fullyQualifiedName === '@baseplate-dev/plugin-auth:local-auth') ?? availableImplementations[0];
    const defaultValues = useMemo(() => {
        if (pluginMetadata?.config) {
            return pluginMetadata.config;
        }
        const defaultAuthFeatureRef = FeatureUtils.getFeatureIdByNameOrDefault(definition, 'auth');
        return {
            implementationPluginKey: defaultImplementation.key,
            authFeatureRef: defaultAuthFeatureRef,
            roles: createDefaultAuthRoles(),
        };
    }, [definition, pluginMetadata?.config, defaultImplementation]);
    const form = useResettableForm({
        resolver: zodResolver(authPluginDefinitionSchema),
        defaultValues,
    });
    const { control, reset, handleSubmit, watch } = form;
    const implementationPluginKey = watch('implementationPluginKey');
    const selectedPlugin = implementationPluginKey &&
        availableImplementations.find((i) => i.key === implementationPluginKey);
    const isSelectedImplementationActive = !!PluginUtils.byKey(definitionContainer.definition, implementationPluginKey);
    const hasSelectedImplementationChanged = implementationPluginKey !==
        pluginMetadata?.config
            ?.implementationPluginKey;
    const selectedImplementationUrl = selectedPlugin
        ? `/plugins/edit/${selectedPlugin.key}`
        : undefined;
    const navigate = useNavigate();
    const onSubmit = handleSubmit((data) => saveDefinitionWithFeedback((draftConfig) => {
        const featureRef = FeatureUtils.ensureFeatureByNameRecursively(draftConfig, data.authFeatureRef);
        PluginUtils.setPluginConfig(draftConfig, metadata, {
            ...data,
            authFeatureRef: featureRef,
        }, definitionContainer);
        // Make sure we disable other plugins that are not the selected implementation
        const otherPlugins = availableImplementations.filter((i) => i.key !== implementationPluginKey);
        for (const plugin of otherPlugins) {
            PluginUtils.disablePlugin(draftConfig, plugin.key, definitionContainer.parserContext);
        }
    }, {
        successMessage: `Successfully saved auth plugin! ${selectedImplementationUrl && !isSelectedImplementationActive ? `Please configure the provider plugin before syncing.` : ''}`,
        onSuccess: () => {
            onSave();
            if (selectedImplementationUrl && !isSelectedImplementationActive) {
                setTimeout(() => {
                    navigate({ to: selectedImplementationUrl });
                }, 100);
            }
        },
    }));
    useBlockUnsavedChangesNavigate({ control, reset, onSubmit });
    return (_jsxs("div", { className: "auth:relative auth:flex auth:h-full auth:flex-1 auth:flex-col auth:gap-4 auth:overflow-hidden", children: [_jsx(AuthConfigTabs, {}), _jsx("div", { className: "auth:mb-[--action-bar-height] auth:flex auth:flex-1 auth:overflow-y-auto", style: {
                    '--action-bar-height': '52px',
                }, children: _jsxs("form", { onSubmit: onSubmit, className: "auth:max-w-6xl auth:flex-1", children: [_jsxs("div", { className: "auth:pb-16", children: [!isSelectedImplementationActive &&
                                    selectedPlugin &&
                                    !hasSelectedImplementationChanged && (_jsxs(Alert, { variant: "warning", children: [_jsx(AlertTitle, { children: "Implementation Provider Not Active" }), _jsx(AlertDescription, { children: _jsxs("p", { children: ["The selected implementation provider is not active. Please make sure", ' ', _jsx(Link, { to: selectedImplementationUrl, className: "auth:inline-block auth:font-medium", children: "configure the provider" }), ' ', "before syncing."] }) })] })), _jsxs(SectionList, { children: [_jsxs(SectionListSection, { children: [_jsxs(SectionListSectionHeader, { children: [_jsx(SectionListSectionTitle, { children: "General Settings" }), _jsx(SectionListSectionDescription, { children: "Configure the general settings for the auth plugin." })] }), _jsxs(SectionListSectionContent, { className: "auth:space-y-6", children: [_jsx(FeatureComboboxFieldController, { label: "Auth Feature Path", name: "authFeatureRef", control: control, canCreate: true, description: "Specify the feature path where auth functionality will be generated" }), _jsx(SelectFieldController, { label: "Authentication Provider", name: "implementationPluginKey", control: control, options: availableImplementations, renderItemLabel: (item) => (_jsxs("div", { className: "auth:flex auth:flex-col auth:gap-2", children: [_jsx("div", { className: "auth:text-sm auth:font-medium", children: item.displayName }), _jsx("div", { className: "auth:text-sm auth:text-muted-foreground", children: item.description })] })), getOptionLabel: (item) => item.displayName, getOptionValue: (item) => item.key, description: "The authentication provider to use for your project" })] })] }), _jsx(RoleEditorForm, { control: control })] })] }), _jsx(FormActionBar, { form: form, allowSaveWithoutDirty: !pluginMetadata })] }) })] }));
}
//# sourceMappingURL=auth-definition-editor.js.map