import { jsxs as R, jsx as a } from "react/jsx-runtime";
import T, { useRef as j, useState as z, useEffect as L, useMemo as S, isValidElement as A } from "react";
import H from "../box/Box.js";
import { c as V } from "../../index-BtlgVD4m.js";
import W from "../tooltip/Tooltip.js";
import B from "../icon/components/icon-extend/IconExtend.js";
const d = ["button"], D = 16, G = ({
  ref: v,
  url: x,
  target: f = "_self",
  isMonochrome: b = !1,
  hasUnderline: h = !1,
  onClick: y,
  id: E,
  icon: l,
  children: t,
  iconPosition: c = "right",
  truncate: n = !1,
  ...I
}) => {
  var p;
  const N = f === "_blank", r = j(null), [O, _] = z(!1);
  L(() => {
    if (!n || !r.current) return;
    const e = r.current, o = () => {
      _(e.scrollWidth > e.clientWidth);
    };
    o();
    const i = new ResizeObserver(o);
    return i.observe(e), () => i.disconnect();
  }, [t, n]);
  const u = l ? /* @__PURE__ */ a(B, { icon: l, size: D }) : null, s = S(
    () => T.Children.toArray(t).some((e) => {
      var o;
      if (A(e)) {
        if (d.includes(e.type))
          return !0;
        if (["function", "object"].includes(typeof e.type)) {
          const g = (o = e.type.displayName) == null ? void 0 : o.toLowerCase();
          if (g && d.includes(g))
            return !0;
        }
      }
      return !1;
    }),
    [t]
  ), w = c === "left" ? u : null, C = c === "right" ? u : null, m = /* @__PURE__ */ R(
    "a",
    {
      ref: v,
      href: x,
      target: f,
      rel: N ? "noopener noreferrer" : void 0,
      onClick: y,
      id: E,
      className: V(
        "inline-flex items-center gap-1 transition-colors duration-200",
        n && "max-w-full min-w-0",
        !s && "hover:underline",
        "ring-offset-[var(--base-background)] focus:outline-none focus:ring-1 focus:ring-offset-2",
        b ? [
          "text-[var(--base-foreground)]",
          !s && "hover:text-[var(--base-foreground)]",
          "focus:text-[var(--base-foreground)] focus:ring-[var(--base-foreground)]"
        ] : ["text-[var(--base-highlight)]", !s && "hover:text-[var(--base-highlight)]", "focus:ring-[var(--base-highlight)]"],
        h && "underline"
      ),
      tabIndex: 0,
      ...I,
      children: [
        w,
        n ? /* @__PURE__ */ a(H, { as: "span", ref: r, className: "flex-1 truncate", children: t }) : t,
        C
      ]
    }
  );
  if (!n)
    return m;
  const k = typeof t == "string" ? t : ((p = r.current) == null ? void 0 : p.textContent) ?? "";
  return /* @__PURE__ */ a(W, { content: k, hidden: !O, delayDuration: 300, asChild: !0, children: m });
};
G.displayName = "Link";
export {
  G as default
};
//# sourceMappingURL=Link.js.map
