import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { PluginUtils } from '@baseplate-dev/project-builder-lib';
import { useBlockUnsavedChangesNavigate, useDefinitionSchema, useProjectDefinition, useResettableForm, } from '@baseplate-dev/project-builder-lib/web';
import { FormActionBar, InputFieldController, SectionList, SectionListSection, SectionListSectionContent, SectionListSectionDescription, SectionListSectionHeader, SectionListSectionTitle, } from '@baseplate-dev/ui-components';
import { zodResolver } from '@hookform/resolvers/zod';
import { useMemo } from 'react';
import { QueueConfigTabs } from '#src/common/index.js';
import { createPgBossPluginDefinitionSchema } from '../schema/plugin-definition.js';
import '#src/styles.css';
export function PgBossDefinitionEditor({ definition: pluginMetadata, metadata, onSave, }) {
    const { definitionContainer, saveDefinitionWithFeedback } = useProjectDefinition();
    const pgBossPluginDefinitionSchema = useDefinitionSchema(createPgBossPluginDefinitionSchema);
    const defaultValues = useMemo(() => {
        if (pluginMetadata?.config) {
            return pluginMetadata.config;
        }
        return {
            pgBossOptions: {
                deleteAfterDays: 7,
            },
        };
    }, [pluginMetadata?.config]);
    const form = useResettableForm({
        resolver: zodResolver(pgBossPluginDefinitionSchema),
        defaultValues,
    });
    const { control, reset, handleSubmit } = form;
    const onSubmit = handleSubmit((data) => saveDefinitionWithFeedback((draftConfig) => {
        PluginUtils.setPluginConfig(draftConfig, metadata, data, definitionContainer);
    }, {
        successMessage: 'Successfully saved pg-boss configuration!',
        onSuccess: () => {
            onSave();
        },
    }));
    useBlockUnsavedChangesNavigate({ control, reset, onSubmit });
    return (_jsxs("div", { className: "queue:relative queue:flex queue:h-full queue:flex-1 queue:flex-col queue:gap-4 queue:overflow-hidden", children: [_jsx(QueueConfigTabs, {}), _jsx("div", { className: "queue:mb-[--action-bar-height] queue:flex queue:flex-1 queue:overflow-y-auto", style: {
                    '--action-bar-height': '52px',
                }, children: _jsxs("form", { onSubmit: onSubmit, className: "queue:max-w-6xl queue:flex-1", children: [_jsx("div", { className: "queue:pb-16", children: _jsx(SectionList, { children: _jsxs(SectionListSection, { children: [_jsxs(SectionListSectionHeader, { children: [_jsx(SectionListSectionTitle, { children: "Queue Behavior" }), _jsx(SectionListSectionDescription, { children: "Configure how pg-boss manages jobs and queues." })] }), _jsx(SectionListSectionContent, { className: "queue:space-y-6", children: _jsx(InputFieldController, { label: "Delete After Days", name: "pgBossOptions.deleteAfterDays", control: control, type: "number", min: 1, description: "Number of days to retain completed jobs before deletion", registerOptions: {
                                                    valueAsNumber: true,
                                                } }) })] }) }) }), _jsx(FormActionBar, { form: form, allowSaveWithoutDirty: !pluginMetadata })] }) })] }));
}
//# sourceMappingURL=pg-boss-definition-editor.js.map