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

import { jsx as _jsx } from "react/jsx-runtime";
import { Children, isValidElement, useRef, } from 'react';
import { Modal } from '../Modal/index.js';
import { DialogContext } from './context.js';
/**
 * A DialogContainer accepts a single Dialog as a child, and manages to show and hide
 * it in a modal.
 * Useful in cases where there is no trigger element
 * or when the trigger unmounts while the dialog is open.
 */
export function DialogContainer(props) {
    const { children, type = 'modal', onDismiss, isDismissable = true, isKeyboardDismissDisabled, isOpen, hideOnClose, } = props;
    const childArray = Children.toArray(children);
    if (childArray.length > 1) {
        throw new Error('Only a single child can be passed to DialogContainer.');
    }
    const lastChild = useRef(null);
    const child = isValidElement(childArray[0]) ? childArray[0] : null;
    if (child) {
        lastChild.current = child;
    }
    const isActuallyOpened = typeof isOpen !== 'boolean' ? !!child : isOpen;
    const context = {
        type,
        onClose: onDismiss,
        isDismissable,
        isOpen,
    };
    return (_jsx(Modal, { isOpen: isActuallyOpened, type: type, hideOnClose: hideOnClose, isDismissable: isDismissable, isKeyboardDismissDisabled: isKeyboardDismissDisabled, onClose: isDismissable ? onDismiss : undefined, children: _jsx(DialogContext.Provider, { value: context, children: child }) }));
}

        