/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-f1f3d22 | Cube Dev Team */
import { _Modal } from "../Modal/Modal.js";
import { DialogContext } from "./context.js";
import { Children, isValidElement, useRef } from "react";
import { jsx } from "react/jsx-runtime";

//#region src/components/overlays/Dialog/DialogContainer.tsx
/**
* 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.
*/
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 /* @__PURE__ */ jsx(_Modal, {
		isOpen: isActuallyOpened,
		type,
		hideOnClose,
		isDismissable,
		isKeyboardDismissDisabled,
		onClose: isDismissable ? onDismiss : void 0,
		children: /* @__PURE__ */ jsx(DialogContext.Provider, {
			value: context,
			children: child
		})
	});
}

//#endregion
export { DialogContainer };
//# sourceMappingURL=DialogContainer.js.map