import { jsxs as J, Fragment as fe, jsx as C } from "react/jsx-runtime";
import { u as ue, o as me, e as pe, f as de, s as ye, g as he, d as ge } from "../vendor.js";
import { useState as h, useRef as a, useCallback as ve, useMemo as L, useEffect as M } from "react";
import { createPortal as Ee } from "react-dom";
import { o as be, c as Te, s as we, q as Fe, m as Ce, a as Le } from "../tools/tools.js";
import { T as Q } from "./tooltip-arrow.js";
import { T as X } from "./tooltip-inner.js";
const x = function(Y) {
  var z, U;
  const {
    arrowProps: u,
    as: Z = "div",
    className: R,
    container: n,
    dropOldClass: k,
    fade: A = !0,
    html: B,
    inner: H,
    innerProps: $,
    offset: ee,
    onChange: m,
    placement: t = "top",
    style: S,
    trigger: g,
    triggerType: p = "hover",
    triggerWrapper: te,
    variables: D,
    visible: d,
    ...ne
  } = Y, [v, E] = h(!1), [o, y] = h(!1), I = a(null), [N, O] = h(
    typeof n != "string" ? n : null
  ), [q, P] = h(!1), oe = a(() => {
    c(!0);
  }), se = a(() => {
    c(!0);
  }), re = a(() => {
    c(!1);
  }), ae = a(() => {
    P(!0);
  }), ie = a(() => {
    P(!1);
  }), j = a(null), { floatingStyles: le, middlewareData: K, refs: r } = ue({
    middleware: [
      me(() => ee ?? 8, [o]),
      pe((e) => e, [o]),
      de((e) => e, [o]),
      ye((e) => e, [o]),
      he(
        (e) => ({
          ...e,
          element: I
        }),
        [o]
      )
    ],
    onOpenChange: y,
    open: o,
    placement: be(
      [
        ["bottom", t === "bottom"],
        ["left", t === "start" || t === "left"],
        ["right", t === "end" || t === "right"],
        ["top", t === "top"]
      ],
      "right"
    ),
    whileElementsMounted: ge
  }), c = ve(
    (e) => {
      m == null || m(e);
    },
    [m]
  ), V = L(() => {
    const e = Te(
      !k && "tooltip",
      A && "fade",
      o && "show",
      t && {
        "bs-tooltip-bottom": t === "bottom",
        "bs-tooltip-end": t === "end" || t === "right",
        "bs-tooltip-start": t === "start" || t === "left",
        "bs-tooltip-top": t === "top"
      },
      R
    ), i = we((l, T) => ({ tKey: Le(T) }), D, S);
    return {
      className: e,
      style: i
    };
  }, [R, k, A, t, o, S, D]), b = L(() => Fe(p) ? p : [p], [p]), W = L(() => (e) => ({ ...e }), []);
  return M(() => {
    if (d && !v) {
      E(!0);
      return;
    }
    const e = r.floating.current;
    if (!e)
      return;
    const i = () => {
      E(!!d);
    };
    e.addEventListener("transitionend", i);
    let l;
    return d ? l = requestAnimationFrame(() => y(!0)) : y(!1), () => {
      e.removeEventListener("transitionend", i), l && cancelAnimationFrame(l);
    };
  }, [r.floating, v, d]), M(() => {
    if (typeof n == "string") {
      const e = n.startsWith("#") ? document.getElementById(n.slice(1)) : document.querySelector(n);
      O(e);
    } else
      O(n);
  }, [n]), M(() => {
    const e = oe.current, i = se.current, l = re.current, T = ae.current, ce = ie.current, _ = j.current, s = [];
    if (b.includes("focus") && s.push({
      fn: e,
      type: "focus"
    }), b.includes("hover") ? (s.push({
      fn: i,
      type: "mouseenter"
    }), s.push({
      fn: l,
      type: "mouseleave"
    })) : (s.push({
      fn: T,
      type: "mouseenter"
    }), s.push({
      fn: ce,
      type: "mouseleave"
    })), s.length) {
      let f = r.reference.current;
      _ && (f = _);
      const G = () => {
        q || (y(!1), c(!1));
      };
      return document.body.addEventListener("click", G), f && s.forEach(({ fn: w, type: F }) => f.addEventListener(F, w)), () => {
        document.body.removeEventListener("click", G), f && s.forEach(({ fn: w, type: F }) => f.removeEventListener(F, w));
      };
    }
  }, [q, c, r.reference, b]), /* @__PURE__ */ J(fe, { children: [
    g && (te ? /* @__PURE__ */ C("span", { className: "d-inline-block", ref: j, tabIndex: 0, children: g(r.setReference, W) }) : g(r.setReference, W)),
    Ee(
      v && /* @__PURE__ */ J(
        Z,
        {
          ...ne,
          ...V,
          ref: r.setFloating,
          style: { ...V.style, ...le },
          children: [
            /* @__PURE__ */ C(
              Q,
              {
                ...Ce(u, {
                  style: {
                    ...u == null ? void 0 : u.style,
                    left: (z = K.arrow) == null ? void 0 : z.x,
                    top: (U = K.arrow) == null ? void 0 : U.y
                  }
                }),
                onRef: (e) => I.current = e
              }
            ),
            (H || B) && /* @__PURE__ */ C(X, { ...$, html: B, children: H })
          ]
        }
      ),
      N || document.body
    )
  ] });
};
x.Arrow = Q;
x.Inner = X;
x.displayName = "BRL.Tooltip";
export {
  x as T
};
//# sourceMappingURL=tooltip.js.map
