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 xe } from "../../hooks/useMemoizedId.es.js";
import { FOCUS_VISIBLE_STYLE as Ee } 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))(ke || {});
const Te = {
  "top-Start": "top-start",
  "bottom-Start": "bottom-start",
  "left-Start": "left-start",
  "right-Start": "right-start",
  "top-Middle": "top",
  "right-Middle": "right",
  "bottom-Middle": "bottom",
  "left-Middle": "left"
}, ze = (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: E,
  buttons: o,
  children: H,
  position: T = "bottom",
  alignment: z = "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]
  ), le = Te[`${T}-${z}`], [b, ne] = h(null), [y, ae] = h(null), [ie, de] = h(null), F = xe(), p = ve(C, b, {
    placement: le,
    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) ?? T, pe = ze(fe, E, z), n = D(null), we = S(() => {
    n.current && clearTimeout(n.current), n.current = setTimeout(() => i(!1), N);
  }, [N]), I = S(() => {
    if (n.current && clearTimeout(n.current), L) {
      n.current = setTimeout(() => i(!0), L);
      return;
    }
    i(!0);
  }, [L]), ce = S(
    (l) => {
      var w;
      const g = (w = l.composedPath) == null ? void 0 : w.call(l);
      g && [C, y, b].some((j) => j && g.includes(j)) && I();
    },
    [I, b, y, C]
  ), me = S(
    (l) => {
      const { relatedTarget: g } = l;
      (!g || ![b, y].some((w) => w == null ? void 0 : w.contains(g))) && i(!1);
    },
    [b, y]
  ), _ = d ? {} : {
    onMouseOver: (l) => ce(l.nativeEvent),
    onMouseLeave: we,
    onFocus: () => i(!0),
    onBlur: me
  };
  V(() => {
    n.current && clearTimeout(n.current), i(d ? !1 : O);
  }, [O, d]);
  const x = S(
    (l) => {
      s && l.key === "Escape" && i(!1);
    },
    [s]
  );
  return Se(() => {
    typeof p.update == "function" && s && p.update();
  }, [s]), V(() => (s ? window.addEventListener("keydown", x) : window.removeEventListener("keydown", x), () => {
    window.removeEventListener("keydown", x);
  }), [x, 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: ne,
        "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: [
          E ? /* @__PURE__ */ t(Le, { headerStyle: E }) : 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",
                      Ee
                    ]),
                    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
