import _extends from "@babel/runtime/helpers/esm/extends";
import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
var _dec, _class, _Token;
import React from 'react';
import { locale } from "../../../lib/locale/decorators";
import { emptyHandler } from "../../../lib/utils";
import { ThemeContext } from "../../../lib/theming/ThemeContext";
import { CommonWrapper } from "../../../internal/CommonWrapper";
import { cx } from "../../../lib/theming/Emotion";
import { rootNode } from "../../../lib/rootNode";
import { CloseButtonIcon } from "../../../internal/CloseButtonIcon/CloseButtonIcon";
import { reactGetTextContent } from "../../../lib/reactGetTextContent";
import { getVisualStateDataAttributes } from "../../../internal/CommonWrapper/utils/getVisualStateDataAttributes";
import { styles } from "../Token.styles";
import { TokenLocaleHelper } from "../locale";
import { TokenView } from "../TokenView";
export var TokenDataTids = {
  root: 'Token__root',
  removeIcon: 'Token__removeIcon'
};

/** Токены — это значение заданного формата внутри поля ввода. Например, номера телефонов, почты, ИНН, СНИЛС, статусы, группы и другие.
 *
 * Используется ак значение внутри [TokenInput](https://tech.skbkontur.ru/kontur-ui/?path=/docs/react-ui_input-data-tokeninput-tokeninput--docs).
 */
export var Token = (_dec = locale('Token', TokenLocaleHelper), rootNode(_class = _dec(_class = (_Token = /*#__PURE__*/function (_React$Component) {
  function Token() {
    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.onRemoveClick = function (event) {
      var _this$props = _this.props,
        disabled = _this$props.disabled,
        _this$props$onRemove = _this$props.onRemove,
        onRemove = _this$props$onRemove === void 0 ? emptyHandler : _this$props$onRemove;
      if (disabled) {
        event.preventDefault();
        return;
      }
      onRemove(event);
    };
    return _this;
  }
  _inheritsLoose(Token, _React$Component);
  var _proto = Token.prototype;
  _proto.render = function render() {
    var _this2 = this;
    return /*#__PURE__*/React.createElement(ThemeContext.Consumer, null, function (theme) {
      _this2.theme = theme;
      return _this2.renderMain();
    });
  };
  _proto.renderMain = function renderMain() {
    var _this$props2 = this.props,
      size = _this$props2.size,
      children = _this$props2.children,
      isActive = _this$props2.isActive,
      error = _this$props2.error,
      warning = _this$props2.warning,
      disabled = _this$props2.disabled,
      ariaDescribedby = _this$props2['aria-describedby'],
      _this$props2$onClick = _this$props2.onClick,
      onClick = _this$props2$onClick === void 0 ? emptyHandler : _this$props2$onClick,
      _this$props2$onDouble = _this$props2.onDoubleClick,
      onDoubleClick = _this$props2$onDouble === void 0 ? emptyHandler : _this$props2$onDouble,
      _this$props2$onMouseE = _this$props2.onMouseEnter,
      onMouseEnter = _this$props2$onMouseE === void 0 ? emptyHandler : _this$props2$onMouseE,
      _this$props2$onMouseL = _this$props2.onMouseLeave,
      onMouseLeave = _this$props2$onMouseL === void 0 ? emptyHandler : _this$props2$onMouseL,
      _this$props2$onFocus = _this$props2.onFocus,
      onFocus = _this$props2$onFocus === void 0 ? emptyHandler : _this$props2$onFocus,
      _this$props2$onBlur = _this$props2.onBlur,
      onBlur = _this$props2$onBlur === void 0 ? emptyHandler : _this$props2$onBlur;
    var theme = this.theme;
    var removeButtonAriaLabel = this.locale.removeButtonAriaLabel + ' ' + reactGetTextContent(children);
    var icon = /*#__PURE__*/React.createElement(CloseButtonIcon, {
      side: 16,
      color: "inherit",
      colorHover: "inherit",
      "aria-label": removeButtonAriaLabel,
      tabbable: false
    });
    var classNames = cx(styles.tokenIdle(theme), !isActive && !warning && !error && !disabled && styles.tokenHover(theme), isActive && styles.tokenActive(theme), warning && styles.tokenWarning(theme), error && styles.tokenError(theme), disabled && styles.tokenDisabled(theme));
    var textholder = /*#__PURE__*/React.createElement("span", {
      className: styles.text()
    }, children);
    var closeButton = /*#__PURE__*/React.createElement("span", {
      onClick: this.onRemoveClick,
      "data-tid": TokenDataTids.removeIcon
    }, icon);
    return /*#__PURE__*/React.createElement(CommonWrapper, _extends({
      rootNodeRef: this.setRootNode
    }, this.props, getVisualStateDataAttributes({
      disabled: disabled
    })), /*#__PURE__*/React.createElement(TokenView, {
      className: classNames,
      size: size,
      closeButton: closeButton,
      "data-tid": TokenDataTids.root,
      "aria-describedby": ariaDescribedby,
      onClick: onClick,
      onDoubleClick: onDoubleClick,
      onMouseEnter: onMouseEnter,
      onMouseLeave: onMouseLeave,
      onFocus: onFocus,
      onBlur: onBlur
    }, textholder));
  };
  return Token;
}(React.Component), _Token.__KONTUR_REACT_UI__ = 'Token', _Token.displayName = 'Token', _Token)) || _class) || _class);