"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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Overlay = void 0;
const hooks_1 = require("../../../hooks/index.js");
const classnames_1 = __importDefault(require("classnames"));
const react_1 = __importStar(require("react"));
const react_popper_1 = require("react-popper");
const Fade_1 = __importDefault(require("../../../animations/Fade"));
const Portal_1 = require("../Portal/Portal");
const Overlay_module_scss_1 = __importDefault(require("./Overlay.module.scss"));
const Overlay = (props) => {
    var _a, _b, _c;
    const [referenceElement, setReferenceElement] = (0, react_1.useState)(null);
    const [popperElement, setPopperElement] = (0, react_1.useState)(null);
    const [isOpen, setIsOpen] = (0, react_1.useState)(props.open);
    const { onBodyClickExcept } = (0, hooks_1.useBodyClick)();
    // Used to prevent premature closing of window before it gets a chance to open
    (0, react_1.useEffect)(() => {
        let timeout;
        let cancelled = false;
        if (props.open) {
            setIsOpen(props.open);
        }
        else {
            // Allow for fade out animation to run before removing element
            timeout = setTimeout(() => {
                if (!cancelled) {
                    setIsOpen(false);
                }
            }, 500);
        }
        return () => {
            cancelled = true;
            clearTimeout(timeout);
        };
    }, [props.open]);
    (0, react_1.useLayoutEffect)(() => {
        const { remove } = onBodyClickExcept((e) => {
            var _a;
            if (isOpen) {
                e.preventDefault();
                e.stopPropagation();
                (_a = props.onClose) === null || _a === void 0 ? void 0 : _a.call(props, e);
            }
        }, popperElement);
        return () => {
            remove();
        };
    }, [isOpen, popperElement]);
    (0, react_1.useEffect)(() => {
        if (props.strategy === 'fixed' && props.open && props.preventScroll) {
            document.body.style.setProperty('overflow', 'hidden');
        }
        return () => {
            if (props.strategy === 'fixed' && props.open && props.preventScroll) {
                document.body.style.removeProperty('overflow');
            }
        };
    }, [props.open, props.preventScroll, props.strategy]);
    const modifiers = [
        {
            name: 'flip',
            options: {
                flipVariations: false,
                fallbackPlacements: props.fallbackPlacements,
            },
        },
        {
            name: 'preventOverflow',
            enabled: true,
            options: {
                altAxis: false,
                altBoundary: false,
                tether: false,
                rootBoundary: 'body',
                padding: 8,
            },
        },
    ];
    if (props.offset) {
        modifiers.push({
            name: 'offset',
            options: {
                offset: props.offset,
            },
        });
    }
    const { styles: popperStyles, attributes } = (0, react_popper_1.usePopper)((_a = props.followElement) !== null && _a !== void 0 ? _a : referenceElement, popperElement, {
        placement: (_b = props.placement) !== null && _b !== void 0 ? _b : 'bottom',
        strategy: (_c = props.strategy) !== null && _c !== void 0 ? _c : 'absolute',
        modifiers,
    });
    const overlayClasses = (0, classnames_1.default)(props.className, {
        [Overlay_module_scss_1.default['rs-overlay']]: !props.hideStyles,
    });
    const overlayElement = (
    // Here we depend on the props.open as that's the "intended" state. Fade animation will handle it going in and out.
    react_1.default.createElement(react_1.default.Fragment, null,
        !props.followElement && react_1.default.createElement("span", { ref: setReferenceElement }),
        react_1.default.createElement("div", Object.assign({ ref: setPopperElement, style: popperStyles.popper }, attributes.popper, { className: (0, classnames_1.default)(Overlay_module_scss_1.default['rs-popper-container'], props.containerClassName, { [Overlay_module_scss_1.default['--above-nav']]: props.isAboveNav }) }),
            react_1.default.createElement(Fade_1.default, { open: props.open },
                react_1.default.createElement("div", { ref: props.overlayRef, className: overlayClasses }, props.children)))));
    if (!isOpen) {
        return react_1.default.createElement(react_1.default.Fragment, null);
    }
    if (props.usePortal || props.container) {
        // PreventZIndex is only enabled if container is not specified, since modal will be appended to body, Roomstay engine by default will overlay it
        return (react_1.default.createElement(Portal_1.RoomstayPortal, { target: props.container || document.body, preventZIndex: !!props.container }, overlayElement));
    }
    return overlayElement;
};
exports.Overlay = Overlay;
//# sourceMappingURL=Overlay.js.map