import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { applyMergedDefinition, diffDefinition, PluginUtils, } from '@baseplate-dev/project-builder-lib';
import { DefinitionDiffAlert, 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 { createPlaceholderAuthPartialDefinition } 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 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(() => createPlaceholderAuthPartialDefinition(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 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(DefinitionDiffAlert, { diff: diff, upToDateMessage: "All required models are already configured correctly. No changes needed." }) })] }) }) }), _jsx(FormActionBar, { form: form, allowSaveWithoutDirty: !pluginMetadata || diff.hasChanges })] }) })] }));
}
//# sourceMappingURL=placeholder-auth-definition-editor.js.map