"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importStar(require("react"));
const acui_1 = require("@Acme Corporation/acui");
const react_dom_1 = require("react-dom");
const Modal = (0, react_1.forwardRef)(({ isOpen, onClose, onCloseComplete, title, description, size = 'md', variant = 'default', position = 'center', isClosable = true, closeOnEsc = true, closeOnOverlayClick = true, isCentered = false, hasOverlay = true, overlayClassName = '', className = '', wrapperClassName = '', headerClassName = '', bodyClassName = '', footerClassName = '', titleClassName = '', descriptionClassName = '', closeButtonClassName = '', children, ...props }, ref) => {
    const theme = (0, acui_1.useTheme)();
    const modalRef = (0, react_1.useRef)(null);
    const [mounted, setMounted] = (0, react_1.useState)(false);
    const [isVisible, setIsVisible] = (0, react_1.useState)(false);
    const modalId = react_1.default.useId();
    const titleId = react_1.default.useId();
    const descriptionId = react_1.default.useId();
    (0, react_1.useEffect)(() => {
        setMounted(true);
        return () => setMounted(false);
    }, []);
    (0, react_1.useEffect)(() => {
        if (isOpen) {
            setIsVisible(true);
            document.body.style.overflow = 'hidden';
        }
        else {
            setIsVisible(false);
            document.body.style.overflow = '';
        }
        return () => {
            document.body.style.overflow = '';
        };
    }, [isOpen]);
    (0, react_1.useEffect)(() => {
        if (!closeOnEsc)
            return;
        const handleEsc = (event) => {
            if (event.key === 'Escape' && isOpen) {
                onClose();
            }
        };
        document.addEventListener('keydown', handleEsc);
        return () => document.removeEventListener('keydown', handleEsc);
    }, [closeOnEsc, isOpen, onClose]);
    const handleOverlayClick = (0, react_1.useCallback)((event) => {
        if (!closeOnOverlayClick)
            return;
        if (event.target === event.currentTarget) {
            onClose();
        }
    }, [closeOnOverlayClick, onClose]);
    const handleAnimationEnd = (0, react_1.useCallback)(() => {
        if (!isVisible) {
            onCloseComplete?.();
        }
    }, [isVisible, onCloseComplete]);
    const ariaProps = {
        role: 'dialog',
        'aria-modal': 'true',
        'aria-labelledby': title ? titleId : undefined,
        'aria-describedby': description ? descriptionId : undefined
    };
    const wrapperClasses = [
        'bf-modal-wrapper',
        isCentered && 'bf-modal-wrapper--centered',
        hasOverlay && 'bf-modal-wrapper--overlay',
        wrapperClassName
    ].filter(Boolean).join(' ');
    const overlayClasses = [
        'bf-modal-overlay',
        overlayClassName
    ].filter(Boolean).join(' ');
    const modalClasses = [
        'bf-modal',
        `bf-modal--${size}`,
        `bf-modal--${variant}`,
        variant === 'drawer' && `bf-modal--${position}`,
        className
    ].filter(Boolean).join(' ');
    const headerClasses = [
        'bf-modal-header',
        headerClassName
    ].filter(Boolean).join(' ');
    const bodyClasses = [
        'bf-modal-body',
        bodyClassName
    ].filter(Boolean).join(' ');
    const footerClasses = [
        'bf-modal-footer',
        footerClassName
    ].filter(Boolean).join(' ');
    const titleClasses = [
        'bf-modal-title',
        titleClassName
    ].filter(Boolean).join(' ');
    const descriptionClasses = [
        'bf-modal-description',
        descriptionClassName
    ].filter(Boolean).join(' ');
    const closeButtonClasses = [
        'bf-modal-close-button',
        closeButtonClassName
    ].filter(Boolean).join(' ');
    if (!mounted)
        return null;
    return (0, react_dom_1.createPortal)((0, jsx_runtime_1.jsxs)("div", { ref: ref, id: modalId, className: wrapperClasses, "data-visible": isVisible, onAnimationEnd: handleAnimationEnd, ...props, children: [hasOverlay && ((0, jsx_runtime_1.jsx)("div", { className: overlayClasses, onClick: handleOverlayClick, "aria-hidden": "true" })), (0, jsx_runtime_1.jsxs)("div", { ref: modalRef, ...ariaProps, className: modalClasses, tabIndex: -1, children: [title && ((0, jsx_runtime_1.jsxs)("div", { className: headerClasses, children: [(0, jsx_runtime_1.jsx)("div", { id: titleId, className: titleClasses, children: title }), isClosable && ((0, jsx_runtime_1.jsx)("button", { type: "button", className: closeButtonClasses, onClick: onClose, "aria-label": "Close modal", children: (0, jsx_runtime_1.jsxs)("svg", { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", "aria-hidden": "true", children: [(0, jsx_runtime_1.jsx)("line", { x1: "18", y1: "6", x2: "6", y2: "18" }), (0, jsx_runtime_1.jsx)("line", { x1: "6", y1: "6", x2: "18", y2: "18" })] }) }))] })), description && ((0, jsx_runtime_1.jsx)("div", { id: descriptionId, className: descriptionClasses, children: description })), (0, jsx_runtime_1.jsx)("div", { className: bodyClasses, children: children }), props.footer && ((0, jsx_runtime_1.jsx)("div", { className: footerClasses, children: props.footer }))] })] }), document.body);
});
Modal.displayName = 'Modal';
exports.default = (0, react_1.memo)(Modal);
//# sourceMappingURL=Modal.js.map