/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

import keycode from '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(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);
    }
}

export { addModal, current, currentModal, removeModal };
