"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "ModalBase", {
    enumerable: true,
    get: function() {
        return ModalBase;
    }
});
var _react = /*#__PURE__*/ _interop_require_default(require("react"));
var _styledcomponents = require("styled-components");
var _PopupBase = require("../PopupBase/PopupBase");
var _hooks = require("../../hooks");
var _ModalOverlay = require("./ModalOverlay");
var _hooks1 = require("./hooks");
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
var NoScroll = (0, _styledcomponents.createGlobalStyle)([
    "body{overflow-y:hidden;}"
]);
var ModalBase = function ModalBase(_0) {
    var id = _0.id, withAnimation = _0.withAnimation, onClose = _0.onClose, onOverlayClick = _0.onOverlayClick, onEscKeyDown = _0.onEscKeyDown, _0_closeOnEsc = _0.closeOnEsc, closeOnEsc = _0_closeOnEsc === void 0 ? true : _0_closeOnEsc, _0_closeOnOverlayClick = _0.closeOnOverlayClick, closeOnOverlayClick = _0_closeOnOverlayClick === void 0 ? true : _0_closeOnOverlayClick, withBlur = _0.withBlur, initialFocusRef = _0.initialFocusRef, focusAfterRef = _0.focusAfterRef, zIndex = _0.zIndex, popupInfo = _0.popupInfo, children = _0.children, rest = _object_without_properties(_0, [
        "id",
        "withAnimation",
        "onClose",
        "onOverlayClick",
        "onEscKeyDown",
        "closeOnEsc",
        "closeOnOverlayClick",
        "withBlur",
        "initialFocusRef",
        "focusAfterRef",
        "zIndex",
        "popupInfo",
        "children"
    ]);
    var trapRef = (0, _hooks.useFocusTrap)(true, initialFocusRef, focusAfterRef);
    var uniqId = (0, _hooks.useUniqId)();
    var innerId = id || uniqId;
    var modalInfo = (0, _hooks1.useModal)({
        id: innerId,
        closeOnEsc: closeOnEsc,
        onEscKeyDown: onEscKeyDown,
        onClose: onClose,
        popupInfo: popupInfo
    }).modalInfo;
    return /*#__PURE__*/ _react.default.createElement(_PopupBase.PopupBase, _object_spread({
        id: id,
        ref: trapRef,
        popupInfo: modalInfo,
        withAnimation: withAnimation,
        zIndex: zIndex,
        overlay: /*#__PURE__*/ _react.default.createElement(_ModalOverlay.ModalOverlay, {
            id: innerId,
            withBlur: withBlur,
            onOverlayClick: onOverlayClick,
            onClose: onClose,
            zIndex: zIndex,
            closeOnOverlayClick: closeOnOverlayClick
        })
    }, rest), /*#__PURE__*/ _react.default.createElement(NoScroll, null), children);
};
