import { cn } from "./index3.js";
import { Tooltip, TooltipPopup, TooltipTrigger } from "./index436.js";
import { cva } from "class-variance-authority";
import { ExternalLink, Globe } from "lucide-react";
import * as React from "react";
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
//#region src/components/organisms/ai/AISource/AISource.tsx
var sourceVariants = cva("inline-flex items-center gap-1.5 transition-colors cursor-pointer focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring", {
	variants: { variant: {
		default: "text-sm text-primary hover:text-primary underline underline-offset-2 rounded-sm",
		pill: "px-2.5 py-1 rounded-full bg-accent hover:bg-surface-2 text-xs text-secondary-foreground delight-selection-press",
		minimal: "text-xs text-quaternary-foreground hover:text-secondary-foreground rounded-sm"
	} },
	defaultVariants: { variant: "default" }
});
function SourceFavicon({ url, favicon, className }) {
	const [hasError, setHasError] = React.useState(false);
	const faviconUrl = React.useMemo(() => {
		if (favicon) return favicon;
		try {
			return `https://www.google.com/s2/favicons?domain=${new URL(url).hostname}&sz=32`;
		} catch {
			return null;
		}
	}, [url, favicon]);
	if (!faviconUrl || hasError) return /* @__PURE__ */ jsx(Globe, { className: cn("size-4 text-muted-foreground", className) });
	return /* @__PURE__ */ jsx("img", {
		src: faviconUrl,
		alt: "",
		className: cn("size-4 rounded delight-fade-in motion-reduce:!animate-none", className),
		onError: () => setHasError(true)
	});
}
SourceFavicon.displayName = "SourceFavicon";
function AISourceTrigger({ source, variant, showFavicon = true, className, onClick }) {
	const displayText = source.title || source.siteName || source.url;
	const sharedClassName = cn(sourceVariants({ variant }), className);
	const sharedChildren = /* @__PURE__ */ jsxs(Fragment$1, { children: [
		showFavicon && /* @__PURE__ */ jsx(SourceFavicon, {
			url: source.url,
			favicon: source.favicon
		}),
		/* @__PURE__ */ jsx("span", {
			className: "truncate max-w-[200px]",
			children: displayText
		}),
		variant === "default" && /* @__PURE__ */ jsx(ExternalLink, {
			className: "size-3 shrink-0",
			"aria-hidden": "true"
		})
	] });
	if (!onClick) return /* @__PURE__ */ jsx("a", {
		"data-slot": "ai-source-trigger",
		href: source.url,
		target: "_blank",
		rel: "noopener noreferrer",
		className: sharedClassName,
		"aria-label": `Source: ${displayText}`,
		children: sharedChildren
	});
	return /* @__PURE__ */ jsx("button", {
		type: "button",
		"data-slot": "ai-source-trigger",
		onClick,
		className: sharedClassName,
		"aria-label": `Source: ${displayText}`,
		children: sharedChildren
	});
}
AISourceTrigger.displayName = "AISourceTrigger";
function AISourceContent({ source, className }) {
	const domain = React.useMemo(() => {
		try {
			return new URL(source.url).hostname;
		} catch {
			return source.url;
		}
	}, [source.url]);
	return /* @__PURE__ */ jsxs("div", {
		className: cn("p-3 max-w-xs space-y-2", className),
		children: [
			/* @__PURE__ */ jsxs("div", {
				className: "flex items-start gap-2",
				children: [/* @__PURE__ */ jsx(SourceFavicon, {
					url: source.url,
					favicon: source.favicon,
					className: "mt-0.5"
				}), /* @__PURE__ */ jsxs("div", {
					className: "flex-1 min-w-0",
					children: [/* @__PURE__ */ jsx("p", {
						className: "text-sm font-medium text-foreground truncate",
						children: source.title || domain
					}), /* @__PURE__ */ jsx("p", {
						className: "text-xs text-quaternary-foreground truncate",
						children: domain
					})]
				})]
			}),
			source.description && /* @__PURE__ */ jsx("p", {
				className: "text-xs text-muted-foreground line-clamp-2",
				children: source.description
			}),
			/* @__PURE__ */ jsxs("a", {
				href: source.url,
				target: "_blank",
				rel: "noopener noreferrer",
				className: "flex items-center gap-1 text-xs text-primary hover:text-primary",
				children: ["Visit source", /* @__PURE__ */ jsx(ExternalLink, { className: "size-3" })]
			})
		]
	});
}
AISourceContent.displayName = "AISourceContent";
function AISource({ source, variant, showFavicon = true, showTooltip = true, className }) {
	if (!showTooltip) return /* @__PURE__ */ jsx("span", {
		"data-slot": "ai-source",
		children: /* @__PURE__ */ jsx(AISourceTrigger, {
			source,
			variant,
			showFavicon,
			className
		})
	});
	return /* @__PURE__ */ jsxs(Tooltip, { children: [/* @__PURE__ */ jsx(TooltipTrigger, { render: /* @__PURE__ */ jsx("span", {
		"data-slot": "ai-source",
		children: /* @__PURE__ */ jsx(AISourceTrigger, {
			source,
			variant,
			showFavicon,
			className
		})
	}) }), /* @__PURE__ */ jsx(TooltipPopup, {
		side: "top",
		className: "p-0",
		children: /* @__PURE__ */ jsx(AISourceContent, { source })
	})] });
}
AISource.displayName = "AISource";
function AISourceList({ sources, maxVisible = 3, showAll = false, onShowMore, className }) {
	const visibleSources = showAll ? sources : sources.slice(0, maxVisible);
	const remainingCount = sources.length - maxVisible;
	return /* @__PURE__ */ jsxs("nav", {
		"data-slot": "ai-source-list",
		"aria-label": "Sources",
		className: cn("flex flex-wrap items-center gap-2", className),
		children: [
			/* @__PURE__ */ jsx("span", {
				className: "text-xs text-quaternary-foreground",
				children: "Sources:"
			}),
			visibleSources.map((source, index) => /* @__PURE__ */ jsx("span", {
				className: "delight-stagger motion-reduce:!animate-none",
				style: { "--delight-delay": `${index * 50}ms` },
				children: /* @__PURE__ */ jsx(AISource, {
					source,
					variant: "pill"
				})
			}, `${source.url}-${index}`)),
			!showAll && remainingCount > 0 && /* @__PURE__ */ jsxs("button", {
				type: "button",
				onClick: onShowMore,
				"aria-label": `Show ${remainingCount} more sources`,
				className: "px-2.5 py-1 rounded-full bg-accent hover:bg-surface-2 text-xs text-secondary-foreground transition-colors focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring delight-selection-press",
				children: [
					"+",
					remainingCount,
					" more"
				]
			})
		]
	});
}
AISourceList.displayName = "AISourceList";
//#endregion
export { AISource, AISourceContent, AISourceList, AISourceTrigger, SourceFavicon, sourceVariants };

//# sourceMappingURL=index437.js.map