import _extends from "@babel/runtime/helpers/esm/extends";
import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
var _class, _Hint;
import React from 'react';
import { globalObject } from '@skbkontur/global-object';
import { ThemeContext } from "../../../lib/theming/ThemeContext";
import { ThemeFactory } from "../../../lib/theming/ThemeFactory";
import { DUMMY_LOCATION, Popup } from "../../../internal/Popup";
import { isTestEnv } from "../../../lib/currentEnvironment";
import { CommonWrapper } from "../../../internal/CommonWrapper";
import { cx } from "../../../lib/theming/Emotion";
import { rootNode } from "../../../lib/rootNode";
import { createPropsGetter } from "../../../lib/createPropsGetter";
import { styles } from "../Hint.styles";
var HINT_BORDER_COLOR = 'transparent';

/**
 * Всплывающая подсказка `Hint`.
 * По умолчанию отображается при наведении на элемент, но можно задать другие условия отображения.
 *
 * Для подсказки, всплывающей по клику используйте Tooltip.
 */
export var Hint = rootNode(_class = (_Hint = /*#__PURE__*/function (_React$PureComponent) {
  function Hint() {
    var _this;
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _React$PureComponent.call.apply(_React$PureComponent, [this].concat(args)) || this;
    _this.getProps = createPropsGetter(Hint.defaultProps);
    _this.state = {
      opened: _this.getProps().manual ? !!_this.getProps().opened : false,
      position: DUMMY_LOCATION.position
    };
    _this.popupRef = /*#__PURE__*/React.createRef();
    _this.getAnchorElement = function () {
      var _this$popupRef$curren;
      return (_this$popupRef$curren = _this.popupRef.current) == null ? void 0 : _this$popupRef$curren.anchorElement;
    };
    _this.getPositions = function () {
      return _this.props.allowedPositions;
    };
    _this.handleMouseEnter = function (e) {
      if (!_this.getProps().manual && !_this.timer) {
        _this.timer = globalObject.setTimeout(_this.open, 400);
      }
      if (_this.props.onMouseEnter) {
        _this.props.onMouseEnter(e);
      }
    };
    _this.handleMouseLeave = function (e) {
      if (!_this.getProps().manual && _this.timer) {
        globalObject.clearTimeout(_this.timer);
        _this.timer = null;
        _this.setState({
          opened: false
        });
      }
      if (_this.props.onMouseLeave) {
        _this.props.onMouseLeave(e);
      }
    };
    _this.open = function () {
      _this.setState({
        opened: true
      });
    };
    return _this;
  }
  _inheritsLoose(Hint, _React$PureComponent);
  var _proto = Hint.prototype;
  _proto.getAllowedPositions = function getAllowedPositions() {
    return this.props.allowedPositions;
  };
  _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
    var _this$getProps = this.getProps(),
      opened = _this$getProps.opened,
      manual = _this$getProps.manual;
    if (!manual) {
      return;
    }
    if (this.timer) {
      globalObject.clearTimeout(this.timer);
      this.timer = null;
    }
    if (opened !== prevProps.opened) {
      this.setState({
        opened: !!opened
      });
    }
  };
  _proto.componentWillUnmount = function componentWillUnmount() {
    if (this.timer) {
      globalObject.clearTimeout(this.timer);
      this.timer = null;
    }
  };
  _proto.render = function render() {
    var _this2 = this;
    return /*#__PURE__*/React.createElement(ThemeContext.Consumer, null, function (theme) {
      _this2.theme = theme;
      return /*#__PURE__*/React.createElement(ThemeContext.Provider, {
        value: ThemeFactory.create({
          popupMargin: theme.hintMargin,
          popupBorder: theme.hintBorder,
          popupBorderRadius: theme.hintBorderRadius
        }, _this2.theme)
      }, _this2.renderMain());
    });
  };
  _proto.renderMain = function renderMain() {
    var _this3 = this;
    var _this$getProps2 = this.getProps(),
      disableAnimations = _this$getProps2.disableAnimations,
      useWrapper = _this$getProps2.useWrapper;
    return /*#__PURE__*/React.createElement(CommonWrapper, _extends({
      rootNodeRef: this.setRootNode
    }, this.props), /*#__PURE__*/React.createElement(Popup, {
      hasPin: false,
      opened: this.state.opened,
      anchorElement: this.props.children,
      positions: this.getPositions(),
      pos: this.props.pos,
      backgroundColor: this.theme.hintBgColor,
      borderColor: HINT_BORDER_COLOR,
      onPositionChange: function onPositionChange(position) {
        return _this3.setState({
          position: position
        });
      },
      disableAnimations: disableAnimations,
      onMouseEnter: this.handleMouseEnter,
      onMouseLeave: this.handleMouseLeave,
      useWrapper: useWrapper,
      ref: this.popupRef,
      withoutMobile: true
    }, this.renderContent()));
  };
  _proto.renderContent = function renderContent() {
    var _cx;
    if (!this.props.text) {
      return null;
    }
    var _this$getProps3 = this.getProps(),
      maxWidth = _this$getProps3.maxWidth;
    var centerAlignPositions = ['top', 'top center', 'bottom', 'bottom center'];
    var className = cx((_cx = {}, _cx[styles.content(this.theme)] = true, _cx[styles.contentCenter(this.theme)] = centerAlignPositions.includes(this.state.position), _cx));
    return /*#__PURE__*/React.createElement("div", {
      className: className,
      style: {
        maxWidth: maxWidth
      }
    }, this.props.text);
  };
  return Hint;
}(React.PureComponent), _Hint.__KONTUR_REACT_UI__ = 'Hint', _Hint.displayName = 'Hint', _Hint.defaultProps = {
  manual: false,
  opened: false,
  maxWidth: 200,
  disableAnimations: isTestEnv,
  useWrapper: false
}, _Hint)) || _class;