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 { CommonWrapper } from '../CommonWrapper/index.js';
import { getStyles } from './ClearCrossIcon.styles.js';
import { CrossIcon } from './CrossIcon.js';
export var ClearCrossIcon = function (_a) {
    var _b = _a.size, size = _b === void 0 ? 'small' : _b, style = _a.style, dataTid = _a["data-tid"], rest = __rest(_a, ["size", "style", 'data-tid']);
    var globalObject = useGlobal();
    var cx = useEmotion().cx;
    var styles = useStyles(getStyles);
    var theme = React.useContext(ThemeContext);
    var keyListener = useKeyListener();
    var getSizeClassNames = function (size) {
        switch (size) {
            case 'large':
                return { button: styles.clearCrossLarge(theme), span: styles.relativeWidthLarge(theme) };
            case 'medium':
                return { button: styles.clearCrossMedium(theme), span: styles.relativeWidthMedium(theme) };
            case 'small':
            default:
                return { button: styles.clearCrossSmall(theme), span: styles.relativeWidthSmall(theme) };
        }
    };
    var _c = React.useState(false), focusedByTab = _c[0], setFocusedByTab = _c[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); };
    return (React.createElement(CommonWrapper, __assign({}, rest),
        React.createElement("span", { className: getSizeClassNames(size).span },
            React.createElement("button", __assign({ "data-tid": dataTid, type: "button", tabIndex: -1, className: cx(styles.root(theme), 
                // Todo: use &:focus-visible on root instead styles.focus. It supported on Chrome >= 86, Firefox >= 4, Safari >= 15.4
                focusedByTab && styles.focus(theme), getSizeClassNames(size).button), style: style, onFocus: handleFocus, onBlur: handleBlur }, rest),
                React.createElement(CrossIcon, { size: size, focusable: true })))));
};
ClearCrossIcon.__KONTUR_REACT_UI__ = 'ClearCrossIcon';
ClearCrossIcon.displayName = 'ClearCrossIcon';
//# sourceMappingURL=ClearCrossIcon.js.map