var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
import { jsx, jsxs } from "react/jsx-runtime";
import React, { useState, useEffect } from "react";
import { bool, node, number, oneOf, string } from "prop-types";
import { Text } from "../..";
import { useCombinedRefs } from "../../hooks";
import { PopoverContainer, PopoverButton, Wrapper } from "./styles";
const Popover = React.forwardRef(
  (_a, forwardedRef) => {
    var _b = _a, {
      children,
      title,
      description,
      position = "bottom-center",
      width = 265,
      height = 200,
      zIndex = 1,
      a11yId,
      hover = false
    } = _b, props = __objRest(_b, [
      "children",
      "title",
      "description",
      "position",
      "width",
      "height",
      "zIndex",
      "a11yId",
      "hover"
    ]);
    const ref = useCombinedRefs(forwardedRef);
    const [isPopoverOpen, setIsPopoverOpen] = useState(false);
    useEffect(() => {
      const handleClickOutside = (event) => {
        if (ref.current && !ref.current.contains(event.target)) {
          setIsPopoverOpen(false);
        }
      };
      document.addEventListener("click", handleClickOutside, true);
      return () => {
        document.removeEventListener("click", handleClickOutside, true);
      };
    }, [setIsPopoverOpen]);
    const handleShowPopover = () => {
      setIsPopoverOpen(true);
    };
    const handleHidePopover = () => {
      setIsPopoverOpen(false);
    };
    const toggleOpenPopover = (event) => {
      event.preventDefault();
      setIsPopoverOpen((current) => !current);
    };
    return /* @__PURE__ */ jsxs(Wrapper, __spreadProps(__spreadValues({}, props), { children: [
      isPopoverOpen && /* @__PURE__ */ jsxs(
        PopoverContainer,
        __spreadProps(__spreadValues({}, a11yId && { id: a11yId }), {
          position,
          width,
          height,
          role: "tooltip",
          $zIndex: zIndex,
          children: [
            !!title && /* @__PURE__ */ jsx(Text.Body2, { bold: true, mb: "xxxsmall", color: "white", children: title }),
            /* @__PURE__ */ jsx(Text.Body2, { m: "zero", color: "white", children: description })
          ]
        })
      ),
      /* @__PURE__ */ jsx(
        PopoverButton,
        __spreadProps(__spreadValues({}, a11yId && { "aria-describedby": a11yId }), {
          ref,
          onMouseEnter: hover ? handleShowPopover : void 0,
          onMouseLeave: hover ? handleHidePopover : void 0,
          onClick: !hover ? toggleOpenPopover : (event) => event.preventDefault(),
          children
        })
      )
    ] }));
  }
);
Popover.propTypes = {
  a11yId: string,
  children: node.isRequired,
  title: string,
  description: string.isRequired,
  /** Position of the popover relative to the children. Accepted values: bottom-[start|center|end], left-[start|center|end], top-[start|center|end] or right-[start|center|end] */
  position: oneOf([
    "bottom-start",
    "bottom-center",
    "bottom-end",
    "left-start",
    "left-center",
    "left-end",
    "top-start",
    "top-center",
    "top-end",
    "right-start",
    "right-center",
    "right-end"
  ]),
  width: number,
  height: number,
  zIndex: number,
  hover: bool
};
var Popover_default = Popover;
export {
  Popover_default as default
};
