import { jsxs as c, Fragment as be, jsx as t } from "react/jsx-runtime";
import { useLink as ge } from "@react-aria/link";
import { useState as h, useRef as D, useMemo as he, useCallback as S, useEffect as V, useLayoutEffect as Se, cloneElement as W } from "react";
import { usePopper as ve } from "react-popper";
import { Button as Y } from "../Button/Button.es.js";
import { ButtonSize as q, ButtonEmphasis as G, ButtonStyle as J } from "../Button/ButtonTypes.es.js";
import { Portal as ye } from "../Portal/Portal.es.js";
import { IconSize as K } from "../../foundation/Icon/IconSize.es.js";
import { useMemoizedId as Ee } from "../../hooks/useMemoizedId.es.js";
import { FOCUS_VISIBLE_STYLE as xe } from "../../utilities/focusStyle.es.js";
import { merge as f } from "../../utilities/merge.es.js";
import { BrightHeader as Le, brightHeaderArrowBackgroundColors as k } from "./BrightHeader.es.js";
const Q = {
  small: "tw-pt-2",
  large: "tw-pt-3"
}, X = {
  small: "tw-pb-2",
  large: "tw-pb-3"
};
var Ce = /* @__PURE__ */ ((e) => (e.Top = "top", e.Right = "right", e.Bottom = "bottom", e.Left = "left", e))(Ce || {}), ke = /* @__PURE__ */ ((e) => (e.Start = "Start", e.Middle = "Middle", e.End = "End", e))(ke || {});
const ze = {
  "top-Start": "top-start",
  "top-End": "top-end",
  "bottom-Start": "bottom-start",
  "bottom-End": "bottom-end",
  "left-Start": "left-start",
  "left-End": "left-end",
  "right-Start": "right-start",
  "right-End": "right-end",
  "top-Middle": "top",
  "right-Middle": "right",
  "bottom-Middle": "bottom",
  "left-Middle": "left"
}, Te = (e, r, a) => {
  switch (!0) {
    case e.toString().includes("top".toLowerCase()):
      return "before:tw-border-t-0 before:tw-border-l-0 tw-bottom-[-6px] before:tw-bg-primary";
    case e.toString().includes("right".toLowerCase()):
      return f([
        "before:tw-border-t-0 before:tw-border-r-0 tw-left-[-5px]",
        r && // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
        a === "Start" && e.toString().includes("Start".toLowerCase()) ? k[r] : "before:tw-bg-primary"
      ]);
    case e.toString().includes("bottom".toLowerCase()):
      return f([
        "before:tw-border-b-0 before:tw-border-r-0 tw-top-[-6px]",
        r ? k[r] : "before:tw-bg-primary"
      ]);
    case e.toString().includes("left".toLowerCase()):
      return f([
        "before:tw-border-b-0 before:tw-border-l-0 tw-right-[-5px]",
        r && // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
        a === "Start" && e.toString().includes("Start".toLowerCase()) ? k[r] : "before:tw-bg-primary"
      ]);
    default:
      return "before:tw-border-b-0 before:tw-border-r-0 tw-top-[-6px] before:tw-primary";
  }
}, Me = ({
  enablePortal: e = !1,
  children: r
}) => e ? /* @__PURE__ */ t(ye, { children: r }) : r, Ne = ({
  content: e,
  tooltipIcon: r,
  heading: a,
  headingIcon: v,
  linkUrl: m,
  linkLabel: Z,
  brightHeader: x,
  buttons: o,
  children: H,
  position: z = "bottom",
  alignment: T = "Middle",
  withStrongBorder: M = !1,
  withArrow: u,
  flip: P = !0,
  triggerElement: A,
  hoverDelay: N = 200,
  enterDelay: L = 0,
  open: O = !1,
  disabled: U = !1,
  enablePortal: ee = !1,
  hidden: te = !1,
  "data-test-id": R = "tooltip"
}) => {
  var $;
  const [C, re] = h(
    null
  ), [s, i] = h(!1), B = D(null), d = te || U, { linkProps: oe } = ge({ isDisabled: d }, B), se = he(
    () => m || o || a || v,
    [m, o, a, v]
  ), ne = ze[`${z}-${T}`], [b, le] = h(null), [y, ae] = h(null), [ie, de] = h(null), F = Ee(), p = ve(C, b, {
    placement: ne,
    strategy: "fixed",
    modifiers: [
      {
        name: "arrow",
        options: {
          element: ie
        }
      },
      {
        name: "eventListeners",
        options: { scroll: s, resize: s }
      },
      {
        name: "offset",
        options: {
          offset: [0, u ? 10 : 5]
        }
      },
      {
        name: "flip",
        enabled: P
      }
    ]
  }), fe = (($ = p.state) == null ? void 0 : $.placement) ?? z, pe = Te(fe, x, T), l = D(null), we = S(() => {
    l.current && clearTimeout(l.current), l.current = setTimeout(() => i(!1), N);
  }, [N]), I = S(() => {
    if (l.current && clearTimeout(l.current), L) {
      l.current = setTimeout(() => i(!0), L);
      return;
    }
    i(!0);
  }, [L]), ce = S(
    (n) => {
      var w;
      const g = (w = n.composedPath) == null ? void 0 : w.call(n);
      g && [C, y, b].some((j) => j && g.includes(j)) && I();
    },
    [I, b, y, C]
  ), me = S(
    (n) => {
      const { relatedTarget: g } = n;
      (!g || ![b, y].some((w) => w == null ? void 0 : w.contains(g))) && i(!1);
    },
    [b, y]
  ), _ = d ? {} : {
    onMouseOver: (n) => ce(n.nativeEvent),
    onMouseLeave: we,
    onFocus: () => i(!0),
    onBlur: me
  };
  V(() => {
    l.current && clearTimeout(l.current), i(d ? !1 : O);
  }, [O, d]);
  const E = S(
    (n) => {
      s && n.key === "Escape" && i(!1);
    },
    [s]
  );
  return Se(() => {
    typeof p.update == "function" && s && p.update();
  }, [s]), V(() => (s ? window.addEventListener("keydown", E) : window.removeEventListener("keydown", E), () => {
    window.removeEventListener("keydown", E);
  }), [E, s]), /* @__PURE__ */ c(be, { children: [
    /* @__PURE__ */ t("div", { ..._, ref: ae, children: /* @__PURE__ */ t("div", { ref: re, "aria-describedby": F, role: "tooltip", children: A }) }),
    /* @__PURE__ */ t(Me, { enablePortal: ee, children: /* @__PURE__ */ c(
      "div",
      {
        ref: le,
        "aria-hidden": d,
        className: f([
          "tw-popper-container tw-inline-block tw-max-w-[200px] tw-bg-primary tw-rounded-md tw-shadow-mid tw-text-primary-on-primary tw-z-[120000]",
          !s && "tw-opacity-0 tw-h-0 tw-w-0 tw-overflow-hidden",
          M && "tw-border tw-border-line-mid"
        ]),
        "data-test-id": R,
        role: "tooltip",
        id: F,
        style: p.styles.popper,
        ...p.attributes.popper,
        ..._,
        children: [
          x ? /* @__PURE__ */ t(Le, { headerStyle: x }) : null,
          /* @__PURE__ */ c(
            "div",
            {
              className: f([
                "tw-px-3 tw-bg-primary tw-rounded-md tw-relative tw-z-[120000]",
                se ? Q.large : Q.small,
                m ? X.large : X.small
              ]),
              children: [
                a ? /* @__PURE__ */ c("h4", { className: "tw-flex tw-text-m tw-font-bold tw-mb-1", children: [
                  v && /* @__PURE__ */ t("span", { className: "tw-mr-1.5", children: W(v, { size: K.Size20 }) }),
                  a
                ] }) : null,
                /* @__PURE__ */ c("div", { className: "tw-flex", children: [
                  r ? /* @__PURE__ */ t("span", { className: "tw-shrink-0 tw-mr-1 tw-leading-4", children: W(r, { size: K.Size16 }) }) : null,
                  /* @__PURE__ */ t("span", { className: "tw-text-s tw-min-w-0 tw-break-words", children: e })
                ] }),
                m ? /* @__PURE__ */ t(
                  "a",
                  {
                    ...oe,
                    "data-test-id": `${R}-link`,
                    ref: B,
                    href: m,
                    target: "_blank",
                    rel: "noopener noreferrer",
                    className: f([
                      "tw-text-xs tw-text-black-40 tw-underline tw-mt-1",
                      xe
                    ]),
                    children: Z ?? "Click here to learn more."
                  }
                ) : null,
                o ? /* @__PURE__ */ c("div", { className: "tw-flex tw-flex-row-reverse tw-gap-x-1 tw-mt-4", children: [
                  o.length > 0 && /* @__PURE__ */ t(
                    Y,
                    {
                      style: J.Default,
                      emphasis: G.Strong,
                      size: q.Small,
                      onClick: o[0].action,
                      disabled: d,
                      children: o[0].label
                    }
                  ),
                  o.length === 2 && /* @__PURE__ */ t(
                    Y,
                    {
                      style: J.Default,
                      emphasis: G.Default,
                      size: q.Small,
                      onClick: o[1].action,
                      disabled: d,
                      children: o[1].label
                    }
                  )
                ] }) : null
              ]
            }
          ),
          /* @__PURE__ */ t("div", { "aria-hidden": "true", children: /* @__PURE__ */ t(
            "div",
            {
              "data-test-id": "popover-arrow",
              "data-popper-arrow": u,
              ref: de,
              style: p.styles.arrow,
              className: f([
                u && "tw-popper-arrow tw-z-[110000] tw-absolute tw-w-3 tw-h-3 tw-pointer-events-none before:tw-absolute before:tw-w-3 before:tw-h-3 before:tw-rotate-45 before:tw-border",
                u && pe,
                M ? "before:tw-border-line-strong" : u && "before:tw-border-line"
              ])
            }
          ) }),
          H
        ]
      }
    ) })
  ] });
};
Ne.displayName = "FondueLegacyTooltip";
export {
  Ne as LegacyTooltip,
  ke as TooltipAlignment,
  Ce as TooltipPosition
};
//# sourceMappingURL=LegacyTooltip.es.js.map
