"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireWildcard(require("react"));

var _Layer = _interopRequireDefault(require("../Layer"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _constants = require("../../constants");

var _utils = require("../utils");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

// Copyright 1999-2020. Plesk International GmbH. All rights reserved.
const createRectFactory = (width, height) => (left, top) => ({
  top,
  left,
  right: left + width,
  bottom: top + height
});

const isInside = (r1, r2) => r1.left >= r2.left && r1.right <= r2.right && r1.top >= r2.top && r1.bottom <= r2.bottom;

const determinePosition = ({
  clientX,
  clientY,
  tooltipWidth,
  tooltipHeight,
  targetRect
}) => {
  const X_EXTRA_SPACE = 8;
  const Y_BOTTOM_EXTRA_SPACE = 16;
  const Y_TOP_EXTRA_SPACE = 8;
  const createTooltipRect = createRectFactory(tooltipWidth, tooltipHeight);
  const viewRect = {
    top: 0,
    left: 0,
    right: document.documentElement.clientWidth,
    bottom: document.documentElement.clientHeight
  };
  const positionFns = [// right bottom
  () => ({
    left: clientX + X_EXTRA_SPACE,
    top: clientY + Y_BOTTOM_EXTRA_SPACE
  }), // left bottom
  () => ({
    left: clientX - tooltipWidth - X_EXTRA_SPACE,
    top: clientY + Y_BOTTOM_EXTRA_SPACE
  }), // center bottom
  () => ({
    left: targetRect.left + (targetRect.right - targetRect.left) / 2 - tooltipWidth / 2,
    top: clientY + Y_BOTTOM_EXTRA_SPACE
  }), // right top
  () => ({
    left: clientX + X_EXTRA_SPACE,
    top: clientY - tooltipHeight - Y_TOP_EXTRA_SPACE
  }), // left top
  () => ({
    left: clientX - tooltipWidth - Y_TOP_EXTRA_SPACE,
    top: clientY - tooltipHeight - Y_TOP_EXTRA_SPACE
  }), // center top
  () => ({
    left: targetRect.left + (targetRect.right - targetRect.left) / 2 - tooltipWidth / 2,
    top: clientY - tooltipHeight - Y_BOTTOM_EXTRA_SPACE
  })];

  for (let i = 0; i < positionFns.length; i++) {
    const fn = positionFns[i];
    const {
      left,
      top
    } = fn();
    const tooltipRect = createTooltipRect(left, top);

    if (isInside(tooltipRect, viewRect)) {
      return {
        left: left + window.pageXOffset,
        top: top + window.pageYOffset
      };
    }
  }

  return {
    top: targetRect.top + Y_BOTTOM_EXTRA_SPACE,
    left: targetRect.left + X_EXTRA_SPACE
  };
};
/**
 * `Tooltip` component is used for providing information about an interface element
 * while user hovers the pointer over the element.
 * @since 0.0.35
 */


class Tooltip extends _react.Component {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2.default)(this, "state", {
      left: 0,
      top: 0,
      ready: false,
      visible: false
    });
    (0, _defineProperty2.default)(this, "handleShow", () => {
      if (this.showTimer) {
        clearTimeout(this.showTimer);
        this.showTimer = null;
      }

      this.setState({
        visible: true
      });
    });
    (0, _defineProperty2.default)(this, "handleHide", () => {
      if (this.showTimer) {
        clearTimeout(this.showTimer);
        this.showTimer = null;
      }

      this.setState({
        visible: false,
        ready: false
      });
    });
    (0, _defineProperty2.default)(this, "handleMouseEnter", () => {
      if (this.showTimer) {
        return;
      }

      this.setState({
        ready: true
      });
      this.showTimer = setTimeout(() => this.handleShow(), this.props.delay);
    });
    (0, _defineProperty2.default)(this, "handleMouseMove", e => {
      // TODO: try to add throttle
      if (this.tooltipEl) {
        const targetRect = e.target.getBoundingClientRect();
        const {
          width: tooltipWidth,
          height: tooltipHeight
        } = this.tooltipEl.getBoundingClientRect();
        const position = determinePosition({
          clientX: e.clientX,
          clientY: e.clientY,
          tooltipWidth,
          tooltipHeight,
          targetRect
        });
        this.setState({ ...position
        });
      }
    });
  }

  componentWillUnmount() {
    clearTimeout(this.showTimer);
  }

  renderTooltip() {
    const {
      baseClassName,
      className,
      children,
      title,
      style,
      delay,
      ...props
    } = this.props;
    const {
      left,
      top,
      visible
    } = this.state;
    return /*#__PURE__*/_react.default.createElement(_Layer.default, {
      level: _constants.Z_INDEX_TOOLTIP
    }, /*#__PURE__*/_react.default.createElement("span", (0, _extends2.default)({
      ref: el => {
        this.tooltipEl = el;
      },
      className: (0, _classnames.default)(baseClassName, className),
      style: { ...style,
        transform: `translate(${left}px, ${top}px)`,
        visibility: visible ? 'visible' : 'hidden'
      },
      role: "tooltip"
    }, props), title));
  }

  render() {
    const {
      ready
    } = this.state;
    return /*#__PURE__*/_react.default.createElement(_react.Fragment, null, /*#__PURE__*/(0, _react.cloneElement)(this.props.children, {
      onMouseEnter: (0, _utils.wrapFunction)(this.props.children.props.onMouseEnter, this.handleMouseEnter),
      onMouseMove: (0, _utils.wrapFunction)(this.props.children.props.onMouseMove, this.handleMouseMove),
      onMouseLeave: (0, _utils.wrapFunction)(this.props.children.props.onMouseLeave, this.handleHide),
      onMouseDown: (0, _utils.wrapFunction)(this.props.children.props.onMouseDown, this.handleHide)
    }), ready && this.renderTooltip());
  }

}

Tooltip.propTypes = {
  /**
   * Content of the tooltip
   * @since 0.0.42
   */
  title: _propTypes.default.node.isRequired,

  /**
   * Show delay (ms)
   * @since 0.0.50
   */
  delay: _propTypes.default.number,

  /**
   * Element with tooltip
   * @since 0.0.42
   */
  children: _propTypes.default.element.isRequired,

  /**
   * @ignore
   */
  style: _propTypes.default.object,

  /**
   * @ignore
   */
  className: _propTypes.default.string,

  /**
   * @ignore
   */
  baseClassName: _propTypes.default.string
};
Tooltip.defaultProps = {
  delay: 550,
  style: undefined,
  className: undefined,
  baseClassName: `${_constants.CLS_PREFIX}tooltip`
};
var _default = Tooltip;
exports.default = _default;