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

var _ResizeDetector = require("../../internal/ResizeDetector");
var _identifiers = require("../../lib/events/keyboard/identifiers");
var _keyListener = require("../../lib/events/keyListener");

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


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

var _rootNode = require("../../lib/rootNode");
var _createPropsGetter = require("../../lib/createPropsGetter");
var _getVisualStateDataAttributes = require("../../internal/CommonWrapper/utils/getVisualStateDataAttributes");


var _TabsContext = require("./TabsContext");
var _Tab2 = require("./Tab.styles");var _class, _Tab;









var TabDataTids = exports.TabDataTids = {
  root: 'Tab__root'
};










































/**
 * Вложенный элемент компонента Tabs.
 */var

Tab = exports.Tab = (0, _rootNode.rootNode)(_class = (_Tab = /*#__PURE__*/function (_React$Component) {






















  function Tab(props) {var _this;
    _this = _React$Component.call(this, props) || this;_this.context = _this.context;_this.getProps = (0, _createPropsGetter.createPropsGetter)(Tab.defaultProps);_this.state = { focusedByKeyboard: false };_this.tabComponent = null;_this.












































    getUnderlyingNode = function () {return _this.tabComponent;};_this.









































































    getId = function () {return _this.props.id || _this.getProps().href;};_this.

    refTabComponent = function (instance) {
      _this.tabComponent = instance;
    };_this.

    getTabInstance = function () {return _this;};_this.

    switchTab = function (event) {
      if (_this.props.disabled) {
        event.preventDefault();
        return;
      }
      var _this$getProps = _this.getProps(),href = _this$getProps.href,component = _this$getProps.component;

      var id = _this.props.id || href;
      if (_this.props.onClick) {
        _this.props.onClick(event);
        if (event.defaultPrevented) {
          return;
        }
      }
      if (typeof id === 'string') {
        _this.context.switchTab(id);
      }
      if (component === 'a' && !href) {
        event.preventDefault();
      }
    };_this.

    handleKeyDown = function (e) {
      if (_this.props.disabled) {
        return;
      }

      if (_this.props.onKeyDown) {
        _this.props.onKeyDown(e);
        if (e.defaultPrevented) {
          return;
        }
      }
      var id = _this.getId();
      if (typeof id !== 'string') {
        return;
      }
      if (!(0, _identifiers.isKeyArrow)(e)) {
        return;
      }
      e.preventDefault();
      var delta = (0, _identifiers.isKeyArrowLeft)(e) || (0, _identifiers.isKeyArrowUp)(e) ? -1 : 1;
      _this.context.shiftFocus(id, delta);
    };_this.

    handleFocus = function () {
      if (_this.props.disabled) {
        return;
      }

      // focus event fires before keyDown eventlistener
      // so we should check focusKeyPressed in async way
      _globalObject.globalObject.requestAnimationFrame == null || _globalObject.globalObject.requestAnimationFrame(function () {
        if (_keyListener.keyListener.isTabPressed || _keyListener.keyListener.isArrowPressed) {
          _this.setState({ focusedByKeyboard: true });
        }
      });
    };_this.

    handleBlur = function () {
      if (_this.props.disabled) {
        return;
      }

      _this.setState({ focusedByKeyboard: false });
    };(0, _invariant.default)(_this.context !== _TabsContext.TabsContextDefaultValue, 'Tab should be placed inside Tabs component');return _this;}(0, _inheritsLoose2.default)(Tab, _React$Component);var _proto = Tab.prototype;_proto.componentDidMount = function componentDidMount() {var id = this.getId();if (typeof id === 'string') {this.context.addTab(id, this.getTabInstance);}};_proto.componentDidUpdate = function componentDidUpdate() {if (this.context.activeTab === this.props.id) {this.context.notifyUpdate();}};_proto.componentWillUnmount = function componentWillUnmount() {var id = this.getId();if (typeof id === 'string') {this.context.removeTab(id);}};_proto.render = function render() {var _this2 = this;return /*#__PURE__*/_react.default.createElement(_ThemeContext.ThemeContext.Consumer, null, function (theme) {_this2.theme = theme;return _this2.renderMain();});};_proto.getIndicators = function getIndicators() {return { error: Boolean(this.props.error), warning: Boolean(this.props.warning), success: Boolean(this.props.success), primary: Boolean(this.props.primary), disabled: Boolean(this.props.disabled) };};_proto.renderMain = function renderMain() {var _cx, _cx2;var _this$props = this.props,children = _this$props.children,disabled = _this$props.disabled,error = _this$props.error,warning = _this$props.warning,success = _this$props.success,primary = _this$props.primary,ariaDescribedby = _this$props['aria-describedby'],ariaLabel = _this$props['aria-label'];var _this$getProps2 = this.getProps(),Component = _this$getProps2.component,href = _this$getProps2.href;var isActive = false;var isVertical = false;var id = this.getId();if (typeof id === 'string') {isActive = this.context.activeTab === this.getId();isVertical = this.context.vertical;}var orientationStyles = isVertical ? _Tab2.verticalStyles : _Tab2.horizontalStyles;return /*#__PURE__*/_react.default.createElement(_CommonWrapper.CommonWrapper, (0, _extends2.default)({ rootNodeRef: this.setRootNode }, (0, _getVisualStateDataAttributes.getVisualStateDataAttributes)({ active: isActive, disabled: disabled }), this.props), /*#__PURE__*/_react.default.createElement(Component, { "data-tid": TabDataTids.root, className: (0, _Emotion.cx)((_cx = {}, _cx[_Tab2.styles.rootSmall(this.theme)] = this.context.size === 'small', _cx[_Tab2.styles.rootMedium(this.theme)] = this.context.size === 'medium', _cx[_Tab2.styles.rootLarge(this.theme)] = this.context.size === 'large', _cx[_Tab2.styles.verticalSmall(this.theme)] = !!isVertical && this.context.size === 'small', _cx[_Tab2.styles.verticalMedium(this.theme)] = !!isVertical && this.context.size === 'medium', _cx[_Tab2.styles.verticalLarge(this.theme)] = !!isVertical && this.context.size === 'large', _cx[orientationStyles.primary(this.theme)] = !!primary, _cx[orientationStyles.success(this.theme)] = !!success, _cx[orientationStyles.warning(this.theme)] = !!warning, _cx[orientationStyles.error(this.theme)] = !!error, _cx[_Tab2.styles.active()] = !!isActive, _cx[orientationStyles.active(this.theme)] = !!isActive, _cx[_Tab2.styles.disabled(this.theme)] = !!disabled, _cx[orientationStyles.disabled()] = !!disabled, _cx)), onBlur: this.handleBlur, onClick: this.switchTab, onFocus: this.handleFocus, onKeyDown: this.handleKeyDown, tabIndex: disabled ? -1 : 0, ref: (0, _utils.isFunctionalComponent)(Component) ? null : this.refTabComponent, href: href, "aria-describedby": ariaDescribedby, "aria-label": ariaLabel }, /*#__PURE__*/_react.default.createElement(_ResizeDetector.ResizeDetector, { onResize: this.context.notifyUpdate }, children), this.state.focusedByKeyboard && /*#__PURE__*/_react.default.createElement("div", { className: (0, _Emotion.cx)(_Tab2.globalClasses.focus, (_cx2 = {}, _cx2[_Tab2.styles.focusSmall(this.theme)] = this.context.size === 'small', _cx2[_Tab2.styles.focusMedium(this.theme)] = this.context.size === 'medium', _cx2[_Tab2.styles.focusLarge(this.theme)] = this.context.size === 'large', _cx2)) })));};return Tab;}(_react.default.Component), _Tab.__KONTUR_REACT_UI__ = 'Tab', _Tab.displayName = 'Tab', _Tab.contextType = _TabsContext.TabsContext, _Tab.defaultProps = { component: 'a', href: '' }, _Tab)) || _class;