/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var keycode = require('keycode');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var keycode__default = /*#__PURE__*/_interopDefaultLegacy(keycode);

/**
 * @file ModalManager
 * @description
 * @author fex
 */
var modals = [];
function current() {
    return modals.length;
}
function currentModal() {
    return modals[modals.length - 1];
}
function addModal(modal) {
    modals.push(modal);
}
function removeModal(modal) {
    modals = modals.filter(function (el) { return el !== modal; });
}
window.addEventListener('keydown', handleWindowKeyDown);
function handleWindowKeyDown(e) {
    var code = keycode__default["default"](e);
    if (code !== 'esc') {
        return;
    }
    var modal = currentModal();
    if (!modal) {
        return;
    }
    var _a = modal.props, disabled = _a.disabled, closeOnEsc = _a.closeOnEsc;
    if (closeOnEsc && !disabled && !e.defaultPrevented) {
        modal.props.onHide(e);
    }
}

exports.addModal = addModal;
exports.current = current;
exports.currentModal = currentModal;
exports.removeModal = removeModal;
