"use client";
import * as HoverCardPrimitive from '@radix-ui/react-hover-card';
import { clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
import { jsxs, jsx } from 'react/jsx-runtime';

function cn(...inputs) {
  return twMerge(clsx(inputs));
}
var SAFE_URL_SCHEMES = ["http:", "https:", "mailto:"];
function safeUrl(raw) {
  if (raw === void 0 || raw === "") return void 0;
  let parsed;
  try {
    parsed = new URL(raw);
  } catch {
    return raw.includes(":") ? void 0 : raw;
  }
  return SAFE_URL_SCHEMES.some((scheme) => scheme === parsed.protocol) ? raw : void 0;
}
function InlineCitation({
  index,
  title,
  url: rawUrl,
  excerpt,
  openDelay = 200,
  className
}) {
  const url = safeUrl(rawUrl);
  const triggerClasses = cn(
    "inline-flex select-none items-center justify-center rounded-sm bg-primary/10 px-1 py-0.5 text-[0.7em] font-mono font-semibold text-primary leading-none",
    "transition-all duration-[var(--duration-normal,200ms)] ease-out",
    "hover:bg-primary/15",
    "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
    className
  );
  const trigger = url ? /* @__PURE__ */ jsxs(
    "a",
    {
      href: url,
      target: "_blank",
      rel: "noreferrer noopener external",
      className: triggerClasses,
      "aria-label": `Source ${index}: ${title}`,
      children: [
        "[",
        index,
        "]"
      ]
    }
  ) : (
    // No URL → still focusable so keyboard users can open the popover.
    // Real <button type="button"> over a styled span so the trigger
    // honors space/enter activation natively.
    /* @__PURE__ */ jsxs(
      "button",
      {
        type: "button",
        className: triggerClasses,
        "aria-label": `Source ${index}: ${title}`,
        children: [
          "[",
          index,
          "]"
        ]
      }
    )
  );
  return /* @__PURE__ */ jsxs(HoverCardPrimitive.Root, { openDelay, children: [
    /* @__PURE__ */ jsx(HoverCardPrimitive.Trigger, { asChild: true, children: /* @__PURE__ */ jsx("sup", { className: "inline-block leading-none", children: trigger }) }),
    /* @__PURE__ */ jsx(HoverCardPrimitive.Portal, { children: /* @__PURE__ */ jsx(
      HoverCardPrimitive.Content,
      {
        side: "top",
        align: "center",
        sideOffset: 4,
        className: cn(
          "z-50 max-w-xs rounded-md border border-border bg-popover p-3 text-popover-foreground shadow-md",
          "data-[state=open]:animate-in data-[state=closed]:animate-out",
          "data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
          "data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95"
        ),
        children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1", children: [
          /* @__PURE__ */ jsxs("span", { className: "font-mono text-[10px] text-muted-foreground", children: [
            "[",
            index,
            "]"
          ] }),
          /* @__PURE__ */ jsx("span", { className: "text-sm font-medium leading-snug text-foreground", children: title }),
          excerpt ? /* @__PURE__ */ jsx("span", { className: "text-xs leading-relaxed text-muted-foreground", children: excerpt }) : null,
          url ? /* @__PURE__ */ jsx("span", { className: "truncate text-xs text-primary underline-offset-2 hover:underline", children: inferDisplayUrl(url) }) : null
        ] })
      }
    ) })
  ] });
}
function inferDisplayUrl(href) {
  try {
    const url = new URL(href);
    return url.hostname.replace(/^www\./, "") + url.pathname;
  } catch {
    return href;
  }
}

export { InlineCitation };
//# sourceMappingURL=inline-citation.js.map
//# sourceMappingURL=inline-citation.js.map