import { r as e } from "./chunk-NBPlniwU.js";
import { n as t, t as n } from "./utils-C1xOyiTe.js";
import { n as r } from "./AIProvider-niZNPSGi.js";
import i, { useCallback as a, useEffect as o, useRef as s, useState as c } from "react";
import { AlertCircle as l, AlertTriangle as u, Bot as d, Check as f, CheckCheck as p, CheckCircle as m, Copy as h, GripVertical as g, Loader2 as _, Pencil as v, RefreshCw as y, Send as b, Settings as x, Square as S, Trash2 as C, User as w, X as T } from "lucide-react";
import { Fragment as E, jsx as D, jsxs as O } from "react/jsx-runtime";
import k from "remark-gfm";
import A from "react-markdown";
//#region src/components/ai/AIChatMessage.tsx
function j({ message: e, onRegenerate: r, onEdit: o, onDelete: s }) {
	let [u, g] = c(!1), [b, x] = c(null), [S, j] = c(!1), [M, N] = c(e.content), P = e.role === "user", F = e.isStreaming, I = !!e.error, L = a(async () => {
		try {
			await t(e.content), g(!0), setTimeout(() => g(!1), 2e3);
		} catch {
			console.error("Failed to copy message");
		}
	}, [e.content, t]), R = a(async (e, n) => {
		n.stopPropagation();
		try {
			await t(e), x(e), setTimeout(() => x(null), 2e3);
		} catch {
			console.error("Failed to copy code");
		}
	}, [t]), z = a(() => {
		M.trim() && M !== e.content && o?.(M.trim()), j(!1);
	}, [
		M,
		e.content,
		o
	]), B = a(() => {
		N(e.content), j(!1);
	}, [e.content]);
	return /* @__PURE__ */ O("div", {
		className: n("flex gap-3 p-4 group", P ? "flex-row-reverse" : "flex-row"),
		children: [/* @__PURE__ */ D("div", {
			className: n("flex-shrink-0 w-8 h-8 rounded-full flex items-center justify-center", P ? "bg-primary text-primary-foreground" : "bg-muted text-muted-foreground"),
			children: D(P ? w : d, { className: "w-4 h-4" })
		}), /* @__PURE__ */ D("div", {
			className: n("flex-1 min-w-0", P ? "text-right" : "text-left"),
			children: S ? /* @__PURE__ */ O("div", {
				className: "inline-block max-w-full",
				children: [/* @__PURE__ */ D("textarea", {
					value: M,
					onChange: (e) => N(e.target.value),
					className: n("w-full min-w-[200px] max-w-[400px] p-3 rounded-lg border", "bg-background text-sm resize-none", "focus:outline-none focus:ring-2 focus:ring-ring"),
					rows: Math.min(10, M.split("\n").length + 1),
					autoFocus: !0
				}), /* @__PURE__ */ O("div", {
					className: "flex justify-end gap-2 mt-2",
					children: [/* @__PURE__ */ D("button", {
						onClick: B,
						className: "p-1.5 rounded text-muted-foreground hover:text-foreground hover:bg-muted transition-colors",
						title: "取消",
						children: /* @__PURE__ */ D(T, { className: "w-4 h-4" })
					}), /* @__PURE__ */ D("button", {
						onClick: z,
						className: "p-1.5 rounded text-primary hover:bg-primary/10 transition-colors",
						title: "保存",
						children: /* @__PURE__ */ D(p, { className: "w-4 h-4" })
					})]
				})]
			}) : /* @__PURE__ */ O(E, { children: [/* @__PURE__ */ O("div", {
				className: n("inline-block max-w-full rounded-lg px-4 py-2", P ? "bg-primary text-primary-foreground" : "bg-muted text-foreground", I && "border border-destructive"),
				children: [e.content ? /* @__PURE__ */ D(A, {
					remarkPlugins: [k],
					components: {
						pre: ({ children: e }) => {
							let t = (e) => {
								if (typeof e == "string") return e;
								if (Array.isArray(e)) return e.map(t).join("");
								if (i.isValidElement(e)) {
									let n = e.props;
									if (n.children) return t(n.children);
								}
								return "";
							}, n = i.Children.toArray(e).find((e) => i.isValidElement(e) && e.type === "code"), r = n && i.isValidElement(n) ? t(n.props.children).replace(/\n$/, "") : "";
							return /* @__PURE__ */ O("div", {
								className: "relative my-3 rounded-lg overflow-hidden border dark:border-zinc-700 border-zinc-200 dark:bg-[#141414] bg-[#fafafa]",
								children: [/* @__PURE__ */ O("div", {
									className: "flex items-center justify-between px-4 py-2 border-b dark:border-zinc-700 border-zinc-200 dark:bg-[#1a1a1a] bg-zinc-100",
									children: [/* @__PURE__ */ D("span", {
										className: "text-xs dark:text-zinc-400 text-zinc-500",
										children: "代码"
									}), r && /* @__PURE__ */ D("button", {
										type: "button",
										onClick: (e) => R(r, e),
										className: "flex items-center gap-1 px-2 py-1 text-xs rounded dark:bg-zinc-700 dark:hover:bg-zinc-600 dark:text-zinc-300 bg-zinc-200 hover:bg-zinc-300 text-zinc-600 transition-colors",
										children: b === r ? /* @__PURE__ */ O(E, { children: [/* @__PURE__ */ D(m, { className: "w-3.5 h-3.5 text-green-500" }), "已复制"] }) : /* @__PURE__ */ O(E, { children: [/* @__PURE__ */ D(h, { className: "w-3.5 h-3.5" }), "复制"] })
									})]
								}), /* @__PURE__ */ D("pre", {
									className: "!m-0 p-4 overflow-x-auto text-sm hljs",
									children: e
								})]
							});
						},
						code: ({ className: e, children: t, ...r }) => /language-(\w+)/.exec(e || "") || e?.includes("hljs") ? /* @__PURE__ */ D("code", {
							className: n(e, "block"),
							...r,
							children: t
						}) : !e && String(t).includes("\n") ? /* @__PURE__ */ D("code", {
							className: "block",
							...r,
							children: t
						}) : /* @__PURE__ */ D("code", {
							className: "px-1.5 py-0.5 rounded dark:bg-zinc-700 bg-zinc-200 text-sm",
							...r,
							children: t
						})
					},
					children: e.content
				}) : F ? /* @__PURE__ */ O("div", {
					className: "flex items-center gap-2 text-muted-foreground",
					children: [/* @__PURE__ */ D(_, { className: "w-4 h-4 animate-spin" }), /* @__PURE__ */ D("span", { children: "正在思考..." })]
				}) : null, I && /* @__PURE__ */ O("div", {
					className: "flex items-center gap-2 text-destructive mt-2",
					children: [/* @__PURE__ */ D(l, { className: "w-4 h-4" }), /* @__PURE__ */ D("span", {
						className: "text-sm",
						children: e.error
					})]
				})]
			}), !F && /* @__PURE__ */ O("div", {
				className: n("flex items-center gap-2 mt-2 opacity-0 group-hover:opacity-100 transition-opacity", P ? "justify-end" : "justify-start"),
				children: [
					!P && e.content && /* @__PURE__ */ O(E, { children: [/* @__PURE__ */ D("button", {
						onClick: L,
						className: "p-1.5 rounded text-muted-foreground hover:text-foreground hover:bg-muted transition-colors",
						title: "复制",
						children: u ? /* @__PURE__ */ D(f, { className: "w-3.5 h-3.5 text-green-500" }) : /* @__PURE__ */ D(h, { className: "w-3.5 h-3.5" })
					}), r && /* @__PURE__ */ D("button", {
						onClick: r,
						className: "p-1.5 rounded text-muted-foreground hover:text-foreground hover:bg-muted transition-colors",
						title: "重新生成",
						children: /* @__PURE__ */ D(y, { className: "w-3.5 h-3.5" })
					})] }),
					P && o && /* @__PURE__ */ D("button", {
						onClick: () => j(!0),
						className: "p-1.5 rounded text-muted-foreground hover:text-foreground hover:bg-muted transition-colors",
						title: "编辑",
						children: /* @__PURE__ */ D(v, { className: "w-3.5 h-3.5" })
					}),
					s && /* @__PURE__ */ D("button", {
						onClick: s,
						className: "p-1.5 rounded text-muted-foreground hover:text-destructive hover:bg-destructive/10 transition-colors",
						title: "删除",
						children: /* @__PURE__ */ D(C, { className: "w-3.5 h-3.5" })
					})
				]
			})] })
		})]
	});
}
//#endregion
//#region src/components/ai/AIChatInput.tsx
function M({ onSend: e, onStop: t, isLoading: r = !1, disabled: i = !1, placeholder: a = "输入消息..." }) {
	let [l, u] = c(""), d = s(null);
	o(() => {
		let e = d.current;
		e && (e.style.height = "auto", e.style.height = `${Math.min(e.scrollHeight, 150)}px`);
	}, [l]);
	let f = () => {
		let t = l.trim();
		!t || r || i || (e(t), u(""), d.current && (d.current.style.height = "auto"));
	};
	return /* @__PURE__ */ O("div", {
		className: "flex items-end gap-2 p-4 border-t bg-background",
		children: [/* @__PURE__ */ D("div", {
			className: "flex-1 relative",
			children: /* @__PURE__ */ D("textarea", {
				ref: d,
				value: l,
				onChange: (e) => u(e.target.value),
				onKeyDown: (e) => {
					e.key === "Enter" && !e.shiftKey && (e.preventDefault(), f());
				},
				placeholder: a,
				disabled: i,
				rows: 1,
				className: n("w-full resize-none rounded-lg border bg-background px-4 py-2.5", "text-sm placeholder:text-muted-foreground", "focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2", "disabled:cursor-not-allowed disabled:opacity-50", "min-h-[40px] max-h-[150px]"),
				style: { height: "40px" }
			})
		}), r ? /* @__PURE__ */ D("button", {
			onClick: t,
			className: n("flex-shrink-0 w-10 h-10 rounded-lg", "flex items-center justify-center", "bg-destructive text-destructive-foreground", "hover:bg-destructive/90 transition-colors"),
			title: "停止生成",
			children: /* @__PURE__ */ D(S, { className: "w-4 h-4" })
		}) : /* @__PURE__ */ D("button", {
			onClick: f,
			disabled: !l.trim() || i,
			className: n("flex-shrink-0 w-10 h-10 rounded-lg", "flex items-center justify-center", "bg-primary text-primary-foreground", "hover:bg-primary/90 transition-colors", "disabled:opacity-50 disabled:cursor-not-allowed"),
			title: "发送消息",
			children: /* @__PURE__ */ D(b, { className: "w-4 h-4" })
		})]
	});
}
//#endregion
//#region src/components/ai/AIChatDialog.tsx
var N = /* @__PURE__ */ e({ AIChatDialog: () => L }), P = 300, F = 700, I = 500;
function L() {
	let { isDialogOpen: e, closeDialog: t, openSettings: i, messages: l, isLoading: f, error: p, isConfigured: m, selectedText: h, sendMessage: _, regenerateLast: v, clearHistory: y, stopGeneration: b, editMessage: S, deleteMessage: w } = r(), E = s(null), k = s(null), A = s(null), N = s(null), L = s(!0), [R, z] = c(I), [B, V] = c(!1), [H, U] = c({
		x: 0,
		y: 0
	}), [W, G] = c(!1), K = s({
		x: 0,
		y: 0,
		posX: 0,
		posY: 0
	}), q = a((e = "smooth") => {
		E.current && E.current.scrollIntoView({ behavior: e });
	}, []), J = a(() => {
		let e = k.current;
		if (!e) return;
		let { scrollTop: t, scrollHeight: n, clientHeight: r } = e;
		L.current = n - t - r < 100;
	}, []);
	o(() => {
		if (L.current && E.current) {
			let e = l[l.length - 1]?.isStreaming;
			q(e ? "auto" : "smooth");
		}
	}, [l, q]), o(() => {
		let n = (n) => {
			n.key === "Escape" && e && t();
		};
		return document.addEventListener("keydown", n), () => document.removeEventListener("keydown", n);
	}, [e, t]);
	let Y = a((e) => {
		e.preventDefault(), V(!0);
		let t = e.clientY, n = R, r = (e) => {
			let r = t - e.clientY;
			z(Math.min(F, Math.max(P, n + r)));
		}, i = () => {
			V(!1), document.removeEventListener("mousemove", r), document.removeEventListener("mouseup", i);
		};
		document.addEventListener("mousemove", r), document.addEventListener("mouseup", i);
	}, [R]), X = a((e) => {
		if (e.target.closest("button")) return;
		e.preventDefault(), G(!0), K.current = {
			x: e.clientX,
			y: e.clientY,
			posX: H.x,
			posY: H.y
		};
		let t = (e) => {
			let t = e.clientX - K.current.x, n = e.clientY - K.current.y;
			U({
				x: K.current.posX + t,
				y: K.current.posY + n
			});
		}, n = () => {
			G(!1), document.removeEventListener("mousemove", t), document.removeEventListener("mouseup", n);
		};
		document.addEventListener("mousemove", t), document.addEventListener("mouseup", n);
	}, [H]), Z = a(() => {
		U({
			x: 0,
			y: 0
		});
	}, []);
	return !m && e ? /* @__PURE__ */ D("div", {
		className: "fixed inset-x-4 bottom-4 z-50 mx-auto max-w-2xl",
		children: /* @__PURE__ */ O("div", {
			className: n("bg-background border rounded-xl shadow-2xl", "animate-in slide-in-from-bottom-4 duration-300"),
			children: [/* @__PURE__ */ O("div", {
				className: "flex items-center justify-between p-4 border-b",
				children: [/* @__PURE__ */ O("div", {
					className: "flex items-center gap-2",
					children: [/* @__PURE__ */ D(d, { className: "w-5 h-5 text-primary" }), /* @__PURE__ */ D("h3", {
						className: "font-semibold",
						children: "AI 助手"
					})]
				}), /* @__PURE__ */ D("button", {
					onClick: t,
					className: "p-1.5 rounded hover:bg-muted transition-colors",
					children: /* @__PURE__ */ D(T, { className: "w-4 h-4" })
				})]
			}), /* @__PURE__ */ O("div", {
				className: "p-6 text-center",
				children: [
					/* @__PURE__ */ D(u, { className: "w-12 h-12 mx-auto text-muted-foreground mb-4" }),
					/* @__PURE__ */ D("h4", {
						className: "text-lg font-medium mb-2",
						children: "需要配置 AI"
					}),
					/* @__PURE__ */ D("p", {
						className: "text-muted-foreground mb-4",
						children: "请先配置 AI 提供商和 API 密钥才能使用此功能"
					}),
					/* @__PURE__ */ D("button", {
						onClick: () => {
							t(), i();
						},
						className: "px-4 py-2 bg-primary text-primary-foreground rounded-lg hover:bg-primary/90 transition-colors",
						children: "前往设置"
					})
				]
			})]
		})
	}) : e ? /* @__PURE__ */ D("div", {
		className: "fixed inset-x-4 bottom-4 z-50 mx-auto max-w-2xl",
		style: { transform: `translate(${H.x}px, ${H.y}px)` },
		children: /* @__PURE__ */ O("div", {
			ref: A,
			className: n("bg-background border rounded-xl shadow-2xl overflow-hidden", "animate-in slide-in-from-bottom-4 duration-300", "flex flex-col", W && "cursor-grabbing", B && "cursor-ns-resize"),
			style: {
				height: `${R}px`,
				maxHeight: "70vh"
			},
			children: [
				/* @__PURE__ */ O("div", {
					className: "flex items-center justify-between p-4 border-b shrink-0 cursor-grab active:cursor-grabbing",
					onMouseDown: X,
					children: [/* @__PURE__ */ O("div", {
						className: "flex items-center gap-2",
						children: [
							/* @__PURE__ */ D(g, { className: "w-4 h-4 text-muted-foreground" }),
							/* @__PURE__ */ D(d, { className: "w-5 h-5 text-primary" }),
							/* @__PURE__ */ D("h3", {
								className: "font-semibold",
								children: "AI 助手"
							})
						]
					}), /* @__PURE__ */ O("div", {
						className: "flex items-center gap-1",
						children: [
							H.x !== 0 || H.y !== 0 ? /* @__PURE__ */ D("button", {
								onClick: Z,
								className: "p-1.5 rounded hover:bg-muted transition-colors text-muted-foreground hover:text-foreground",
								title: "重置位置",
								children: /* @__PURE__ */ D(T, { className: "w-3 h-3" })
							}) : null,
							l.length > 0 && /* @__PURE__ */ D("button", {
								onClick: y,
								className: "p-1.5 rounded hover:bg-muted transition-colors text-muted-foreground hover:text-foreground",
								title: "清除对话",
								children: /* @__PURE__ */ D(C, { className: "w-4 h-4" })
							}),
							/* @__PURE__ */ D("button", {
								onClick: i,
								className: "p-1.5 rounded hover:bg-muted transition-colors text-muted-foreground hover:text-foreground",
								title: "设置",
								children: /* @__PURE__ */ D(x, { className: "w-4 h-4" })
							}),
							/* @__PURE__ */ D("button", {
								onClick: t,
								className: "p-1.5 rounded hover:bg-muted transition-colors text-muted-foreground hover:text-foreground",
								title: "关闭",
								children: /* @__PURE__ */ D(T, { className: "w-4 h-4" })
							})
						]
					})]
				}),
				h && /* @__PURE__ */ O("div", {
					className: "px-4 py-2 bg-muted/50 border-b shrink-0",
					children: [/* @__PURE__ */ D("div", {
						className: "text-xs text-muted-foreground mb-1",
						children: "选中的内容："
					}), /* @__PURE__ */ D("div", {
						className: "text-sm line-clamp-2",
						children: h
					})]
				}),
				/* @__PURE__ */ D("div", {
					ref: k,
					className: "flex-1 overflow-y-auto",
					onScroll: J,
					children: l.length === 0 ? /* @__PURE__ */ O("div", {
						className: "flex flex-col items-center justify-center h-full text-muted-foreground p-4",
						children: [/* @__PURE__ */ D(d, { className: "w-12 h-12 mb-4 opacity-50" }), /* @__PURE__ */ O("p", {
							className: "text-center",
							children: [
								"你好！我是 AI 助手。",
								/* @__PURE__ */ D("br", {}),
								"有什么我可以帮助你的吗？"
							]
						})]
					}) : /* @__PURE__ */ O("div", {
						className: "divide-y",
						children: [l.map((e, t) => /* @__PURE__ */ D(j, {
							message: e,
							onRegenerate: e.role === "assistant" && t === l.length - 1 && !f ? v : void 0,
							onEdit: e.role === "user" ? (t) => S(e.id, t) : void 0,
							onDelete: f ? void 0 : () => w(e.id)
						}, e.id)), /* @__PURE__ */ D("div", { ref: E })]
					})
				}),
				p && /* @__PURE__ */ D("div", {
					className: "px-4 py-2 bg-destructive/10 text-destructive text-sm border-t",
					children: p
				}),
				/* @__PURE__ */ D(M, {
					onSend: _,
					onStop: b,
					isLoading: f,
					placeholder: h ? "询问关于选中内容的问题..." : "输入消息..."
				}),
				/* @__PURE__ */ D("div", {
					ref: N,
					className: "absolute top-0 left-1/2 -translate-x-1/2 w-20 h-2 cursor-ns-resize flex items-center justify-center",
					onMouseDown: Y,
					children: /* @__PURE__ */ D("div", { className: "w-8 h-1 rounded-full bg-muted-foreground/30" })
				})
			]
		})
	}) : null;
}
//#endregion
export { j as i, N as n, M as r, L as t };
