import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { createAndApplyModelMergerResults, createModelMergerResults, doesModelMergerResultsHaveChanges, PluginUtils, } from '@baseplate-dev/project-builder-lib';
import { ModelMergerResultAlert, useBlockUnsavedChangesNavigate, useDefinitionSchema, useProjectDefinition, useResettableForm, } from '@baseplate-dev/project-builder-lib/web';
import { FormActionBar, SectionList, SectionListSection, SectionListSectionContent, SectionListSectionDescription, SectionListSectionHeader, SectionListSectionTitle, } from '@baseplate-dev/ui-components';
import { zodResolver } from '@hookform/resolvers/zod';
import { useMemo } from 'react';
import { getAuthPluginDefinition } from '#src/auth/utils/get-auth-plugin-definition.js';
import { AUTH0_MODELS } from '#src/auth0/constants/model-names.js';
import { AuthConfigTabs } from '#src/common/components/auth-config-tabs.js';
import { createAuth0Models } from '../schema/models.js';
import { createAuth0PluginDefinitionSchema } from '../schema/plugin-definition.js';
import '#src/styles.css';
export 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: zodResolver(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 (_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: [_jsx("div", { className: "auth:pb-16", children: _jsx(SectionList, { children: _jsxs(SectionListSection, { children: [_jsxs(SectionListSectionHeader, { children: [_jsx(SectionListSectionTitle, { children: "Auth0 Configuration" }), _jsx(SectionListSectionDescription, { children: "The plugin will automatically configure the models it needs." })] }), _jsx(SectionListSectionContent, { className: "auth:space-y-6", children: _jsx(ModelMergerResultAlert, { pendingModelChanges: pendingModelChanges }) })] }) }) }), _jsx(FormActionBar, { form: form, allowSaveWithoutDirty: !pluginMetadata ||
                                doesModelMergerResultsHaveChanges(pendingModelChanges) })] }) })] }));
}
//# sourceMappingURL=auth0-definition-editor.js.map