"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.LinkDataTids = exports.Link = void 0;var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));var _react = _interopRequireDefault(require("react"));
var _globalObject = require("@skbkontur/global-object");


var _Mixins = require("../../lib/styles/Mixins");

var _keyListener = require("../../lib/events/keyListener");

var _ThemeContext = require("../../lib/theming/ThemeContext");
var _utils = require("../../lib/utils");

var _CommonWrapper = require("../../internal/CommonWrapper");
var _Emotion = require("../../lib/theming/Emotion");

var _rootNode = require("../../lib/rootNode");

var _createPropsGetter = require("../../lib/createPropsGetter");
var _ThemeFactory = require("../../lib/theming/ThemeFactory");
var _getVisualStateDataAttributes = require("../../internal/CommonWrapper/utils/getVisualStateDataAttributes");

var _Link2 = require("./Link.styles");
var _LinkIcon = require("./LinkIcon");var _excluded = ["disabled", "icon", "rightIcon", "use", "loading", "_button", "_buttonOpened", "component", "focused", "error", "warning", "tabIndex", "theme"];var _class, _Link;








































var LINK_DEFAULT_COMPONENT = 'a';









var LinkDataTids = exports.LinkDataTids = {
  root: 'Link__root'
};




/**
 * С помощью ссылки пользователь может перейти на другую страницу, раздел приложения или внешний URL.
 */var

Link = exports.Link = (0, _rootNode.rootNode)(_class = (_Link = /*#__PURE__*/function (_React$Component) {function Link() {var _this;for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {args[_key] = arguments[_key];}_this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;_this.











    getProps = (0, _createPropsGetter.createPropsGetter)(Link.defaultProps);_this.

    state = {
      focusedByTab: false
    };_this.




















    getTabIndex = function (_ref)





    {var nonInteractive = _ref.nonInteractive,_ref$tabIndex = _ref.tabIndex,tabIndex = _ref$tabIndex === void 0 ? 0 : _ref$tabIndex;
      return nonInteractive ? -1 : tabIndex;
    };_this.

    getRel = function () {
      if (isAnchorProps(_this.props)) {
        var _this$props = _this.props,rel = _this$props.rel,href = _this$props.href;
        if (!rel && href) {
          return "noopener" + ((0, _utils.isExternalLink)(href) ? ' noreferrer' : '');
        }
        return rel;
      }

      return undefined;
    };_this.

    renderMain = function (props) {var _cx;
      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,_props$focused = props.focused,focused = _props$focused === void 0 ? false : _props$focused,error = props.error,warning = props.warning,tabIndex = props.tabIndex,theme = props.theme,rest = (0, _objectWithoutPropertiesLoose2.default)(props, _excluded);

      var arrow = null;
      if (_button) {
        arrow = /*#__PURE__*/_react.default.createElement("span", { className: _Link2.styles.arrow() });
      }

      var isFocused = !disabled && (_this.state.focusedByTab || focused);

      var leftIconElement = icon && /*#__PURE__*/_react.default.createElement(_LinkIcon.LinkIcon, { icon: icon, loading: loading, position: "left" });
      var rightIconElement = rightIcon && /*#__PURE__*/
      _react.default.createElement(_LinkIcon.LinkIcon, { hasBothIcons: !!icon && !!rightIcon, icon: rightIcon, loading: loading, position: "right" });

      var nonInteractive = disabled || loading;
      var getUseStyles = function getUseStyles() {
        switch (use) {
          case 'default':
            return _Link2.styles.default(_this.theme);
          case 'danger':
            return _Link2.styles.danger(_this.theme);
          case 'success':
            return _Link2.styles.success(_this.theme);
          case 'grayed':
            return _Link2.styles.grayed(_this.theme);
        }
      };
      var getUseLineFocusStyles = function getUseLineFocusStyles() {
        switch (use) {
          case 'default':
            return _Link2.styles.lineFocus(_this.theme);
          case 'danger':
            return _Link2.styles.lineFocusDanger(_this.theme);
          case 'success':
            return _Link2.styles.lineFocusSuccess(_this.theme);
          case 'grayed':
            return _Link2.styles.lineFocusGrayed(_this.theme);
        }
      };

      var rootProps = (0, _extends2.default)({},
      rest, {
        className: (0, _Emotion.cx)((_cx = {}, _cx[
        _Link2.styles.root(_this.theme)] = true, _cx[
        (0, _Mixins.resetButton)()] = Root === 'button', _cx[
        _Link2.styles.focus(_this.theme)] = isFocused, _cx[
        _Link2.styles.disabled(_this.theme)] = disabled || loading, _cx[
        getUseStyles()] = true, _cx[
        _Link2.styles.useGrayedFocus(_this.theme)] = use === 'grayed' && focused, _cx[
        _Link2.styles.button(_this.theme)] = !!_button, _cx[
        _Link2.styles.buttonOpened(_this.theme)] = !!_buttonOpened, _cx[
        _Link2.styles.warning(_this.theme)] = warning, _cx[
        _Link2.styles.error(_this.theme)] = error, _cx[
        getUseLineFocusStyles()] = isFocused, _cx)
        ),
        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 /*#__PURE__*/(
        _react.default.createElement(Root, (0, _extends2.default)({
          "data-tid": LinkDataTids.root },
        rootProps,
        buttonOnlyProps,
        (0, _getVisualStateDataAttributes.getVisualStateDataAttributes)({ disabled: disabled })),

        leftIconElement,
        _this.props.children,
        rightIconElement,
        arrow
        ));

    };_this.

    handleFocus = function () {
      if (!_this.props.disabled) {
        // focus event fires before keyDown eventlistener
        // so we should check tabPressed in async way
        _globalObject.globalObject.requestAnimationFrame == null || _globalObject.globalObject.requestAnimationFrame(function () {
          if (_keyListener.keyListener.isTabPressed) {
            _this.setState({ focusedByTab: true });
          }
        });
      }
    };_this.

    handleBlur = function () {
      _this.setState({ focusedByTab: false });
    };_this.

    handleClick = function (event) {
      var _this$props2 = _this.props,onClick = _this$props2.onClick,disabled = _this$props2.disabled,loading = _this$props2.loading;

      if (disabled) {
        event.preventDefault();
        event.stopPropagation();
      }

      if (onClick && !disabled && !loading) {
        onClick(event);
      }
    };return _this;}(0, _inheritsLoose2.default)(Link, _React$Component);var _proto = Link.prototype;_proto.render = function render() {var _this2 = this;return /*#__PURE__*/_react.default.createElement(_ThemeContext.ThemeContext.Consumer, null, function (theme) {_this2.theme = _this2.props.theme ? _ThemeFactory.ThemeFactory.create(_this2.props.theme, theme) : theme;return /*#__PURE__*/_react.default.createElement(_CommonWrapper.CommonWrapper, (0, _extends2.default)({ rootNodeRef: _this2.setRootNode }, _this2.getProps()), _this2.renderMain);});};return Link;}(_react.default.Component), _Link.__KONTUR_REACT_UI__ = 'Link', _Link.displayName = 'Link', _Link.defaultProps = { use: 'default', component: LINK_DEFAULT_COMPONENT }, _Link)) || _class;


var isAnchorProps = function isAnchorProps(props) {
  return props.component === 'a';
};