/**
 * @license MIT
 * author: Cube Dev Team
 * @cube-dev/ui-kit v0.108.1
 * Released under the MIT license.
 */

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Button, ButtonGroup } from '../../actions/index.js';
import { Content } from '../../content/Content.js';
import { Header } from '../../content/Header.js';
import { Title } from '../../content/Title.js';
import { Form, SubmitButton, useForm, } from '../../form/index.js';
import { useDialogContext } from './context.js';
import { Dialog } from './Dialog.js';
/**
 * DialogForms are a specific type of Dialog. They contain forms to fill.
 */
export function DialogForm(props) {
    let { qa, name, form, defaultValues, onDismiss, onSubmit, onSubmitFailed, onValuesChange, labelStyles, labelPosition, requiredMark, necessityIndicator, necessityLabel, isReadOnly, validationState, validateTrigger, children, danger, noActions, submitProps, cancelProps, preserve, title, size, closeIcon, ...dialogProps } = props;
    [form] = useForm(form);
    const { onClose } = useDialogContext();
    function onLocalDismiss() {
        onClose?.();
        onDismiss?.();
        if (!preserve) {
            // let animations finish before resetting the form
            setTimeout(() => {
                form?.resetFields();
            }, 250);
        }
    }
    return (_jsxs(Dialog, { qa: `${qa || ''}Dialog`, size: size, closeIcon: closeIcon, ...dialogProps, children: [_jsx(Header, { children: _jsx(Title, { ellipsis: true, children: title }) }), _jsx(Content, { children: _jsxs(Form, { qa: qa || 'DialogForm', form: form, name: name, defaultValues: defaultValues, labelStyles: labelStyles, labelPosition: labelPosition, requiredMark: requiredMark, necessityIndicator: necessityIndicator, necessityLabel: necessityLabel, isReadOnly: isReadOnly, validationState: validationState, validateTrigger: validateTrigger, onSubmit: async (data) => {
                        await onSubmit?.(data);
                        onClose?.();
                        if (!preserve) {
                            // let animations finish before resetting the form
                            setTimeout(() => {
                                form.resetFields();
                            }, 250);
                        }
                    }, onSubmitFailed: onSubmitFailed, onValuesChange: onValuesChange, children: [typeof children === 'function' ? children(onLocalDismiss) : children, !noActions ? (_jsxs(ButtonGroup, { children: [_jsx(SubmitButton, { qa: `${qa || ''}SubmitButton`, theme: danger ? 'danger' : undefined, label: "Submit", ...(submitProps || {}) }), _jsx(Button, { qa: `${qa || ''}CancelButton`, label: "Cancel", onPress: onLocalDismiss, ...(cancelProps || {}) })] })) : undefined] }) })] }));
}

        