"use client";
import * as CollapsiblePrimitive from '@radix-ui/react-collapsible';
import { clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
import { jsxs, jsx, Fragment } 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 Citation({ title, url, page, index, className, children }) {
  const baseClasses = cn(
    "inline-flex items-center gap-1.5 rounded-md border border-foreground/15 bg-card px-2 py-0.5 text-xs",
    "transition-all duration-[var(--duration-normal,200ms)] ease-out",
    className
  );
  const body = /* @__PURE__ */ jsxs(Fragment, { children: [
    typeof index === "number" ? /* @__PURE__ */ jsxs("span", { className: "font-mono text-[10px] font-semibold text-muted-foreground", children: [
      "[",
      index,
      "]"
    ] }) : /* @__PURE__ */ jsx(DocGlyph, {}),
    /* @__PURE__ */ jsx("span", { className: "truncate text-foreground", children: title }),
    typeof page === "number" ? /* @__PURE__ */ jsxs("span", { className: "text-muted-foreground", children: [
      "p.",
      page
    ] }) : null,
    children
  ] });
  if (url) {
    return /* @__PURE__ */ jsx(
      "a",
      {
        href: url,
        target: "_blank",
        rel: "noreferrer noopener",
        className: cn(
          baseClasses,
          "hover:border-foreground/30 hover:bg-secondary/40 hover:shadow-sm",
          "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2"
        ),
        children: body
      }
    );
  }
  return /* @__PURE__ */ jsx("span", { className: baseClasses, children: body });
}
function DocGlyph() {
  return /* @__PURE__ */ jsxs(
    "svg",
    {
      "aria-hidden": true,
      viewBox: "0 0 16 16",
      width: "11",
      height: "11",
      fill: "none",
      stroke: "currentColor",
      strokeWidth: "1.5",
      strokeLinecap: "round",
      strokeLinejoin: "round",
      className: "shrink-0 text-muted-foreground",
      children: [
        /* @__PURE__ */ jsx("path", { d: "M9 1.5H4.5A1.5 1.5 0 0 0 3 3v10a1.5 1.5 0 0 0 1.5 1.5h7A1.5 1.5 0 0 0 13 13V5.5L9 1.5z" }),
        /* @__PURE__ */ jsx("path", { d: "M9 1.5V5.5h4" })
      ]
    }
  );
}
function Sources({ sources, defaultOpen = true, className }) {
  if (sources.length === 0) return null;
  const count = sources.length;
  const headerLabel = count === 1 ? "1 source" : `${count} sources`;
  return /* @__PURE__ */ jsxs(
    CollapsiblePrimitive.Root,
    {
      defaultOpen,
      className: cn(
        "overflow-hidden rounded-md border border-border bg-card text-card-foreground",
        className
      ),
      children: [
        /* @__PURE__ */ jsxs(
          CollapsiblePrimitive.Trigger,
          {
            className: cn(
              "group flex w-full items-center gap-2 px-3 py-1.5 text-left text-xs text-muted-foreground",
              "transition-all duration-[var(--duration-normal,200ms)] ease-out",
              "hover:text-foreground",
              "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2"
            ),
            children: [
              /* @__PURE__ */ jsx(DocStackGlyph, {}),
              /* @__PURE__ */ jsx("span", { className: "font-medium", children: headerLabel }),
              /* @__PURE__ */ jsx(Chevron, {})
            ]
          }
        ),
        /* @__PURE__ */ jsx(CollapsiblePrimitive.Content, { className: "overflow-hidden border-t border-foreground/[0.06]", children: /* @__PURE__ */ jsx("div", { className: "flex flex-wrap items-center gap-1.5 p-2", children: sources.map((s, i) => /* @__PURE__ */ jsx(
          Citation,
          {
            title: s.title,
            url: safeUrl(s.url),
            page: s.page,
            index: s.index ?? i + 1
          },
          `${s.url ?? s.title}-${i}`
        )) }) })
      ]
    }
  );
}
function DocStackGlyph() {
  return /* @__PURE__ */ jsxs(
    "svg",
    {
      "aria-hidden": true,
      viewBox: "0 0 16 16",
      width: "12",
      height: "12",
      fill: "none",
      stroke: "currentColor",
      strokeWidth: "1.5",
      strokeLinecap: "round",
      strokeLinejoin: "round",
      className: "shrink-0",
      children: [
        /* @__PURE__ */ jsx("path", { d: "M5 3h7l1.5 1.5V13H5V3z" }),
        /* @__PURE__ */ jsx("path", { d: "M3 5v8.5L4.5 15H11" })
      ]
    }
  );
}
function Chevron() {
  return /* @__PURE__ */ jsx(
    "svg",
    {
      "aria-hidden": true,
      viewBox: "0 0 16 16",
      width: "12",
      height: "12",
      fill: "none",
      stroke: "currentColor",
      strokeWidth: "1.5",
      strokeLinecap: "round",
      strokeLinejoin: "round",
      className: "ml-auto shrink-0 transition-transform duration-200 group-data-[state=open]:rotate-180",
      children: /* @__PURE__ */ jsx("path", { d: "M4 6l4 4 4-4" })
    }
  );
}

export { Sources };
//# sourceMappingURL=sources.js.map
//# sourceMappingURL=sources.js.map