"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Modal = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const box_1 = __importDefault(require("../box"));
const core_1 = require("../core");
const focusLock_1 = __importDefault(require("../focusLock"));
const portal_1 = __importDefault(require("../portal"));
const utils_1 = require("../utils");
const context_1 = require("./context");
const modalBackdrop_1 = __importDefault(require("./modalBackdrop"));
const modalContent_1 = __importDefault(require("./modalContent"));
const modalManager_1 = require("./modalManager");
/**
 * Displays provided content on top of a dark backdrop element with fixed position.
 * By default uses Portal for content and closes when clicking backdrop or pressing 'Escape'.
 * Uses focus and scroll lock to achieve proper accessibility.
 * More about accessibility: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/dialog_role
 */
exports.Modal = (0, core_1.vui)((props, ref) => {
    var _a, _b;
    const { allowPinchZoom, animation = 'fadeDown', children, className, contentProps, disableAutoFocus, disableBackdropClose, disableEscClose, disablePortal, disableReturnFocus, disableScrollLock, disableTrapFocus, finalFocusRef, hideBackdrop, initialFocusRef, isOpen, onBackdropClick: onBackdropClickProp, onClose, onEsc, preserveScrollBar, size, variant } = props, rest = __rest(props, ["allowPinchZoom", "animation", "children", "className", "contentProps", "disableAutoFocus", "disableBackdropClose", "disableEscClose", "disablePortal", "disableReturnFocus", "disableScrollLock", "disableTrapFocus", "finalFocusRef", "hideBackdrop", "initialFocusRef", "isOpen", "onBackdropClick", "onClose", "onEsc", "preserveScrollBar", "size", "variant"]);
    const modalRef = (0, react_1.useRef)(null);
    const mouseDownTarget = (0, react_1.useRef)(null);
    (0, modalManager_1.useModalManager)(modalRef, isOpen, disableScrollLock);
    if (!isOpen)
        return null;
    const onBackdropClick = (e) => {
        e.stopPropagation();
        // Avoid closing modal when click starts inside the content and is released outside
        if (mouseDownTarget.current !== e.target)
            return;
        if (!modalManager_1.manager.isTopModal(modalRef))
            return;
        if (!disableBackdropClose) {
            onClose === null || onClose === void 0 ? void 0 : onClose();
            onBackdropClickProp === null || onBackdropClickProp === void 0 ? void 0 : onBackdropClickProp(e);
        }
    };
    const onKeyDown = (e) => {
        if (e.key === core_1.KeyboardKeys.Escape) {
            e.stopPropagation();
            !disableEscClose && (onClose === null || onClose === void 0 ? void 0 : onClose());
            onEsc === null || onEsc === void 0 ? void 0 : onEsc();
        }
    };
    const onMouseDown = (e) => (mouseDownTarget.current = e.target);
    const childProps = {
        onClick: (e) => {
            var _a, _b;
            e.stopPropagation();
            (_b = (_a = children === null || children === void 0 ? void 0 : children.props) === null || _a === void 0 ? void 0 : _a.onClick) === null || _b === void 0 ? void 0 : _b.call(_a, e);
        },
        tabIndex: (_b = (_a = children === null || children === void 0 ? void 0 : children.props) === null || _a === void 0 ? void 0 : _a.tabIndex) !== null && _b !== void 0 ? _b : -1,
    };
    const context = { isOpen, onBackdropClick, onClose, size, variant };
    return ((0, jsx_runtime_1.jsx)(context_1.ModalProvider, { value: context, children: (0, jsx_runtime_1.jsx)(portal_1.default, { disablePortal: disablePortal, children: (0, jsx_runtime_1.jsxs)(box_1.default, Object.assign({ className: (0, utils_1.cs)('vui-modal', className), ref: (0, utils_1.mergeRefs)(ref, modalRef) }, rest, { onKeyDown: (0, utils_1.callAll)(props.onKeyDown, onKeyDown), onMouseDown: (0, utils_1.callAll)(props.onMouseDown, onMouseDown), children: [!hideBackdrop && (0, jsx_runtime_1.jsx)(modalBackdrop_1.default, {}), (0, jsx_runtime_1.jsx)(focusLock_1.default, { autoFocus: !disableAutoFocus, finalFocusRef: finalFocusRef, initialFocusRef: initialFocusRef, isDisabled: disableTrapFocus, returnFocus: !disableReturnFocus, children: (0, jsx_runtime_1.jsx)(modalContent_1.default, Object.assign({ animation: animation }, contentProps, { children: (0, react_1.cloneElement)(children, childProps) })) })] })) }) }));
});
exports.Modal.Backdrop = modalBackdrop_1.default;
exports.Modal.Content = modalContent_1.default;
exports.Modal.displayName = 'Modal';
exports.default = exports.Modal;
//# sourceMappingURL=modal.js.map