var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
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 { KeyListener } from '../../lib/events/keyListener.js';
import { ThemeContext } from '../../lib/theming/ThemeContext.js';
import { isExternalLink } from '../../lib/utils.js';
import { CommonWrapper } from '../../internal/CommonWrapper/index.js';
import { rootNode } from '../../lib/rootNode/index.js';
import { createPropsGetter } from '../../lib/createPropsGetter.js';
import { ThemeFactory } from '../../lib/theming/ThemeFactory.js';
import { getVisualStateDataAttributes } from '../../internal/CommonWrapper/utils/getVisualStateDataAttributes.js';
import { withRenderEnvironment } from '../../lib/renderEnvironment/index.js';
import { getStyles } from './Link.styles.js';
import { LinkIcon } from './LinkIcon.js';
var LINK_DEFAULT_COMPONENT = 'a';
export var LinkDataTids = {
    root: 'Link__root',
};
/**
 * С помощью ссылки пользователь может перейти на другую страницу, раздел приложения или внешний URL.
 */
var Link = /** @class */ (function (_super) {
    __extends(Link, _super);
    function Link() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.getProps = createPropsGetter(Link_1.defaultProps);
        _this.state = {
            focusedByTab: false,
        };
        _this.getTabIndex = function (_a) {
            var nonInteractive = _a.nonInteractive, _b = _a.tabIndex, tabIndex = _b === void 0 ? 0 : _b;
            return nonInteractive ? -1 : tabIndex;
        };
        _this.getRel = function () {
            if (isAnchorProps(_this.props)) {
                var _a = _this.props, rel = _a.rel, href = _a.href;
                if (!rel && href) {
                    return "noopener".concat(isExternalLink(href, _this.globalObject) ? ' noreferrer' : '');
                }
                return rel;
            }
            return undefined;
        };
        _this.renderMain = function (props) {
            var _a;
            var disabled = props.disabled, icon = props.icon, rightIcon = props.rightIcon, use = props.use, loading = props.loading, _button = props._button, _buttonOpened = props._buttonOpened, Root = props.component, _b = props.focused, focused = _b === void 0 ? false : _b, error = props.error, warning = props.warning, tabIndex = props.tabIndex, theme = props.theme, rest = __rest(props, ["disabled", "icon", "rightIcon", "use", "loading", "_button", "_buttonOpened", "component", "focused", "error", "warning", "tabIndex", "theme"]);
            var arrow = null;
            if (_button) {
                arrow = React.createElement("span", { className: _this.styles.arrow() });
            }
            var isFocused = !disabled && (_this.state.focusedByTab || focused);
            var leftIconElement = icon && React.createElement(LinkIcon, { icon: icon, loading: loading, position: "left" });
            var rightIconElement = rightIcon && (React.createElement(LinkIcon, { hasBothIcons: !!icon && !!rightIcon, icon: rightIcon, loading: loading, position: "right" }));
            var nonInteractive = disabled || loading;
            var getUseStyles = function () {
                switch (use) {
                    case 'default':
                        return _this.styles.default(_this.theme);
                    case 'danger':
                        return _this.styles.danger(_this.theme);
                    case 'success':
                        return _this.styles.success(_this.theme);
                    case 'grayed':
                        return _this.styles.grayed(_this.theme);
                }
            };
            var getUseLineFocusStyles = function () {
                switch (use) {
                    case 'default':
                        return _this.styles.lineFocus(_this.theme);
                    case 'danger':
                        return _this.styles.lineFocusDanger(_this.theme);
                    case 'success':
                        return _this.styles.lineFocusSuccess(_this.theme);
                    case 'grayed':
                        return _this.styles.lineFocusGrayed(_this.theme);
                }
            };
            var rootProps = __assign(__assign({}, rest), { className: _this.cx((_a = {},
                    _a[_this.styles.root(_this.theme)] = true,
                    _a[_this.styles.reserButton()] = Root === 'button',
                    _a[_this.styles.focus(_this.theme)] = isFocused,
                    _a[_this.styles.disabled(_this.theme)] = disabled || loading,
                    _a[getUseStyles()] = true,
                    _a[_this.styles.useGrayedFocus(_this.theme)] = use === 'grayed' && focused,
                    _a[_this.styles.button(_this.theme)] = !!_button,
                    _a[_this.styles.buttonOpened(_this.theme)] = !!_buttonOpened,
                    _a[_this.styles.warning(_this.theme)] = warning,
                    _a[_this.styles.error(_this.theme)] = error,
                    _a[getUseLineFocusStyles()] = isFocused,
                    _a)), onClick: _this.handleClick, onFocus: _this.handleFocus, onBlur: _this.handleBlur, tabIndex: _this.getTabIndex({ nonInteractive: nonInteractive, tabIndex: tabIndex }), rel: _this.getRel() });
            var buttonOnlyProps = Root === 'button' ? { disabled: nonInteractive } : {};
            return (React.createElement(Root, __assign({ "data-tid": LinkDataTids.root }, rootProps, buttonOnlyProps, getVisualStateDataAttributes({ disabled: disabled })),
                leftIconElement,
                _this.props.children,
                rightIconElement,
                arrow));
        };
        _this.handleFocus = function () {
            var _a, _b;
            if (!_this.props.disabled) {
                // focus event fires before keyDown eventlistener
                // so we should check tabPressed in async way
                (_b = (_a = _this.globalObject).requestAnimationFrame) === null || _b === void 0 ? void 0 : _b.call(_a, function () {
                    if (_this.keyListener.isTabPressed) {
                        _this.setState({ focusedByTab: true });
                    }
                });
            }
        };
        _this.handleBlur = function () {
            _this.setState({ focusedByTab: false });
        };
        _this.handleClick = function (event) {
            var _a = _this.props, onClick = _a.onClick, disabled = _a.disabled, loading = _a.loading;
            if (disabled) {
                event.preventDefault();
                event.stopPropagation();
            }
            if (onClick && !disabled && !loading) {
                onClick(event);
            }
        };
        return _this;
    }
    Link_1 = Link;
    Link.prototype.componentDidMount = function () {
        this.keyListener = new KeyListener(this.globalObject);
    };
    Link.prototype.render = function () {
        var _this = this;
        this.styles = getStyles(this.emotion);
        return (React.createElement(ThemeContext.Consumer, null, function (theme) {
            _this.theme = _this.props.theme ? ThemeFactory.create(_this.props.theme, theme) : theme;
            return (React.createElement(CommonWrapper, __assign({ rootNodeRef: _this.setRootNode }, _this.getProps()), _this.renderMain));
        }));
    };
    var Link_1;
    Link.__KONTUR_REACT_UI__ = 'Link';
    Link.displayName = 'Link';
    Link.defaultProps = {
        use: 'default',
        component: LINK_DEFAULT_COMPONENT,
    };
    Link = Link_1 = __decorate([
        withRenderEnvironment,
        rootNode
    ], Link);
    return Link;
}(React.Component));
export { Link };
var isAnchorProps = function (props) {
    return props.component === 'a';
};
//# sourceMappingURL=Link.js.map