"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "useDrawer", {
    enumerable: true,
    get: function() {
        return useDrawer;
    }
});
var _react = require("react");
var _DrawerContext = require("../DrawerContext");
var _Popup = require("../../Popup");
var _utils = require("../../../utils");
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;
}
var ESCAPE_KEYCODE = 27;
var useDrawer = function useDrawer(param) {
    var id = param.id, isOpen = param.isOpen, popupInfo = param.popupInfo, onEscKeyDown = param.onEscKeyDown, onClose = param.onClose, _param_closeOnEsc = param.closeOnEsc, closeOnEsc = _param_closeOnEsc === void 0 ? true : _param_closeOnEsc, _param_disableScroll = param.disableScroll, disableScroll = _param_disableScroll === void 0 ? true : _param_disableScroll;
    var popupController = (0, _Popup.usePopupContext)();
    var overflow = (0, _react.useRef)(_utils.canUseDOM ? document.body.style.overflow : 'initial');
    // При ESC закрывает текущее окно, если это возможно
    var onKeyDown = (0, _react.useCallback)(function(event) {
        if (!closeOnEsc) {
            return;
        }
        if (event.keyCode === ESCAPE_KEYCODE && (0, _DrawerContext.getIdLastDrawer)(Array.from(popupController.items.values())) === id) {
            if (onEscKeyDown) {
                onEscKeyDown(event);
                return;
            }
            if (onClose) {
                onClose();
            }
        }
    }, [
        onClose,
        onEscKeyDown,
        popupController.items,
        closeOnEsc
    ]);
    (0, _react.useEffect)(function() {
        window.addEventListener('keydown', onKeyDown);
        return function() {
            window.removeEventListener('keydown', onKeyDown);
        };
    }, [
        onKeyDown
    ]);
    // linaria не поддерживает динамический global
    (0, _react.useEffect)(function() {
        if (!_utils.canUseDOM) {
            return;
        }
        if (isOpen && disableScroll) {
            overflow.current = document.body.style.overflow;
            document.body.style.overflow = 'hidden';
        }
    }, [
        isOpen,
        disableScroll
    ]);
    (0, _react.useEffect)(function() {
        if (!_utils.canUseDOM) {
            return;
        }
        if (!isOpen && !(0, _DrawerContext.hasDrawers)(Array.from(popupController.items.values()))) {
            document.body.style.overflow = overflow.current;
        }
    }, [
        isOpen,
        popupController.items
    ]);
    // При анмаунте компонента нужно обновлять overflow у body.
    (0, _react.useEffect)(function() {
        return function() {
            if (!_utils.canUseDOM) {
                return;
            }
            if (!(0, _DrawerContext.hasDrawers)(Array.from(popupController.items.values()))) {
                document.body.style.overflow = overflow.current;
            }
        };
    }, []);
    var drawerInfo = _object_spread({
        id: id,
        info: {
            isDrawer: true
        }
    }, popupInfo);
    return {
        drawerInfo: drawerInfo
    };
};
