import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { applyMergedDefinition, diffDefinition, FeatureUtils, PluginUtils, } from '@baseplate-dev/project-builder-lib';
import { DefinitionDiffAlert, FeatureComboboxFieldController, 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 { createRateLimitPartialDefinition } from '../schema/models.js';
import { createRateLimitPluginDefinitionSchema } from '../schema/plugin-definition.js';
import '#src/styles.css';
export function RateLimitDefinitionEditor({ definition: pluginMetadata, metadata, onSave, }) {
    const { definition, definitionContainer, saveDefinitionWithFeedback } = useProjectDefinition();
    const rateLimitPluginDefinitionSchema = useDefinitionSchema(createRateLimitPluginDefinitionSchema);
    const defaultValues = useMemo(() => {
        if (pluginMetadata?.config) {
            return pluginMetadata.config;
        }
        const defaultFeatureRef = FeatureUtils.getFeatureIdByNameOrDefault(definition, 'utilities');
        return {
            rateLimitFeatureRef: defaultFeatureRef,
            rateLimitOptions: {},
        };
    }, [definition, pluginMetadata?.config]);
    const form = useResettableForm({
        resolver: zodResolver(rateLimitPluginDefinitionSchema),
        defaultValues,
    });
    const { control, reset, handleSubmit } = form;
    const pluginConfig = pluginMetadata?.config;
    const diff = useMemo(() => {
        const featureRef = pluginConfig?.rateLimitFeatureRef ?? '';
        if (!featureRef)
            return undefined;
        const featureName = FeatureUtils.resolveFeatureName(definition, featureRef);
        const partialDef = createRateLimitPartialDefinition(featureName);
        return diffDefinition(definitionContainer.schema, definitionContainer.definition, partialDef);
    }, [definitionContainer, definition, pluginConfig?.rateLimitFeatureRef]);
    const onSubmit = handleSubmit((data) => saveDefinitionWithFeedback((draftConfig) => {
        const featureName = FeatureUtils.resolveFeatureName(draftConfig, data.rateLimitFeatureRef);
        const partialDef = createRateLimitPartialDefinition(featureName);
        applyMergedDefinition(definitionContainer, partialDef)(draftConfig);
        PluginUtils.setPluginConfig(draftConfig, metadata, {
            ...data,
            rateLimitFeatureRef: FeatureUtils.getFeatureIdByNameOrThrow(draftConfig, featureName),
        }, definitionContainer);
    }, {
        successMessage: 'Successfully saved Rate Limiting configuration!',
        onSuccess: () => {
            onSave();
        },
    }));
    useBlockUnsavedChangesNavigate({ control, reset, onSubmit });
    return (_jsx("div", { className: "ratelimit:relative ratelimit:flex ratelimit:h-full ratelimit:flex-1 ratelimit:flex-col ratelimit:gap-4 ratelimit:overflow-hidden", children: _jsx("div", { className: "ratelimit:mb-[--action-bar-height] ratelimit:flex ratelimit:flex-1 ratelimit:overflow-y-auto", style: {
                '--action-bar-height': '52px',
            }, children: _jsxs("form", { onSubmit: onSubmit, className: "ratelimit:max-w-6xl ratelimit:flex-1", children: [_jsx("div", { className: "ratelimit:pb-16", children: _jsxs(SectionList, { children: [_jsxs(SectionListSection, { children: [_jsxs(SectionListSectionHeader, { children: [_jsx(SectionListSectionTitle, { children: "General Settings" }), _jsx(SectionListSectionDescription, { children: "Configure the general settings for the rate limiting plugin." })] }), _jsx(SectionListSectionContent, { className: "ratelimit:space-y-6", children: _jsx(FeatureComboboxFieldController, { label: "Feature Path", name: "rateLimitFeatureRef", control: control, canCreate: true, description: "Specify the feature path where the rate limiting model will be created" }) })] }), _jsxs(SectionListSection, { children: [_jsxs(SectionListSectionHeader, { children: [_jsx(SectionListSectionTitle, { children: "Rate Limiting Models" }), _jsx(SectionListSectionDescription, { children: "The plugin will automatically configure the models it needs for rate limiting storage." })] }), _jsx(SectionListSectionContent, { className: "ratelimit:space-y-6", children: diff ? (_jsx(DefinitionDiffAlert, { diff: diff, upToDateMessage: "All required models are already configured correctly. No changes needed." })) : (_jsx("p", { className: "ratelimit:text-sm ratelimit:text-muted-foreground", children: "Save to create the RateLimiterFlexible model in your project." })) })] })] }) }), _jsx(FormActionBar, { form: form, allowSaveWithoutDirty: !pluginMetadata || (diff?.hasChanges ?? false) })] }) }) }));
}
//# sourceMappingURL=rate-limit-definition-editor.js.map