import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { applyMergedDefinition, authModelsSpec, 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 { createStoragePartialDefinition } from '../schema/models.js';
import { createStoragePluginDefinitionSchema } from '../schema/plugin-definition.js';
import AdapterEditorForm from './adapter-editor-form.js';
import FileCategoryEditorForm from './file-category-editor-form.js';
export function StorageDefinitionEditor({ definition: pluginMetadata, metadata, onSave, }) {
    const { definition, definitionContainer, saveDefinitionWithFeedback } = useProjectDefinition();
    const storagePluginDefinitionSchema = useDefinitionSchema(createStoragePluginDefinitionSchema);
    const defaultValues = useMemo(() => {
        if (pluginMetadata?.config) {
            return pluginMetadata.config;
        }
        return {
            storageFeatureRef: FeatureUtils.getFeatureIdByNameOrDefault(definition, 'storage'),
            s3Adapters: [],
        };
    }, [definition, pluginMetadata?.config]);
    const form = useResettableForm({
        resolver: zodResolver(storagePluginDefinitionSchema),
        defaultValues,
    });
    const { control, reset, handleSubmit, watch } = form;
    const storageFeatureRef = watch('storageFeatureRef');
    const authModels = definitionContainer.pluginStore.use(authModelsSpec);
    const userModelName = authModels.getAuthModelsOrThrow(definition).user;
    const storageFeatureName = FeatureUtils.resolveFeatureName(definition, storageFeatureRef);
    const partialDef = useMemo(() => createStoragePartialDefinition(storageFeatureName, userModelName), [storageFeatureName, userModelName]);
    const diff = useMemo(() => diffDefinition(definitionContainer.schema, definitionContainer.definition, partialDef), [definitionContainer, partialDef]);
    const onSubmit = handleSubmit((data) => saveDefinitionWithFeedback((draftConfig) => {
        const featureName = FeatureUtils.resolveFeatureName(draftConfig, data.storageFeatureRef);
        const updatedPartialDef = createStoragePartialDefinition(featureName, userModelName);
        applyMergedDefinition(definitionContainer, updatedPartialDef)(draftConfig);
        PluginUtils.setPluginConfig(draftConfig, metadata, {
            ...data,
            storageFeatureRef: FeatureUtils.getFeatureIdByNameOrThrow(draftConfig, featureName),
        }, definitionContainer);
    }, {
        successMessage: pluginMetadata
            ? 'Successfully saved storage plugin!'
            : 'Successfully enabled storage plugin!',
        onSuccess: () => {
            onSave();
        },
    }));
    useBlockUnsavedChangesNavigate({ control, reset, onSubmit });
    return (_jsxs("form", { onSubmit: onSubmit, className: "storage:mb-[--action-bar-height] storage:max-w-6xl", children: [_jsx("div", { className: "storage:pb-16", children: _jsxs(SectionList, { children: [_jsxs(SectionListSection, { children: [_jsxs(SectionListSectionHeader, { children: [_jsx(SectionListSectionTitle, { children: "Storage Configuration" }), _jsx(SectionListSectionDescription, { children: "Configure your storage settings, file models, and S3 adapters." })] }), _jsxs(SectionListSectionContent, { className: "storage:space-y-6", children: [_jsx(DefinitionDiffAlert, { diff: diff, upToDateMessage: "All required models are already configured correctly. No changes needed." }), _jsx(FeatureComboboxFieldController, { label: "Storage Feature Path", name: "storageFeatureRef", control: control, canCreate: true, description: "Specify the feature path where storage endpoints will be generated" })] })] }), _jsx(AdapterEditorForm, { control: control }), _jsx(FileCategoryEditorForm, { control: control })] }) }), _jsx(FormActionBar, { form: form, allowSaveWithoutDirty: !pluginMetadata || diff.hasChanges })] }));
}
//# sourceMappingURL=storage-definition-editor.js.map