"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;exports.__esModule = true;exports.KebabDataTids = exports.Kebab = void 0;var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = _interopRequireDefault(require("react"));
var _reactIs = require("react-is");
var _globalObject = require("@skbkontur/global-object");

var _utils = require("../../lib/utils");
var _identifiers = require("../../lib/events/keyboard/identifiers");
var LayoutEvents = _interopRequireWildcard(require("../../lib/LayoutEvents"));
var _keyListener = require("../../lib/events/keyListener");

var _PopupMenu = require("../../internal/PopupMenu");


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

var _currentEnvironment = require("../../lib/currentEnvironment");
var _ThemeFactory = require("../../lib/theming/ThemeFactory");

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 _SizeDecorator = require("../../lib/size/SizeDecorator");

var _Kebab2 = require("./Kebab.styles");
var _KebabIcon = require("./KebabIcon");var _class, _Kebab;































var KebabDataTids = exports.KebabDataTids = {
  caption: 'Kebab__caption'
};








/**
 * Кебаб-меню `Kebab` содержит действия с объектом.
 */var



Kebab = exports.Kebab = (0, _rootNode.rootNode)(_class = (0, _SizeDecorator.withSize)(_class = (_Kebab = /*#__PURE__*/function (_React$Component) {function Kebab() {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)(Kebab.defaultProps);_this.

    state = {
      focusedByTab: false,
      anchor: null
    };_this.






    listener =

    {
      remove: function remove() {return undefined;}
    };_this.

























































    renderCaption = function (captionProps) {
      var _this$getProps = _this.getProps(),disabled = _this$getProps.disabled;
      var size = _this.size;
      var handleCaptionKeyDown = function handleCaptionKeyDown(event) {
        if (!disabled) {
          _this.handleCaptionKeyDown(event, captionProps.openMenu);
        }
      };

      var handleCaptionClick = function handleCaptionClick() {
        if (!disabled) {
          captionProps.toggleMenu();
        }
      };

      return /*#__PURE__*/(
        _react.default.createElement("span", {
          role: "button",
          "data-tid": KebabDataTids.caption,
          tabIndex: disabled ? -1 : 0,
          onClick: handleCaptionClick,
          onKeyDown: handleCaptionKeyDown,
          onFocus: _this.handleFocus,
          onBlur: _this.handleBlur,
          className: (0, _Emotion.cx)(
            _Kebab2.styles.kebab(_this.theme),
            size === 'small' && _Kebab2.styles.kebabSmall(_this.theme),
            size === 'medium' && _Kebab2.styles.kebabMedium(_this.theme),
            size === 'large' && _Kebab2.styles.kebabLarge(_this.theme),
            captionProps.opened && _Kebab2.styles.opened(_this.theme),
            disabled && _Kebab2.styles.disabled(),
            _this.state.focusedByTab && _Kebab2.styles.focused(_this.theme)
          ),
          "aria-describedby": _this.props['aria-describedby'] },

        _this.renderIcon()
        ));

    };_this.

    handleCaptionKeyDown = function (
    e,
    openMenu)
    {
      if ((0, _identifiers.someKeys)(_identifiers.isKeyEnter, _identifiers.isKeySpace, _identifiers.isKeyArrowVertical)(e)) {
        e.preventDefault();
        openMenu(true);
      }
    };_this.

    handleChangeMenuState = function (isOpened, restoreFocus) {
      _this.setState({
        focusedByTab: !isOpened && restoreFocus
      });
    };_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
      });
    };return _this;}(0, _inheritsLoose2.default)(Kebab, _React$Component);var _proto = Kebab.prototype;_proto.componentDidMount = function componentDidMount() {/** addListener'у нужен колбэк в аргумент */this.listener = LayoutEvents.addListener(function () {return undefined;});};_proto.componentWillUnmount = function componentWillUnmount() {this.listener.remove();};_proto.render = function render() {var _this2 = this;return /*#__PURE__*/_react.default.createElement(_ThemeContext.ThemeContext.Consumer, null, function (theme) {_this2.theme = theme;return /*#__PURE__*/_react.default.createElement(_ThemeContext.ThemeContext.Provider, { value: _ThemeFactory.ThemeFactory.create({ popupMargin: theme.kebabMargin }, theme) }, _this2.renderMain());});};_proto.renderMain = function renderMain() {var disabled = this.props.disabled;var _this$getProps2 = this.getProps(),positions = _this$getProps2.positions,disableAnimations = _this$getProps2.disableAnimations,onOpen = _this$getProps2.onOpen,onClose = _this$getProps2.onClose;return /*#__PURE__*/_react.default.createElement(_CommonWrapper.CommonWrapper, (0, _extends2.default)({ rootNodeRef: this.setRootNode }, this.props, (0, _getVisualStateDataAttributes.getVisualStateDataAttributes)({ disabled: disabled })), /*#__PURE__*/_react.default.createElement(_PopupMenu.PopupMenu, { id: this.props.id, popupHasPin: false, preventIconsOffset: this.props.preventIconsOffset, positions: positions, onChangeMenuState: this.handleChangeMenuState, caption: this.renderCaption, disableAnimations: disableAnimations, menuMaxHeight: this.props.menuMaxHeight, onOpen: onOpen, onClose: onClose, popupMenuId: this.props.popupMenuId, "aria-label": this.props['aria-label'] }, !disabled && this.props.children));};_proto.

  renderIcon = function renderIcon() {
    var _this$getProps3 = this.getProps(),_this$getProps3$icon = _this$getProps3.icon,icon = _this$getProps3$icon === void 0 ? /*#__PURE__*/_react.default.createElement(_KebabIcon.KebabIcon, null) : _this$getProps3$icon;

    if ((0, _reactIs.isElement)(icon) && (0, _utils.isKonturIcon)(icon)) {var _icon$props$size, _icon$props$color;
      var sizes = {
        small: parseInt(this.theme.kebabIconSizeSmall),
        medium: parseInt(this.theme.kebabIconSizeMedium),
        large: parseInt(this.theme.kebabIconSizeLarge)
      };

      return /*#__PURE__*/_react.default.cloneElement(icon, {
        size: (_icon$props$size = icon.props.size) != null ? _icon$props$size : sizes[this.size],
        color: (_icon$props$color = icon.props.color) != null ? _icon$props$color : this.theme.kebabIconColor
      });
    }

    return icon;
  };return Kebab;}(_react.default.Component), _Kebab.__KONTUR_REACT_UI__ = 'Kebab', _Kebab.displayName = 'Kebab', _Kebab.defaultProps = { onOpen: function onOpen() {return undefined;}, onClose: function onClose() {return undefined;}, positions: ['bottom left', 'bottom right', 'top left', 'top right'], disableAnimations: _currentEnvironment.isTestEnv }, _Kebab)) || _class) || _class;