"use strict";
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
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 (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Link = void 0;
var react_1 = __importDefault(require("react"));
var bem_react_classname_1 = require("bem-react-classname");
var cn_1 = require("../cn");
/**
 * Компонент ссылки.
 */
var Link = /** @class */ (function (_super) {
    __extends(Link, _super);
    function Link() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.cn = bem_react_classname_1.createCn('link');
        _this.state = {
            hovered: false,
            focused: false,
        };
        _this.handleClick = function (event) {
            if (_this.props.pseudo) {
                event.preventDefault();
            }
            if (!_this.props.disabled && _this.props.onClick) {
                _this.props.onClick(event);
            }
            if (_this.props.disabled && _this.props.onDisabledClick) {
                _this.props.onDisabledClick(event);
            }
        };
        _this.handleFocus = function (event) {
            _this.setState({ focused: true });
            if (_this.props.onFocus) {
                _this.props.onFocus(event);
            }
        };
        _this.handleBlur = function (event) {
            _this.setState({ focused: false });
            if (_this.props.onBlur) {
                _this.props.onBlur(event);
            }
        };
        _this.handleMouseEnter = function (event) {
            _this.setState({ hovered: true });
            if (_this.props.onMouseEnter) {
                _this.props.onMouseEnter(event);
            }
        };
        _this.handleMouseLeave = function (event) {
            _this.setState({ hovered: false });
            if (_this.props.onMouseLeave) {
                _this.props.onMouseLeave(event);
            }
        };
        return _this;
    }
    Link.prototype.render = function () {
        var _this = this;
        var linkElement = this.props.checked || this.props.disabled ? 'span' : 'a';
        var iconPosition = this.props.iconPosition;
        var linkProps = {
            ref: function (root) {
                _this.root = root;
            },
            download: this.props.download,
            className: this.cn({
                disabled: this.props.disabled,
                checked: this.props.checked,
                pseudo: this.props.pseudo,
                view: this.props.view,
                size: this.props.size,
                focused: this.state.focused,
                hovered: this.state.hovered,
                flex: this.props.icon && iconPosition === 'left',
            }),
            id: this.props.id,
            tabIndex: this.props.tabIndex,
            onClick: this.handleClick,
            onFocus: this.handleFocus,
            onBlur: this.handleBlur,
            onMouseEnter: this.handleMouseEnter,
            onMouseLeave: this.handleMouseLeave,
        };
        if (this.props.target === '_blank') {
            linkProps.rel = 'noreferrer noopener';
        }
        if (!this.props.checked && !this.props.disabled) {
            linkProps.href = this.props.url;
            linkProps.target = this.props.target;
        }
        var linkContent = [this.props.children];
        var iconTemplate = this.props.icon && (react_1.default.createElement("span", { key: "icon", className: this.cn('icon', { left: iconPosition === 'left' }) }, this.props.icon));
        var textTemplate = this.props.text && (react_1.default.createElement("span", { key: "text", className: this.cn('text-container') },
            react_1.default.createElement("span", { className: this.cn('text') }, this.props.text)));
        if (iconPosition === 'left') {
            linkContent.push(iconTemplate, textTemplate);
        }
        if (iconPosition === 'right') {
            linkContent.push(textTemplate, iconTemplate);
        }
        return react_1.default.createElement(linkElement, __assign(__assign({}, linkProps), { 'data-test-id': this.props['data-test-id'] }), linkContent);
    };
    /**
     * Возвращает корневой `HTMLElement` компонента.
     */
    Link.prototype.getNode = function () {
        return this.root;
    };
    /**
     * Ставит фокус на ссылку.
     */
    Link.prototype.focus = function () {
        this.root.focus();
    };
    /**
     * Убирает фокус с ссылки.
     */
    // eslint-disable-next-line class-methods-use-this
    Link.prototype.blur = function () {
        if (document.activeElement instanceof HTMLElement) {
            document.activeElement.blur();
        }
    };
    Link.defaultProps = {
        iconPosition: 'left',
        size: 'm',
        url: '#',
        tabIndex: 0,
        disabled: false,
        checked: false,
        pseudo: false,
    };
    return Link;
}(react_1.default.PureComponent));
exports.Link = Link;
exports.default = cn_1.withTheme(Link);

//# sourceMappingURL=link.js.map
