"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var tooltip_exports = {};
__export(tooltip_exports, {
  Tooltip: () => Tooltip
});
module.exports = __toCommonJS(tooltip_exports);
var import_react = __toESM(require("react"));
var import_contexts = require("../Utils/contexts.js");
var import_tooltip_styles = require("./tooltip-styles.js");
const lazyWithPreload = (factory) => {
  const Component = import_react.default.lazy(factory);
  Component.preload = factory;
  return Component;
};
const ToolTipComponent = lazyWithPreload(async () => {
  const result = await Promise.resolve().then(() => __toESM(require(
    /* webpackChunkName: "react-portal-tooltip" */
    "react-portal-tooltip"
  )));
  return result.default?.default ? result.default : result;
});
class Tooltip extends import_react.default.Component {
  constructor() {
    super(...arguments);
    this.disabled = false;
    this.mousePosition = { x: 0, y: 0 };
    this.state = {
      tooltipVisible: false
    };
    this.showTooltip = (e) => {
      ToolTipComponent.preload();
      if (!e.target.disabled) {
        this.mousePosition = { x: e.pageX, y: e.pageY };
        this.timeoutId = setTimeout(() => {
          this.setState({ tooltipVisible: true }, () => {
            this.forceUpdate();
            setTimeout(() => this.props.followMouse && this.updateTooltipPosition());
          });
        }, 300);
      }
    };
    this.onMouseMove = (e) => {
      if (this.props.followMouse) {
        this.mousePosition = { x: e.pageX, y: e.pageY };
        this.updateTooltipPosition();
      }
    };
    this.updateTooltipPosition = () => {
      const { x, y } = this.mousePosition;
      const element = document.querySelector(".ToolTipPortal > div");
      if (element) {
        const { offsetWidth: width, offsetHeight: height } = element;
        element.style.left = `${x - width / 2}px`;
        element.style.top = `${y - height - 25}px`;
      }
    };
    this.hideTooltip = () => {
      clearTimeout(this.timeoutId);
      this.setState({ tooltipVisible: false });
    };
    this.wrappChildrenProp = (propName, func) => {
      return {
        [propName]: (e) => {
          func(e);
          this.props.children.props[propName]?.(e);
        }
      };
    };
    this.wrapperProps = {
      ...this.wrappChildrenProp("onMouseEnter", this.showTooltip),
      ...this.wrappChildrenProp("onMouseLeave", this.hideTooltip),
      ...this.wrappChildrenProp("onClick", this.hideTooltip),
      ...this.wrappChildrenProp("onMouseMove", this.onMouseMove)
    };
  }
  componentDidUpdate() {
    this.disabled = window.richContentHideTooltips;
  }
  componentWillUnmount() {
    clearTimeout(this.timeoutId);
  }
  render() {
    const {
      children,
      content,
      isError,
      place,
      tooltipOffset,
      followMouse,
      hideArrow,
      forceVisibility
    } = this.props;
    const { tooltipVisible } = this.state;
    const { isMobile } = this.context;
    const style = (0, import_tooltip_styles.getTooltipStyles)(isError, followMouse, tooltipOffset, place);
    const elementProps = forceVisibility || tooltipVisible ? { ...this.wrapperProps, "data-tooltipid": true } : this.wrapperProps;
    const tooltipArrow = hideArrow ? null : "center";
    return isMobile || this.disabled || !content ? children : /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, import_react.default.cloneElement(import_react.default.Children.only(children), elementProps), forceVisibility || tooltipVisible ? /* @__PURE__ */ import_react.default.createElement(import_react.Suspense, { fallback: null }, /* @__PURE__ */ import_react.default.createElement(
      ToolTipComponent,
      {
        active: forceVisibility || tooltipVisible,
        parent: "[data-tooltipid=true]",
        position: place,
        arrow: tooltipArrow,
        style,
        tooltipTimeout: 10,
        group: "ricos"
      },
      content
    )) : null);
  }
}
Tooltip.defaultProps = {
  isError: false,
  place: "top",
  tooltipOffset: { x: 0, y: 0 }
};
Tooltip.contextType = import_contexts.GlobalContext;
//# sourceMappingURL=tooltip.js.map
