"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _Layer = _interopRequireDefault(require("../Layer"));
var _classnames = _interopRequireDefault(require("classnames"));
var _constants = require("../../constants");
var _utils = require("../utils");
var _react = require("react");
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. 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 = _ref => {
  let {
    clientX,
    clientY,
    tooltipWidth,
    tooltipHeight,
    targetRect
  } = _ref;
  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() {
    super(...arguments);
    (0, _defineProperty2.default)(this, "state", {
      left: 0,
      top: 0,
      ready: false,
      visible: false
    });
    (0, _defineProperty2.default)(this, "timer", null);
    (0, _defineProperty2.default)(this, "tooltipRef", /*#__PURE__*/(0, _react.createRef)());
    (0, _defineProperty2.default)(this, "clearTimer", () => {
      if (this.timer) {
        clearTimeout(this.timer);
        this.timer = null;
      }
    });
    (0, _defineProperty2.default)(this, "handleShow", () => {
      this.clearTimer();
      this.setState({
        visible: true
      });
    });
    (0, _defineProperty2.default)(this, "handleHide", () => {
      this.clearTimer();
      this.setState({
        visible: false,
        ready: false
      });
    });
    (0, _defineProperty2.default)(this, "handleMouseEnter", () => {
      if (this.timer) {
        return;
      }
      this.setState({
        ready: true
      });
      this.timer = setTimeout(() => this.handleShow(), this.props.delay);
    });
    (0, _defineProperty2.default)(this, "handleMouseMove", e => {
      // TODO: try to add throttle
      if (this.tooltipRef.current) {
        const targetRect = e.currentTarget.getBoundingClientRect();
        const {
          width: tooltipWidth,
          height: tooltipHeight
        } = this.tooltipRef.current.getBoundingClientRect();
        const position = determinePosition({
          clientX: e.clientX,
          clientY: e.clientY,
          tooltipWidth,
          tooltipHeight,
          targetRect
        });
        this.setState({
          ...position
        });
      }
    });
  }
  componentWillUnmount() {
    this.clearTimer();
  }
  renderTooltip() {
    const {
      left,
      top,
      visible
    } = this.state;
    const {
      baseClassName,
      className,
      children,
      title,
      style,
      delay,
      ...props
    } = this.props;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Layer.default, {
      level: _constants.Z_INDEX_TOOLTIP,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        ref: this.tooltipRef,
        className: (0, _classnames.default)(baseClassName, className),
        style: {
          ...style,
          transform: `translate(${left}px, ${top}px)`,
          visibility: visible ? 'visible' : 'hidden'
        },
        role: "tooltip",
        ...props,
        children: title
      })
    });
  }
  render() {
    const {
      ready
    } = this.state;
    const {
      children
    } = this.props;
    return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
      children: [/*#__PURE__*/(0, _react.cloneElement)(children, {
        onMouseEnter: (0, _utils.wrapFunction)(children.props.onMouseEnter, this.handleMouseEnter),
        onMouseMove: (0, _utils.wrapFunction)(children.props.onMouseMove, this.handleMouseMove),
        onMouseLeave: (0, _utils.wrapFunction)(children.props.onMouseLeave, this.handleHide),
        onMouseDown: (0, _utils.wrapFunction)(children.props.onMouseDown, this.handleHide)
      }), ready && this.renderTooltip()]
    });
  }
}
(0, _defineProperty2.default)(Tooltip, "defaultProps", {
  // in ms.
  delay: 550,
  style: undefined,
  className: undefined,
  baseClassName: `${_constants.CLS_PREFIX}tooltip`
});
var _default = exports.default = Tooltip;