var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React, { useContext } from 'react';
import { useGlobal, useEmotion, useStyles } from '../../lib/renderEnvironment/index.js';
import { useLocaleForControl } from '../../lib/locale/useLocaleForControl.js';
import { ThemeContext } from '../../lib/theming/ThemeContext.js';
import { useKeyListener } from '../../lib/events/keyListener.js';
import { ResponsiveLayout } from '../ResponsiveLayout/index.js';
import { CommonWrapper } from '../../internal/CommonWrapper/index.js';
import { XIcon20Regular } from '../../internal/icons2022/XIcon/XIcon20Regular.js';
import { getStyles } from './Modal.styles.js';
import { ModalDataTids } from './Modal.js';
import { ModalLocaleHelper } from './locale/index.js';
export function ModalClose(_a) {
    var disableClose = _a.disableClose, requestClose = _a.requestClose, otherProps = __rest(_a, ["disableClose", "requestClose"]);
    var locale = useLocaleForControl('Modal', ModalLocaleHelper);
    var globalObject = useGlobal();
    var cx = useEmotion().cx;
    var styles = useStyles(getStyles);
    var theme = useContext(ThemeContext);
    var keyListener = useKeyListener();
    var _b = React.useState(false), focusedByTab = _b[0], setFocusedByTab = _b[1];
    var handleFocus = function () {
        var _a;
        // focus event fires before keyDown eventlistener
        // so we should check tabPressed in async way
        (_a = globalObject.requestAnimationFrame) === null || _a === void 0 ? void 0 : _a.call(globalObject, function () {
            if (keyListener.isTabPressed) {
                setFocusedByTab(true);
            }
        });
    };
    var handleBlur = function () {
        setFocusedByTab(false);
    };
    var icon = React.createElement(XIcon20Regular, { align: "none" });
    return (React.createElement(CommonWrapper, __assign({}, otherProps),
        React.createElement(ResponsiveLayout, null, function (_a) {
            var _b;
            var isMobile = _a.isMobile;
            return (React.createElement("button", { "aria-label": locale.closeButtonAriaLabel, className: cx((_b = {},
                    _b[styles.close(theme)] = true,
                    _b[styles.closeMobile(theme)] = isMobile,
                    _b[styles.disabled(theme)] = disableClose,
                    _b[styles.focus(theme)] = focusedByTab,
                    _b)), onClick: requestClose, onFocus: handleFocus, onBlur: handleBlur, "data-tid": ModalDataTids.close, tabIndex: disableClose ? -1 : 0 }, icon));
        })));
}
ModalClose.__KONTUR_REACT_UI__ = 'ModalClose';
ModalClose.displayName = 'ModalClose';
//# sourceMappingURL=ModalClose.js.map