import { h as e } from "./shiki-highlighter-BmQSSJpS.js";
import { t } from "./utils-Ct96Mtjw.js";
import { n } from "./use-toast-d9VPBjMn.js";
import { S as r, d as i, h as ee, l as te, n as a, p as o, s, u as c, v as l } from "./AIProvider-C1_b1iKH.js";
import { useEffect as u, useState as d } from "react";
import { Check as f, ExternalLink as p, Eye as m, EyeOff as ne, Info as re, Loader2 as h, Plus as g, Save as ie, Star as ae, Trash2 as _, X as v } from "lucide-react";
import { Fragment as y, jsx as b, jsxs as x } from "react/jsx-runtime";
//#region src/components/ai/AISettingsPanel.tsx
var S = /* @__PURE__ */ e({ AISettingsPanel: () => C });
function C() {
	let { isSettingsOpen: e, closeSettings: S, config: C, updateConfig: w } = a(), { toast: T } = n(), [E, D] = d("openai"), [O, k] = d(l("openai")), [A, j] = d(""), [M, N] = d("providers"), [P, F] = d(!1), [I, L] = d(""), [R, z] = d({
		modelId: "",
		apiKey: "",
		baseUrl: "",
		maxTokens: 4096,
		temperature: .7,
		enabled: !0
	}), [B, V] = d(!1), [H, U] = d(!1), [W, G] = d(null), [K, q] = d(!1), [oe, se] = d([
		"openai",
		"claude",
		"gemini"
	]), ce = [
		"openai",
		"claude",
		"gemini"
	], J = (e) => ce.includes(e);
	u(() => {
		if (C) {
			let e = C.provider || "openai";
			D(e), k(C.models[e] || l("openai")), j(C.systemPrompt || ""), Y();
		}
	}, [C]);
	let Y = async () => {
		se(await ee());
	}, le = (e) => {
		if (!e) return;
		D(e);
		let t = C?.models[e];
		k(t || l(e)), G(null);
	}, X = (e) => {
		k((t) => ({
			...t,
			...e
		})), G(null);
	}, ue = async () => {
		U(!0), G(null);
		try {
			let e = await s(E, O);
			G(e), e.success ? T({
				title: "连接成功",
				description: e.message
			}) : T({
				title: "连接失败",
				description: e.message,
				variant: "destructive"
			});
		} catch (e) {
			let t = e instanceof Error ? e.message : "测试失败";
			G({
				success: !1,
				message: t
			}), T({
				title: "连接失败",
				description: t,
				variant: "destructive"
			});
		} finally {
			U(!1);
		}
	}, de = async () => {
		q(!0);
		try {
			await w({
				provider: E,
				systemPrompt: A,
				models: {
					...C?.models || {},
					[E]: O
				}
			}), T({
				title: "保存成功",
				description: "配置已保存"
			}), S();
		} catch (e) {
			console.error("Failed to save config:", e), T({
				title: "保存失败",
				description: e instanceof Error ? e.message : "未知错误",
				variant: "destructive"
			});
		} finally {
			q(!1);
		}
	}, Z = async () => {
		if (!I.trim()) {
			T({
				title: "请输入 Provider 名称",
				variant: "destructive"
			});
			return;
		}
		if (J(I)) {
			T({
				title: "不能使用预定义的 Provider 名称",
				variant: "destructive"
			});
			return;
		}
		if (C?.models[I]) {
			T({
				title: "Provider 名称已存在",
				variant: "destructive"
			});
			return;
		}
		let e = r(R);
		if (!e.valid) {
			T({
				title: "配置无效",
				description: e.message,
				variant: "destructive"
			});
			return;
		}
		try {
			await w({ models: {
				...C?.models || {},
				[I]: R
			} }), F(!1), L(""), z({
				modelId: "",
				apiKey: "",
				baseUrl: "",
				maxTokens: 4096,
				temperature: .7,
				enabled: !0
			}), await Y(), D(I), k(R), T({
				title: "添加成功",
				description: `Provider "${I}" 已添加`
			});
		} catch (e) {
			console.error("Failed to add provider:", e), T({
				title: "添加失败",
				description: e instanceof Error ? e.message : "未知错误",
				variant: "destructive"
			});
		}
	}, fe = async (e) => {
		if (J(e)) {
			T({
				title: "不能删除预定义的 Provider",
				variant: "destructive"
			});
			return;
		}
		if (confirm(`确定要删除 "${e}" 吗？`)) try {
			await o(e), await Y(), E === e && (D("openai"), k(C?.models.openai || l("openai"))), T({
				title: "删除成功",
				description: `Provider "${e}" 已删除`
			});
		} catch (e) {
			console.error("Failed to delete provider:", e), T({
				title: "删除失败",
				description: e instanceof Error ? e.message : "未知错误",
				variant: "destructive"
			});
		}
	}, pe = async () => {
		q(!0);
		try {
			await w({ systemPrompt: A }), T({
				title: "保存成功",
				description: "系统提示词已更新"
			});
		} catch (e) {
			console.error("Failed to save system prompt:", e), T({
				title: "保存失败",
				description: e instanceof Error ? e.message : "未知错误",
				variant: "destructive"
			});
		} finally {
			q(!1);
		}
	};
	if (!e) return null;
	let Q = i(E), $ = te(E), me = J(E);
	return /* @__PURE__ */ x("div", {
		className: "fixed inset-0 z-50 flex items-center justify-center",
		children: [/* @__PURE__ */ b("div", {
			className: "absolute inset-0 bg-black/50",
			onClick: S
		}), /* @__PURE__ */ x("div", {
			className: t("relative bg-background border rounded-xl shadow-2xl", "w-full max-w-2xl mx-4 max-h-[90vh] overflow-hidden", "flex flex-col", "animate-in zoom-in-95 duration-200"),
			children: [
				/* @__PURE__ */ x("div", {
					className: "flex items-center justify-between p-4 border-b",
					children: [/* @__PURE__ */ b("h2", {
						className: "text-lg font-semibold",
						children: "AI 设置"
					}), /* @__PURE__ */ b("button", {
						onClick: S,
						className: "p-1.5 rounded hover:bg-muted transition-colors",
						children: /* @__PURE__ */ b(v, { className: "w-4 h-4" })
					})]
				}),
				/* @__PURE__ */ x("div", {
					className: "flex border-b",
					children: [/* @__PURE__ */ b("button", {
						onClick: () => N("providers"),
						className: t("flex-1 px-4 py-3 text-sm font-medium transition-colors", M === "providers" ? "border-b-2 border-primary text-primary" : "text-muted-foreground hover:text-foreground"),
						children: "Provider 配置"
					}), /* @__PURE__ */ b("button", {
						onClick: () => N("settings"),
						className: t("flex-1 px-4 py-3 text-sm font-medium transition-colors", M === "settings" ? "border-b-2 border-primary text-primary" : "text-muted-foreground hover:text-foreground"),
						children: "系统设置"
					})]
				}),
				/* @__PURE__ */ x("div", {
					className: "flex-1 overflow-y-auto",
					children: [M === "providers" && /* @__PURE__ */ x("div", {
						className: "p-4 space-y-6",
						children: [
							/* @__PURE__ */ x("div", {
								className: "space-y-3",
								children: [/* @__PURE__ */ x("div", {
									className: "flex items-center justify-between",
									children: [/* @__PURE__ */ b("label", {
										className: "text-sm font-medium",
										children: "选择 Provider"
									}), /* @__PURE__ */ x("button", {
										onClick: () => F(!0),
										className: "flex items-center gap-1 px-3 py-1.5 rounded-lg text-sm bg-primary text-primary-foreground hover:bg-primary/90 transition-colors",
										children: [/* @__PURE__ */ b(g, { className: "w-4 h-4" }), "添加"]
									})]
								}), /* @__PURE__ */ b("div", {
									className: "grid grid-cols-2 gap-2",
									children: oe.map((e) => /* @__PURE__ */ x("div", {
										className: t("relative flex items-center justify-between p-3 rounded-lg border transition-colors", E === e ? "bg-primary/10 border-primary" : "bg-background hover:bg-muted border-border"),
										children: [/* @__PURE__ */ x("button", {
											onClick: () => le(e),
											className: "flex-1 text-left",
											children: [/* @__PURE__ */ b("div", {
												className: "font-medium text-sm",
												children: c(e)
											}), C?.provider === e && /* @__PURE__ */ x("div", {
												className: "text-xs text-primary flex items-center gap-1",
												children: [/* @__PURE__ */ b(ae, { className: "w-3 h-3" }), "当前使用"]
											})]
										}), !me && /* @__PURE__ */ b("button", {
											onClick: () => fe(e),
											className: "p-1.5 rounded hover:bg-muted text-destructive",
											title: "删除",
											children: /* @__PURE__ */ b(_, { className: "w-4 h-4" })
										})]
									}, e))
								})]
							}),
							!P && /* @__PURE__ */ x("div", {
								className: "space-y-4",
								children: [
									/* @__PURE__ */ x("h3", {
										className: "text-sm font-medium",
										children: [c(E), " 配置"]
									}),
									/* @__PURE__ */ x("div", {
										className: "space-y-2",
										children: [/* @__PURE__ */ x("div", {
											className: "flex items-center justify-between",
											children: [/* @__PURE__ */ b("label", {
												className: "text-sm font-medium",
												children: "API 密钥"
											}), Q.apiKeyUrl && /* @__PURE__ */ x("a", {
												href: Q.apiKeyUrl,
												target: "_blank",
												rel: "noopener noreferrer",
												className: "text-xs text-primary hover:underline flex items-center gap-1",
												children: ["获取密钥", /* @__PURE__ */ b(p, { className: "w-3 h-3" })]
											})]
										}), /* @__PURE__ */ x("div", {
											className: "relative",
											children: [/* @__PURE__ */ b("input", {
												type: B ? "text" : "password",
												value: O.apiKey,
												onChange: (e) => X({ apiKey: e.target.value }),
												placeholder: "sk-...",
												className: "w-full px-3 py-2 pr-10 rounded-lg border bg-background text-sm focus:outline-none focus:ring-2 focus:ring-ring"
											}), /* @__PURE__ */ b("button", {
												type: "button",
												onClick: () => V(!B),
												className: "absolute right-2 top-1/2 -translate-y-1/2 p-1 text-muted-foreground hover:text-foreground",
												children: b(B ? ne : m, { className: "w-4 h-4" })
											})]
										})]
									}),
									/* @__PURE__ */ x("div", {
										className: "space-y-2",
										children: [/* @__PURE__ */ b("label", {
											className: "text-sm font-medium",
											children: "API 地址"
										}), /* @__PURE__ */ b("input", {
											type: "text",
											value: O.baseUrl,
											onChange: (e) => X({ baseUrl: e.target.value }),
											placeholder: "https://api.openai.com/v1",
											className: "w-full px-3 py-2 rounded-lg border bg-background text-sm focus:outline-none focus:ring-2 focus:ring-ring"
										})]
									}),
									/* @__PURE__ */ x("div", {
										className: "space-y-2",
										children: [/* @__PURE__ */ b("label", {
											className: "text-sm font-medium",
											children: "模型"
										}), $.length > 0 ? /* @__PURE__ */ x("select", {
											value: O.modelId,
											onChange: (e) => X({ modelId: e.target.value }),
											className: "w-full px-3 py-2 rounded-lg border bg-background text-sm focus:outline-none focus:ring-2 focus:ring-ring",
											children: [/* @__PURE__ */ b("option", {
												value: "",
												children: "选择模型"
											}), $.map((e) => /* @__PURE__ */ b("option", {
												value: e,
												children: e
											}, e))]
										}) : /* @__PURE__ */ b("input", {
											type: "text",
											value: O.modelId,
											onChange: (e) => X({ modelId: e.target.value }),
											placeholder: "gpt-4o-mini",
											className: "w-full px-3 py-2 rounded-lg border bg-background text-sm focus:outline-none focus:ring-2 focus:ring-ring"
										})]
									}),
									/* @__PURE__ */ x("details", {
										className: "group",
										children: [/* @__PURE__ */ x("summary", {
											className: "cursor-pointer text-sm font-medium flex items-center gap-1",
											children: [/* @__PURE__ */ b("span", { children: "高级设置" }), /* @__PURE__ */ b("span", {
												className: "text-muted-foreground group-open:rotate-90 transition-transform",
												children: "▶"
											})]
										}), /* @__PURE__ */ x("div", {
											className: "mt-4 space-y-4 pl-2",
											children: [/* @__PURE__ */ x("div", {
												className: "space-y-2",
												children: [/* @__PURE__ */ x("div", {
													className: "flex items-center justify-between",
													children: [/* @__PURE__ */ b("label", {
														className: "text-sm",
														children: "温度 (Temperature)"
													}), /* @__PURE__ */ b("span", {
														className: "text-sm text-muted-foreground",
														children: O.temperature.toFixed(1)
													})]
												}), /* @__PURE__ */ b("input", {
													type: "range",
													min: "0",
													max: "2",
													step: "0.1",
													value: O.temperature,
													onChange: (e) => X({ temperature: parseFloat(e.target.value) }),
													className: "w-full"
												})]
											}), /* @__PURE__ */ x("div", {
												className: "space-y-2",
												children: [/* @__PURE__ */ b("label", {
													className: "text-sm",
													children: "最大输出长度 (Max Tokens)"
												}), /* @__PURE__ */ b("input", {
													type: "number",
													min: "1",
													max: "128000",
													value: O.maxTokens,
													onChange: (e) => X({ maxTokens: parseInt(e.target.value) || 4096 }),
													className: "w-full px-3 py-2 rounded-lg border bg-background text-sm focus:outline-none focus:ring-2 focus:ring-ring"
												})]
											})]
										})]
									}),
									W && /* @__PURE__ */ x("div", {
										className: t("flex items-center gap-2 p-3 rounded-lg text-sm", W.success ? "bg-green-500/10 text-green-600 dark:text-green-400" : "bg-destructive/10 text-destructive"),
										children: [W.success ? /* @__PURE__ */ b(f, { className: "w-4 h-4 shrink-0" }) : /* @__PURE__ */ b(re, { className: "w-4 h-4 shrink-0" }), /* @__PURE__ */ b("span", { children: W.message })]
									}),
									/* @__PURE__ */ x("div", {
										className: "flex items-center gap-2 pt-4 border-t",
										children: [/* @__PURE__ */ b("button", {
											onClick: ue,
											disabled: !O.apiKey || !O.modelId || H,
											className: t("flex items-center gap-2 px-4 py-2 rounded-lg text-sm", "border border-border hover:bg-muted transition-colors", "disabled:opacity-50 disabled:cursor-not-allowed"),
											children: H ? /* @__PURE__ */ x(y, { children: [/* @__PURE__ */ b(h, { className: "w-4 h-4 animate-spin" }), "测试中..."] }) : "测试连接"
										}), /* @__PURE__ */ b("button", {
											onClick: de,
											disabled: K,
											className: t("flex items-center gap-2 px-4 py-2 rounded-lg text-sm ml-auto", "bg-primary text-primary-foreground hover:bg-primary/90 transition-colors", "disabled:opacity-50 disabled:cursor-not-allowed"),
											children: K ? /* @__PURE__ */ x(y, { children: [/* @__PURE__ */ b(h, { className: "w-4 h-4 animate-spin" }), "保存中..."] }) : "保存设置"
										})]
									})
								]
							}),
							P && /* @__PURE__ */ x("div", {
								className: "space-y-4",
								children: [
									/* @__PURE__ */ b("h3", {
										className: "text-sm font-medium",
										children: "添加自定义 Provider"
									}),
									/* @__PURE__ */ x("div", {
										className: "space-y-2",
										children: [
											/* @__PURE__ */ b("label", {
												className: "text-sm font-medium",
												children: "Provider 名称"
											}),
											/* @__PURE__ */ b("input", {
												type: "text",
												value: I,
												onChange: (e) => L(e.target.value),
												placeholder: "deepseek",
												className: "w-full px-3 py-2 rounded-lg border bg-background text-sm focus:outline-none focus:ring-2 focus:ring-ring"
											}),
											/* @__PURE__ */ b("p", {
												className: "text-xs text-muted-foreground",
												children: "名称将用于标识此 Provider，不能与预定义 Provider 重复"
											})
										]
									}),
									/* @__PURE__ */ x("div", {
										className: "space-y-2",
										children: [/* @__PURE__ */ b("label", {
											className: "text-sm font-medium",
											children: "API 密钥"
										}), /* @__PURE__ */ b("input", {
											type: B ? "text" : "password",
											value: R.apiKey,
											onChange: (e) => z((t) => ({
												...t,
												apiKey: e.target.value
											})),
											placeholder: "sk-...",
											className: "w-full px-3 py-2 rounded-lg border bg-background text-sm focus:outline-none focus:ring-2 focus:ring-ring"
										})]
									}),
									/* @__PURE__ */ x("div", {
										className: "space-y-2",
										children: [/* @__PURE__ */ b("label", {
											className: "text-sm font-medium",
											children: "API 地址"
										}), /* @__PURE__ */ b("input", {
											type: "text",
											value: R.baseUrl,
											onChange: (e) => z((t) => ({
												...t,
												baseUrl: e.target.value
											})),
											placeholder: "https://api.deepseek.com",
											className: "w-full px-3 py-2 rounded-lg border bg-background text-sm focus:outline-none focus:ring-2 focus:ring-ring"
										})]
									}),
									/* @__PURE__ */ x("div", {
										className: "space-y-2",
										children: [/* @__PURE__ */ b("label", {
											className: "text-sm font-medium",
											children: "模型"
										}), /* @__PURE__ */ b("input", {
											type: "text",
											value: R.modelId,
											onChange: (e) => z((t) => ({
												...t,
												modelId: e.target.value
											})),
											placeholder: "deepseek-chat",
											className: "w-full px-3 py-2 rounded-lg border bg-background text-sm focus:outline-none focus:ring-2 focus:ring-ring"
										})]
									}),
									/* @__PURE__ */ x("div", {
										className: "flex items-center gap-2 pt-4 border-t",
										children: [/* @__PURE__ */ b("button", {
											onClick: () => F(!1),
											className: "px-4 py-2 rounded-lg text-sm border border-border hover:bg-muted transition-colors",
											children: "取消"
										}), /* @__PURE__ */ b("button", {
											onClick: Z,
											disabled: K,
											className: t("flex items-center gap-2 px-4 py-2 rounded-lg text-sm ml-auto", "bg-primary text-primary-foreground hover:bg-primary/90 transition-colors", "disabled:opacity-50 disabled:cursor-not-allowed"),
											children: K ? /* @__PURE__ */ x(y, { children: [/* @__PURE__ */ b(h, { className: "w-4 h-4 animate-spin" }), "添加中..."] }) : /* @__PURE__ */ x(y, { children: [/* @__PURE__ */ b(g, { className: "w-4 h-4" }), "添加"] })
										})]
									})
								]
							})
						]
					}), M === "settings" && /* @__PURE__ */ x("div", {
						className: "p-4 space-y-6",
						children: [/* @__PURE__ */ x("div", {
							className: "space-y-2",
							children: [
								/* @__PURE__ */ b("label", {
									className: "text-sm font-medium",
									children: "系统提示词"
								}),
								/* @__PURE__ */ b("textarea", {
									value: A,
									onChange: (e) => j(e.target.value),
									rows: 6,
									placeholder: "你是一个专业的文档助手，请根据用户的问题，提供准确、有帮助的回答。",
									className: "w-full px-3 py-2 rounded-lg border bg-background text-sm focus:outline-none focus:ring-2 focus:ring-ring resize-none"
								}),
								/* @__PURE__ */ b("p", {
									className: "text-xs text-muted-foreground",
									children: "系统提示词用于定义 AI 助手的行为和角色"
								})
							]
						}), /* @__PURE__ */ b("div", {
							className: "flex items-center gap-2 pt-4 border-t",
							children: /* @__PURE__ */ b("button", {
								onClick: pe,
								disabled: K,
								className: t("flex items-center gap-2 px-4 py-2 rounded-lg text-sm ml-auto", "bg-primary text-primary-foreground hover:bg-primary/90 transition-colors", "disabled:opacity-50 disabled:cursor-not-allowed"),
								children: K ? /* @__PURE__ */ x(y, { children: [/* @__PURE__ */ b(h, { className: "w-4 h-4 animate-spin" }), "保存中..."] }) : /* @__PURE__ */ x(y, { children: [/* @__PURE__ */ b(ie, { className: "w-4 h-4" }), "保存提示词"] })
							})
						})]
					})]
				})
			]
		})]
	});
}
//#endregion
export { S as n, C as t };
