import _defineProperty from "@babel/runtime/helpers/defineProperty";
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

import Layer from '../Layer';
import classNames from 'classnames';
import { CLS_PREFIX, Z_INDEX_TOOLTIP } from '../../constants';
import { wrapFunction } from '../utils';
import { cloneElement, Component, createRef } from 'react';
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
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 Component {
  constructor() {
    super(...arguments);
    _defineProperty(this, "state", {
      left: 0,
      top: 0,
      ready: false,
      visible: false
    });
    _defineProperty(this, "timer", null);
    _defineProperty(this, "tooltipRef", /*#__PURE__*/createRef());
    _defineProperty(this, "clearTimer", () => {
      if (this.timer) {
        clearTimeout(this.timer);
        this.timer = null;
      }
    });
    _defineProperty(this, "handleShow", () => {
      this.clearTimer();
      this.setState({
        visible: true
      });
    });
    _defineProperty(this, "handleHide", () => {
      this.clearTimer();
      this.setState({
        visible: false,
        ready: false
      });
    });
    _defineProperty(this, "handleMouseEnter", () => {
      if (this.timer) {
        return;
      }
      this.setState({
        ready: true
      });
      this.timer = setTimeout(() => this.handleShow(), this.props.delay);
    });
    _defineProperty(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__*/_jsx(Layer, {
      level: Z_INDEX_TOOLTIP,
      children: /*#__PURE__*/_jsx("span", {
        ref: this.tooltipRef,
        className: classNames(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__*/_jsxs(_Fragment, {
      children: [/*#__PURE__*/cloneElement(children, {
        onMouseEnter: wrapFunction(children.props.onMouseEnter, this.handleMouseEnter),
        onMouseMove: wrapFunction(children.props.onMouseMove, this.handleMouseMove),
        onMouseLeave: wrapFunction(children.props.onMouseLeave, this.handleHide),
        onMouseDown: wrapFunction(children.props.onMouseDown, this.handleHide)
      }), ready && this.renderTooltip()]
    });
  }
}
_defineProperty(Tooltip, "defaultProps", {
  // in ms.
  delay: 550,
  style: undefined,
  className: undefined,
  baseClassName: `${CLS_PREFIX}tooltip`
});
export default Tooltip;