import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { createAndApplyModelMergerResults, createModelMergerResults, doesModelMergerResultsHaveChanges, FeatureUtils, PluginUtils, } from '@baseplate-dev/project-builder-lib';
import { FeatureComboboxFieldController, 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 { STORAGE_MODELS } from '#src/storage/constants/model-names.js';
import { createStorageModels } from '../schema/models.js';
import { createStoragePluginDefinitionSchema } from '../schema/plugin-definition.js';
import AdapterEditorForm from './adapter-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 pendingModelChanges = useMemo(() => {
        const desiredModels = createStorageModels({ storageFeatureRef }, definitionContainer);
        return createModelMergerResults(STORAGE_MODELS, desiredModels, definitionContainer);
    }, [definitionContainer, storageFeatureRef]);
    const onSubmit = handleSubmit((data) => saveDefinitionWithFeedback((draftConfig) => {
        const featureRef = FeatureUtils.ensureFeatureByNameRecursively(draftConfig, data.storageFeatureRef);
        const updatedData = {
            ...data,
            storageFeatureRef: featureRef,
        };
        createAndApplyModelMergerResults(draftConfig, STORAGE_MODELS, createStorageModels(updatedData, definitionContainer), definitionContainer);
        PluginUtils.setPluginConfig(draftConfig, metadata, updatedData, 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(ModelMergerResultAlert, { pendingModelChanges: pendingModelChanges }), _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(FormActionBar, { form: form, allowSaveWithoutDirty: !pluginMetadata ||
                    doesModelMergerResultsHaveChanges(pendingModelChanges) })] }));
}
//# sourceMappingURL=storage-definition-editor.js.map