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/index.js';
import { AuthConfigTabs } from '#src/common/components/auth-config-tabs.js';
import { PLACEHOLDER_AUTH_MODELS } from '#src/placeholder-auth/constants/model-names.js';
import { createAuthModels } from '../schema/models.js';
import { createPlaceholderAuthPluginDefinitionSchema } from '../schema/plugin-definition.js';
import '#src/styles.css';
export 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: zodResolver(authPluginDefinitionSchema),
        defaultValues,
    });
    const { control, reset, handleSubmit } = form;
    const pendingModelChanges = useMemo(() => {
        const desiredModels = createAuthModels(authDefinition);
        return createModelMergerResults(PLACEHOLDER_AUTH_MODELS, desiredModels, definitionContainer);
    }, [definitionContainer, authDefinition]);
    const onSubmit = handleSubmit((data) => saveDefinitionWithFeedback((draftConfig) => {
        const updatedData = {
            ...data,
        };
        createAndApplyModelMergerResults(draftConfig, PLACEHOLDER_AUTH_MODELS, createAuthModels(authDefinition), definitionContainer);
        PluginUtils.setPluginConfig(draftConfig, metadata, updatedData, definitionContainer);
    }, {
        successMessage: 'Successfully saved placeholder auth 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: "Placeholder Auth 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=placeholder-auth-definition-editor.js.map