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 _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;
}
import React, { useCallback, useMemo } from 'react';
import styled, { css } from 'styled-components';
import { DEFAULT_Z_INDEX } from '../PopupBase/PopupBaseRoot';
import { usePopupBaseContext } from '../PopupBase';
import { getIdLastModal } from './ModalBaseContext';
/*
 * Класс компонента ModalBaseOverlay: `modal-base-overlay`
 */ export var modalBaseOverlayClass = 'modal-base-overlay';
// TODO: новый отдельный оверлей #778
export var Overlay = styled.div.withConfig({
    displayName: "ModalOverlay__Overlay",
    componentId: "sc-e7f59b8c-0"
})([
    "position:fixed;width:100%;height:100%;top:0;left:0;",
    " ",
    ";background-color:",
    ";backdrop-filter:var(--backdrop-filter);cursor:",
    ";"
], function(param) {
    var zIndex = param.zIndex;
    return css([
        "z-index:",
        ";"
    ], zIndex || DEFAULT_Z_INDEX);
}, function(param) {
    var $withBlur = param.$withBlur;
    return css([
        "--background-color:",
        ";--backdrop-filter:",
        ";"
    ], $withBlur ? 'var(--plasma-modal-blur-overlay-color)' : 'var(--plasma-modal-overlay-color)', $withBlur ? 'blur(1rem)' : 'none');
}, function(param) {
    var transparent = param.transparent;
    return transparent ? 'transparent' : 'var(--background-color)';
}, function(param) {
    var clickable = param.clickable;
    return clickable ? 'pointer' : 'default';
});
/**
 * ModalOverlay - подложка для ModalBase.
 */ export var ModalOverlay = function ModalOverlay(_0) {
    var id = _0.id, withBlur = _0.withBlur, onOverlayClick = _0.onOverlayClick, onClose = _0.onClose, zIndex = _0.zIndex, _0_closeOnOverlayClick = _0.closeOnOverlayClick, closeOnOverlayClick = _0_closeOnOverlayClick === void 0 ? true : _0_closeOnOverlayClick, rest = _object_without_properties(_0, [
        "id",
        "withBlur",
        "onOverlayClick",
        "onClose",
        "zIndex",
        "closeOnOverlayClick"
    ]);
    var popupController = usePopupBaseContext();
    var onModalOverlayKeyDown = useCallback(function(event) {
        if (!closeOnOverlayClick) {
            return;
        }
        if (onOverlayClick) {
            onOverlayClick(event);
            return;
        }
        if (onClose) {
            onClose();
        }
    }, [
        closeOnOverlayClick,
        onOverlayClick,
        onClose
    ]);
    var transparent = useMemo(function() {
        return getIdLastModal(popupController.items) !== id;
    }, [
        id,
        popupController.items
    ]);
    return /*#__PURE__*/ React.createElement(Overlay, _object_spread({
        className: modalBaseOverlayClass,
        transparent: transparent,
        clickable: closeOnOverlayClick,
        onClick: onModalOverlayKeyDown,
        zIndex: zIndex,
        $withBlur: withBlur
    }, rest));
};
