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