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 from 'react';
import { useGlobal, useEmotion, useStyles } from '../../lib/renderEnvironment/index.js';
import { useKeyListener } from '../../lib/events/keyListener.js';
import { ThemeContext } from '../../lib/theming/ThemeContext.js';
import { DEFAULT_ICON_SIZE } from '../icons2022/iconConstants.js';
import { ThemeFactory } from '../../lib/theming/ThemeFactory.js';
import { CommonWrapper } from '../CommonWrapper/index.js';
import { getStyles } from './CloseButtonIcon.styles.js';
import { CrossIcon } from './CrossIcon.js';
export var CloseButtonIcon = function (_a) {
    var _b = _a.side, side = _b === void 0 ? DEFAULT_ICON_SIZE : _b, _c = _a.size, size = _c === void 0 ? DEFAULT_ICON_SIZE : _c, color = _a.color, colorHover = _a.colorHover, _d = _a.tabbable, tabbable = _d === void 0 ? true : _d, style = _a.style, rest = __rest(_a, ["side", "size", "color", "colorHover", "tabbable", "style"]);
    var globalObject = useGlobal();
    var cx = useEmotion().cx;
    var styles = useStyles(getStyles);
    var _theme = React.useContext(ThemeContext);
    var theme = ThemeFactory.create({
        closeBtnIconColor: color !== null && color !== void 0 ? color : _theme.closeBtnIconColor,
        closeBtnIconHoverColor: colorHover !== null && colorHover !== void 0 ? colorHover : _theme.closeBtnIconHoverColor,
    }, _theme);
    var keyListener = useKeyListener();
    var _e = React.useState(false), focusedByTab = _e[0], setFocusedByTab = _e[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 () { return setFocusedByTab(false); };
    var tabIndex = !tabbable || rest.disabled ? -1 : 0;
    return (React.createElement(CommonWrapper, __assign({}, rest),
        React.createElement("button", __assign({ tabIndex: tabIndex, className: cx(styles.root(theme), !rest.disabled && focusedByTab && styles.focus(theme), rest.disabled && styles.rootDisabled(theme)), style: __assign(__assign({}, style), { width: side, height: side }), onFocus: handleFocus, onBlur: handleBlur }, rest),
            React.createElement("span", { className: styles.wrapper() },
                React.createElement(CrossIcon, { size: side < size ? side : size, focusable: tabIndex >= 0 })))));
};
CloseButtonIcon.__KONTUR_REACT_UI__ = 'CloseButtonIcon';
CloseButtonIcon.displayName = 'CloseButtonIcon';
//# sourceMappingURL=CloseButtonIcon.js.map