import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { authConfigSpec } from '@baseplate-dev/project-builder-lib';
import { useDefinitionSchema, useProjectDefinition, } from '@baseplate-dev/project-builder-lib/web';
import { Button, Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, InputFieldController, MultiComboboxFieldController, SelectFieldController, SwitchFieldController, } from '@baseplate-dev/ui-components';
import { zodResolver } from '@hookform/resolvers/zod';
import { useId } from 'react';
import { useForm, useWatch } from 'react-hook-form';
import { createFileCategorySchema } from '../schema/plugin-definition.js';
export function FileCategoryDialog({ open, onOpenChange, category, isNew = false, onSave, parentControl, }) {
    const { definition, pluginContainer } = useProjectDefinition();
    const fileCategorySchema = useDefinitionSchema(createFileCategorySchema);
    const form = useForm({
        resolver: zodResolver(fileCategorySchema),
        values: category,
    });
    const { control, handleSubmit } = form;
    const onSubmit = handleSubmit((data) => {
        onSave(data);
        onOpenChange?.(false);
    });
    const formId = useId();
    // Get available auth roles
    const roleOptions = pluginContainer
        .use(authConfigSpec)
        .getAuthConfigOrThrow(definition)
        .roles.map((role) => ({
        label: role.name,
        value: role.id,
    }));
    // Get available storage adapters from parent form
    const adapters = useWatch({ control: parentControl, name: 's3Adapters' });
    const adapterOptions = adapters.map((adapter) => ({
        label: adapter.name,
        value: adapter.id,
    }));
    return (_jsx(Dialog, { open: open, onOpenChange: onOpenChange, children: _jsx(DialogContent, { children: _jsxs("form", { id: formId, onSubmit: (e) => {
                    e.stopPropagation();
                    return onSubmit(e);
                }, children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { children: isNew ? 'Add File Category' : 'Edit File Category' }), _jsx(DialogDescription, { children: isNew
                                    ? 'Enter the details for the new file category.'
                                    : 'Update the file category details below.' })] }), _jsxs("div", { className: "storage:space-y-4 storage:py-4", children: [_jsx(InputFieldController, { label: "Category Name", name: "name", control: control, placeholder: "e.g., USER_PROFILE_AVATAR", description: "Must be CONSTANT_CASE format" }), _jsx(InputFieldController, { label: "Max File Size (MB)", name: "maxFileSizeMb", control: control, type: "number", placeholder: "e.g., 10", description: "Maximum file size in megabytes", registerOptions: {
                                    valueAsNumber: true,
                                } }), _jsx(MultiComboboxFieldController, { label: "Upload Roles", name: "authorize.uploadRoles", control: control, options: roleOptions, placeholder: "Select roles that can upload...", description: "User roles authorized to upload files" }), _jsx(SelectFieldController, { label: "Storage Adapter", name: "adapterRef", control: control, options: adapterOptions, placeholder: "Select storage adapter...", description: "Where files will be stored" }), _jsx(SwitchFieldController, { label: "Disable Auto-Cleanup", name: "disableAutoCleanup", control: control, description: "When enabled, files in this category will not be automatically cleaned up" })] }), _jsxs(DialogFooter, { children: [_jsx(Button, { type: "button", variant: "outline", onClick: () => {
                                    onOpenChange?.(false);
                                }, children: "Cancel" }), _jsxs(Button, { form: formId, type: "submit", children: [isNew ? 'Add' : 'Update', " Category"] })] })] }) }) }));
}
//# sourceMappingURL=file-category-dialog.js.map