import { u as ee, j as e, c as x, aC as ie, aj as J, aD as le, am as se, a9 as ce, an as de, al as ue, ax as me, aB as xe } from "./DocsApp-CEu0IOyd.js";
import Q, { useState as d, useRef as D, useCallback as S, useEffect as _ } from "react";
import { Bot as $, User as ge, X as G, CheckCheck as pe, CheckCircle as he, Copy as re, Loader2 as X, AlertCircle as fe, Check as oe, RefreshCw as ve, Pencil as be, Trash2 as te, Square as je, Send as ye, AlertTriangle as Ne, GripVertical as we, Settings as ke, Plus as ne, Star as Ce, ExternalLink as Pe, EyeOff as Ie, Eye as Ee, Info as Se, Save as Ae } from "lucide-react";
import Te from "react-markdown";
import Me from "remark-gfm";
function ze(r, p) {
  const n = D(null);
  return S((...m) => {
    n.current && clearTimeout(n.current), n.current = setTimeout(() => r(...m), p);
  }, [r, p]);
}
function $e() {
  const { openDialog: r, isConfigured: p, isDialogOpen: n, openSettings: m } = ee(), [i, u] = d(""), [I, o] = d(!1), [k, E] = d(!1), [l, T] = d({ top: 0, left: 0 }), h = D(null), M = D(null), N = S(() => {
    if (n) {
      o(!1);
      return;
    }
    const v = window.getSelection(), w = v?.toString().trim() || "";
    if (w.length < 5) {
      u(""), o(!1);
      return;
    }
    if (u(w), v && v.rangeCount > 0) {
      const c = v.getRangeAt(0).getBoundingClientRect(), a = c.bottom + window.scrollY + 8, j = c.right + window.scrollX - 40;
      T({ top: a, left: j }), E(!0), o(!0);
    }
  }, [n]), b = ze(N, 150);
  _(() => {
    let v = !1;
    const w = () => {
      v = !0, h.current && (clearTimeout(h.current), h.current = null);
    }, C = () => {
      v = !1, setTimeout(() => {
        N();
      }, 10);
    }, c = () => {
      v || b();
    }, a = (j) => {
      M.current && !M.current.contains(j.target) && (h.current = setTimeout(() => {
        o(!1);
      }, 200));
    };
    return document.addEventListener("mousedown", w), document.addEventListener("mouseup", C), document.addEventListener("selectionchange", c), document.addEventListener("click", a), () => {
      document.removeEventListener("mousedown", w), document.removeEventListener("mouseup", C), document.removeEventListener("selectionchange", c), document.removeEventListener("click", a), h.current && clearTimeout(h.current);
    };
  }, [N, b]);
  const L = S(() => {
    if (i) {
      if (!p) {
        m();
        return;
      }
      r({
        selectedText: i,
        pageTitle: document.title,
        pageUrl: window.location.href
      }), window.getSelection()?.removeAllRanges(), o(!1), u("");
    }
  }, [i, p, r, m]);
  return !I || n ? null : /* @__PURE__ */ e.jsx(
    "button",
    {
      ref: M,
      onClick: L,
      className: x(
        "fixed z-50 flex items-center justify-center w-10 h-10 rounded-full",
        "bg-primary text-primary-foreground shadow-lg",
        "hover:bg-primary/90 transition-all duration-200 hover:scale-110",
        k && "animate-in zoom-in-50 duration-200"
      ),
      style: {
        top: l.top,
        left: l.left
      },
      title: "向AI询问选中内容",
      children: /* @__PURE__ */ e.jsx($, { className: "w-5 h-5" })
    }
  );
}
function De({ message: r, onRegenerate: p, onEdit: n, onDelete: m }) {
  const [i, u] = d(!1), [I, o] = d(null), [k, E] = d(!1), [l, T] = d(r.content), h = r.role === "user", M = r.isStreaming, N = !!r.error, b = S(async (c) => {
    if (navigator.clipboard && window.isSecureContext)
      await navigator.clipboard.writeText(c);
    else {
      const a = document.createElement("textarea");
      a.value = c, a.style.position = "fixed", a.style.left = "-9999px", a.style.top = "-9999px", document.body.appendChild(a), a.focus(), a.select();
      try {
        document.execCommand("copy");
      } finally {
        document.body.removeChild(a);
      }
    }
  }, []), L = S(async () => {
    try {
      await b(r.content), u(!0), setTimeout(() => u(!1), 2e3);
    } catch {
      console.error("Failed to copy message");
    }
  }, [r.content, b]), v = S(async (c, a) => {
    a.stopPropagation();
    try {
      await b(c), o(c), setTimeout(() => o(null), 2e3);
    } catch {
      console.error("Failed to copy code");
    }
  }, [b]), w = S(() => {
    l.trim() && l !== r.content && n?.(l.trim()), E(!1);
  }, [l, r.content, n]), C = S(() => {
    T(r.content), E(!1);
  }, [r.content]);
  return /* @__PURE__ */ e.jsxs(
    "div",
    {
      className: x(
        "flex gap-3 p-4 group",
        h ? "flex-row-reverse" : "flex-row"
      ),
      children: [
        /* @__PURE__ */ e.jsx(
          "div",
          {
            className: x(
              "flex-shrink-0 w-8 h-8 rounded-full flex items-center justify-center",
              h ? "bg-primary text-primary-foreground" : "bg-muted text-muted-foreground"
            ),
            children: h ? /* @__PURE__ */ e.jsx(ge, { className: "w-4 h-4" }) : /* @__PURE__ */ e.jsx($, { className: "w-4 h-4" })
          }
        ),
        /* @__PURE__ */ e.jsx(
          "div",
          {
            className: x(
              "flex-1 min-w-0",
              h ? "text-right" : "text-left"
            ),
            children: k ? /* @__PURE__ */ e.jsxs("div", { className: "inline-block max-w-full", children: [
              /* @__PURE__ */ e.jsx(
                "textarea",
                {
                  value: l,
                  onChange: (c) => T(c.target.value),
                  className: x(
                    "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, l.split(`
`).length + 1),
                  autoFocus: !0
                }
              ),
              /* @__PURE__ */ e.jsxs("div", { className: "flex justify-end gap-2 mt-2", children: [
                /* @__PURE__ */ e.jsx(
                  "button",
                  {
                    onClick: C,
                    className: "p-1.5 rounded text-muted-foreground hover:text-foreground hover:bg-muted transition-colors",
                    title: "取消",
                    children: /* @__PURE__ */ e.jsx(G, { className: "w-4 h-4" })
                  }
                ),
                /* @__PURE__ */ e.jsx(
                  "button",
                  {
                    onClick: w,
                    className: "p-1.5 rounded text-primary hover:bg-primary/10 transition-colors",
                    title: "保存",
                    children: /* @__PURE__ */ e.jsx(pe, { className: "w-4 h-4" })
                  }
                )
              ] })
            ] }) : /* @__PURE__ */ e.jsxs(e.Fragment, { children: [
              /* @__PURE__ */ e.jsxs(
                "div",
                {
                  className: x(
                    "inline-block max-w-full rounded-lg px-4 py-2",
                    h ? "bg-primary text-primary-foreground" : "bg-muted text-foreground",
                    N && "border border-destructive"
                  ),
                  children: [
                    r.content ? /* @__PURE__ */ e.jsx(
                      Te,
                      {
                        remarkPlugins: [Me],
                        components: {
                          pre: ({ children: c }) => {
                            const a = (g) => {
                              if (typeof g == "string") return g;
                              if (Array.isArray(g)) return g.map(a).join("");
                              if (Q.isValidElement(g)) {
                                const A = g.props;
                                if (A.children)
                                  return a(A.children);
                              }
                              return "";
                            }, j = Q.Children.toArray(c).find(
                              (g) => Q.isValidElement(g) && g.type === "code"
                            ), y = j && Q.isValidElement(j) ? a(j.props.children).replace(/\n$/, "") : "";
                            return /* @__PURE__ */ e.jsxs("div", { className: "relative my-3 rounded-lg overflow-hidden border dark:border-zinc-700 border-zinc-200 dark:bg-[#141414] bg-[#fafafa]", children: [
                              /* @__PURE__ */ e.jsxs("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__ */ e.jsx("span", { className: "text-xs dark:text-zinc-400 text-zinc-500", children: "代码" }),
                                y && /* @__PURE__ */ e.jsx(
                                  "button",
                                  {
                                    type: "button",
                                    onClick: (g) => v(y, g),
                                    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: I === y ? /* @__PURE__ */ e.jsxs(e.Fragment, { children: [
                                      /* @__PURE__ */ e.jsx(he, { className: "w-3.5 h-3.5 text-green-500" }),
                                      "已复制"
                                    ] }) : /* @__PURE__ */ e.jsxs(e.Fragment, { children: [
                                      /* @__PURE__ */ e.jsx(re, { className: "w-3.5 h-3.5" }),
                                      "复制"
                                    ] })
                                  }
                                )
                              ] }),
                              /* @__PURE__ */ e.jsx("pre", { className: "!m-0 p-4 overflow-x-auto text-sm hljs", children: c })
                            ] });
                          },
                          code: ({ className: c, children: a, ...j }) => /language-(\w+)/.exec(c || "") || c?.includes("hljs") ? /* @__PURE__ */ e.jsx("code", { className: x(c, "block"), ...j, children: a }) : !c && String(a).includes(`
`) ? /* @__PURE__ */ e.jsx("code", { className: "block", ...j, children: a }) : /* @__PURE__ */ e.jsx("code", { className: "px-1.5 py-0.5 rounded dark:bg-zinc-700 bg-zinc-200 text-sm", ...j, children: a })
                        },
                        children: r.content
                      }
                    ) : M ? /* @__PURE__ */ e.jsxs("div", { className: "flex items-center gap-2 text-muted-foreground", children: [
                      /* @__PURE__ */ e.jsx(X, { className: "w-4 h-4 animate-spin" }),
                      /* @__PURE__ */ e.jsx("span", { children: "正在思考..." })
                    ] }) : null,
                    N && /* @__PURE__ */ e.jsxs("div", { className: "flex items-center gap-2 text-destructive mt-2", children: [
                      /* @__PURE__ */ e.jsx(fe, { className: "w-4 h-4" }),
                      /* @__PURE__ */ e.jsx("span", { className: "text-sm", children: r.error })
                    ] })
                  ]
                }
              ),
              !M && /* @__PURE__ */ e.jsxs("div", { className: x(
                "flex items-center gap-2 mt-2 opacity-0 group-hover:opacity-100 transition-opacity",
                h ? "justify-end" : "justify-start"
              ), children: [
                !h && r.content && /* @__PURE__ */ e.jsxs(e.Fragment, { children: [
                  /* @__PURE__ */ e.jsx(
                    "button",
                    {
                      onClick: L,
                      className: "p-1.5 rounded text-muted-foreground hover:text-foreground hover:bg-muted transition-colors",
                      title: "复制",
                      children: i ? /* @__PURE__ */ e.jsx(oe, { className: "w-3.5 h-3.5 text-green-500" }) : /* @__PURE__ */ e.jsx(re, { className: "w-3.5 h-3.5" })
                    }
                  ),
                  p && /* @__PURE__ */ e.jsx(
                    "button",
                    {
                      onClick: p,
                      className: "p-1.5 rounded text-muted-foreground hover:text-foreground hover:bg-muted transition-colors",
                      title: "重新生成",
                      children: /* @__PURE__ */ e.jsx(ve, { className: "w-3.5 h-3.5" })
                    }
                  )
                ] }),
                h && n && /* @__PURE__ */ e.jsx(
                  "button",
                  {
                    onClick: () => E(!0),
                    className: "p-1.5 rounded text-muted-foreground hover:text-foreground hover:bg-muted transition-colors",
                    title: "编辑",
                    children: /* @__PURE__ */ e.jsx(be, { className: "w-3.5 h-3.5" })
                  }
                ),
                m && /* @__PURE__ */ e.jsx(
                  "button",
                  {
                    onClick: m,
                    className: "p-1.5 rounded text-muted-foreground hover:text-destructive hover:bg-destructive/10 transition-colors",
                    title: "删除",
                    children: /* @__PURE__ */ e.jsx(te, { className: "w-3.5 h-3.5" })
                  }
                )
              ] })
            ] })
          }
        )
      ]
    }
  );
}
function Re({
  onSend: r,
  onStop: p,
  isLoading: n = !1,
  disabled: m = !1,
  placeholder: i = "输入消息..."
}) {
  const [u, I] = d(""), o = D(null);
  _(() => {
    const l = o.current;
    l && (l.style.height = "auto", l.style.height = `${Math.min(l.scrollHeight, 150)}px`);
  }, [u]);
  const k = () => {
    const l = u.trim();
    !l || n || m || (r(l), I(""), o.current && (o.current.style.height = "auto"));
  }, E = (l) => {
    l.key === "Enter" && !l.shiftKey && (l.preventDefault(), k());
  };
  return /* @__PURE__ */ e.jsxs("div", { className: "flex items-end gap-2 p-4 border-t bg-background", children: [
    /* @__PURE__ */ e.jsx("div", { className: "flex-1 relative", children: /* @__PURE__ */ e.jsx(
      "textarea",
      {
        ref: o,
        value: u,
        onChange: (l) => I(l.target.value),
        onKeyDown: E,
        placeholder: i,
        disabled: m,
        rows: 1,
        className: x(
          "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"
        }
      }
    ) }),
    n ? /* @__PURE__ */ e.jsx(
      "button",
      {
        onClick: p,
        className: x(
          "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__ */ e.jsx(je, { className: "w-4 h-4" })
      }
    ) : /* @__PURE__ */ e.jsx(
      "button",
      {
        onClick: k,
        disabled: !u.trim() || m,
        className: x(
          "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__ */ e.jsx(ye, { className: "w-4 h-4" })
      }
    )
  ] });
}
const Le = 300, Fe = 700, Be = 500;
function Ge() {
  const {
    isDialogOpen: r,
    closeDialog: p,
    openSettings: n,
    messages: m,
    isLoading: i,
    error: u,
    isConfigured: I,
    selectedText: o,
    sendMessage: k,
    regenerateLast: E,
    clearHistory: l,
    stopGeneration: T,
    editMessage: h,
    deleteMessage: M
  } = ee(), N = D(null), b = D(null), L = D(null), v = D(null), w = D(!0), [C, c] = d(Be), [a, j] = d(!1), [y, g] = d({ x: 0, y: 0 }), [A, B] = d(!1), H = D({ x: 0, y: 0, posX: 0, posY: 0 }), O = S((s = "smooth") => {
    N.current && N.current.scrollIntoView({ behavior: s });
  }, []), W = S(() => {
    const s = b.current;
    if (!s) return;
    const { scrollTop: P, scrollHeight: z, clientHeight: R } = s, F = z - P - R < 100;
    w.current = F;
  }, []);
  _(() => {
    if (w.current && N.current) {
      const P = m[m.length - 1]?.isStreaming;
      O(P ? "auto" : "smooth");
    }
  }, [m, O]), _(() => {
    const s = (P) => {
      P.key === "Escape" && r && p();
    };
    return document.addEventListener("keydown", s), () => document.removeEventListener("keydown", s);
  }, [r, p]);
  const K = S((s) => {
    s.preventDefault(), j(!0);
    const P = s.clientY, z = C, R = (Y) => {
      const V = P - Y.clientY, q = Math.min(Fe, Math.max(Le, z + V));
      c(q);
    }, F = () => {
      j(!1), document.removeEventListener("mousemove", R), document.removeEventListener("mouseup", F);
    };
    document.addEventListener("mousemove", R), document.addEventListener("mouseup", F);
  }, [C]), U = S((s) => {
    if (s.target.closest("button")) return;
    s.preventDefault(), B(!0), H.current = {
      x: s.clientX,
      y: s.clientY,
      posX: y.x,
      posY: y.y
    };
    const P = (R) => {
      const F = R.clientX - H.current.x, Y = R.clientY - H.current.y;
      g({
        x: H.current.posX + F,
        y: H.current.posY + Y
      });
    }, z = () => {
      B(!1), document.removeEventListener("mousemove", P), document.removeEventListener("mouseup", z);
    };
    document.addEventListener("mousemove", P), document.addEventListener("mouseup", z);
  }, [y]), Z = S(() => {
    g({ x: 0, y: 0 });
  }, []);
  return !I && r ? /* @__PURE__ */ e.jsx("div", { className: "fixed inset-x-4 bottom-4 z-50 mx-auto max-w-2xl", children: /* @__PURE__ */ e.jsxs(
    "div",
    {
      className: x(
        "bg-background border rounded-xl shadow-2xl",
        "animate-in slide-in-from-bottom-4 duration-300"
      ),
      children: [
        /* @__PURE__ */ e.jsxs("div", { className: "flex items-center justify-between p-4 border-b", children: [
          /* @__PURE__ */ e.jsxs("div", { className: "flex items-center gap-2", children: [
            /* @__PURE__ */ e.jsx($, { className: "w-5 h-5 text-primary" }),
            /* @__PURE__ */ e.jsx("h3", { className: "font-semibold", children: "AI 助手" })
          ] }),
          /* @__PURE__ */ e.jsx(
            "button",
            {
              onClick: p,
              className: "p-1.5 rounded hover:bg-muted transition-colors",
              children: /* @__PURE__ */ e.jsx(G, { className: "w-4 h-4" })
            }
          )
        ] }),
        /* @__PURE__ */ e.jsxs("div", { className: "p-6 text-center", children: [
          /* @__PURE__ */ e.jsx(Ne, { className: "w-12 h-12 mx-auto text-muted-foreground mb-4" }),
          /* @__PURE__ */ e.jsx("h4", { className: "text-lg font-medium mb-2", children: "需要配置 AI" }),
          /* @__PURE__ */ e.jsx("p", { className: "text-muted-foreground mb-4", children: "请先配置 AI 提供商和 API 密钥才能使用此功能" }),
          /* @__PURE__ */ e.jsx(
            "button",
            {
              onClick: () => {
                p(), n();
              },
              className: "px-4 py-2 bg-primary text-primary-foreground rounded-lg hover:bg-primary/90 transition-colors",
              children: "前往设置"
            }
          )
        ] })
      ]
    }
  ) }) : r ? /* @__PURE__ */ e.jsx(
    "div",
    {
      className: "fixed inset-x-4 bottom-4 z-50 mx-auto max-w-2xl",
      style: {
        transform: `translate(${y.x}px, ${y.y}px)`
      },
      children: /* @__PURE__ */ e.jsxs(
        "div",
        {
          ref: L,
          className: x(
            "bg-background border rounded-xl shadow-2xl overflow-hidden",
            "animate-in slide-in-from-bottom-4 duration-300",
            "flex flex-col",
            A && "cursor-grabbing",
            a && "cursor-ns-resize"
          ),
          style: { height: `${C}px`, maxHeight: "70vh" },
          children: [
            /* @__PURE__ */ e.jsxs(
              "div",
              {
                className: "flex items-center justify-between p-4 border-b shrink-0 cursor-grab active:cursor-grabbing",
                onMouseDown: U,
                children: [
                  /* @__PURE__ */ e.jsxs("div", { className: "flex items-center gap-2", children: [
                    /* @__PURE__ */ e.jsx(we, { className: "w-4 h-4 text-muted-foreground" }),
                    /* @__PURE__ */ e.jsx($, { className: "w-5 h-5 text-primary" }),
                    /* @__PURE__ */ e.jsx("h3", { className: "font-semibold", children: "AI 助手" })
                  ] }),
                  /* @__PURE__ */ e.jsxs("div", { className: "flex items-center gap-1", children: [
                    y.x !== 0 || y.y !== 0 ? /* @__PURE__ */ e.jsx(
                      "button",
                      {
                        onClick: Z,
                        className: "p-1.5 rounded hover:bg-muted transition-colors text-muted-foreground hover:text-foreground",
                        title: "重置位置",
                        children: /* @__PURE__ */ e.jsx(G, { className: "w-3 h-3" })
                      }
                    ) : null,
                    m.length > 0 && /* @__PURE__ */ e.jsx(
                      "button",
                      {
                        onClick: l,
                        className: "p-1.5 rounded hover:bg-muted transition-colors text-muted-foreground hover:text-foreground",
                        title: "清除对话",
                        children: /* @__PURE__ */ e.jsx(te, { className: "w-4 h-4" })
                      }
                    ),
                    /* @__PURE__ */ e.jsx(
                      "button",
                      {
                        onClick: n,
                        className: "p-1.5 rounded hover:bg-muted transition-colors text-muted-foreground hover:text-foreground",
                        title: "设置",
                        children: /* @__PURE__ */ e.jsx(ke, { className: "w-4 h-4" })
                      }
                    ),
                    /* @__PURE__ */ e.jsx(
                      "button",
                      {
                        onClick: p,
                        className: "p-1.5 rounded hover:bg-muted transition-colors text-muted-foreground hover:text-foreground",
                        title: "关闭",
                        children: /* @__PURE__ */ e.jsx(G, { className: "w-4 h-4" })
                      }
                    )
                  ] })
                ]
              }
            ),
            o && /* @__PURE__ */ e.jsxs("div", { className: "px-4 py-2 bg-muted/50 border-b shrink-0", children: [
              /* @__PURE__ */ e.jsx("div", { className: "text-xs text-muted-foreground mb-1", children: "选中的内容：" }),
              /* @__PURE__ */ e.jsx("div", { className: "text-sm line-clamp-2", children: o })
            ] }),
            /* @__PURE__ */ e.jsx(
              "div",
              {
                ref: b,
                className: "flex-1 overflow-y-auto",
                onScroll: W,
                children: m.length === 0 ? /* @__PURE__ */ e.jsxs("div", { className: "flex flex-col items-center justify-center h-full text-muted-foreground p-4", children: [
                  /* @__PURE__ */ e.jsx($, { className: "w-12 h-12 mb-4 opacity-50" }),
                  /* @__PURE__ */ e.jsxs("p", { className: "text-center", children: [
                    "你好！我是 AI 助手。",
                    /* @__PURE__ */ e.jsx("br", {}),
                    "有什么我可以帮助你的吗？"
                  ] })
                ] }) : /* @__PURE__ */ e.jsxs("div", { className: "divide-y", children: [
                  m.map((s, P) => /* @__PURE__ */ e.jsx(
                    De,
                    {
                      message: s,
                      onRegenerate: s.role === "assistant" && P === m.length - 1 && !i ? E : void 0,
                      onEdit: s.role === "user" ? (z) => h(s.id, z) : void 0,
                      onDelete: i ? void 0 : () => M(s.id)
                    },
                    s.id
                  )),
                  /* @__PURE__ */ e.jsx("div", { ref: N })
                ] })
              }
            ),
            u && /* @__PURE__ */ e.jsx("div", { className: "px-4 py-2 bg-destructive/10 text-destructive text-sm border-t", children: u }),
            /* @__PURE__ */ e.jsx(
              Re,
              {
                onSend: k,
                onStop: T,
                isLoading: i,
                placeholder: o ? "询问关于选中内容的问题..." : "输入消息..."
              }
            ),
            /* @__PURE__ */ e.jsx(
              "div",
              {
                ref: v,
                className: "absolute top-0 left-1/2 -translate-x-1/2 w-20 h-2 cursor-ns-resize flex items-center justify-center",
                onMouseDown: K,
                children: /* @__PURE__ */ e.jsx("div", { className: "w-8 h-1 rounded-full bg-muted-foreground/30" })
              }
            )
          ]
        }
      )
    }
  ) : null;
}
function _e() {
  const { isSettingsOpen: r, closeSettings: p, config: n, updateConfig: m } = ee(), { toast: i } = ie(), [u, I] = d("openai"), [o, k] = d(J("openai")), [E, l] = d(""), [T, h] = d("providers"), [M, N] = d(!1), [b, L] = d(""), [v, w] = d({
    modelId: "",
    apiKey: "",
    baseUrl: "",
    maxTokens: 4096,
    temperature: 0.7,
    enabled: !0
  }), [C, c] = d(!1), [a, j] = d(!1), [y, g] = d(null), [A, B] = d(!1), [H, O] = d(["openai", "claude", "gemini"]), W = ["openai", "claude", "gemini"], K = (t) => W.includes(t);
  _(() => {
    if (n) {
      const t = n.provider || "openai";
      I(t), k(n.models[t] || J("openai")), l(n.systemPrompt || ""), U();
    }
  }, [n]);
  const U = async () => {
    const t = await le();
    O(t);
  }, Z = (t) => {
    if (!t) return;
    I(t);
    const f = n?.models[t];
    k(f || J(t)), g(null);
  }, s = (t) => {
    k((f) => ({ ...f, ...t })), g(null);
  }, P = async () => {
    j(!0), g(null);
    try {
      const t = await me(u, o);
      g(t), t.success ? i({
        title: "连接成功",
        description: t.message
      }) : i({
        title: "连接失败",
        description: t.message,
        variant: "destructive"
      });
    } catch (t) {
      const f = t instanceof Error ? t.message : "测试失败";
      g({
        success: !1,
        message: f
      }), i({
        title: "连接失败",
        description: f,
        variant: "destructive"
      });
    } finally {
      j(!1);
    }
  }, z = async () => {
    B(!0);
    try {
      await m({
        provider: u,
        systemPrompt: E,
        models: {
          ...n?.models || {},
          [u]: o
        }
      }), i({
        title: "保存成功",
        description: "配置已保存"
      }), p();
    } catch (t) {
      console.error("Failed to save config:", t), i({
        title: "保存失败",
        description: t instanceof Error ? t.message : "未知错误",
        variant: "destructive"
      });
    } finally {
      B(!1);
    }
  }, R = async () => {
    if (!b.trim()) {
      i({
        title: "请输入 Provider 名称",
        variant: "destructive"
      });
      return;
    }
    if (K(b)) {
      i({
        title: "不能使用预定义的 Provider 名称",
        variant: "destructive"
      });
      return;
    }
    if (n?.models[b]) {
      i({
        title: "Provider 名称已存在",
        variant: "destructive"
      });
      return;
    }
    const t = xe(v);
    if (!t.valid) {
      i({
        title: "配置无效",
        description: t.message,
        variant: "destructive"
      });
      return;
    }
    try {
      await m({
        models: {
          ...n?.models || {},
          [b]: v
        }
      }), N(!1), L(""), w({
        modelId: "",
        apiKey: "",
        baseUrl: "",
        maxTokens: 4096,
        temperature: 0.7,
        enabled: !0
      }), await U(), I(b), k(v), i({
        title: "添加成功",
        description: `Provider "${b}" 已添加`
      });
    } catch (f) {
      console.error("Failed to add provider:", f), i({
        title: "添加失败",
        description: f instanceof Error ? f.message : "未知错误",
        variant: "destructive"
      });
    }
  }, F = async (t) => {
    if (K(t)) {
      i({
        title: "不能删除预定义的 Provider",
        variant: "destructive"
      });
      return;
    }
    if (confirm(`确定要删除 "${t}" 吗？`))
      try {
        await ce(t), await U(), u === t && (I("openai"), k(n?.models.openai || J("openai"))), i({
          title: "删除成功",
          description: `Provider "${t}" 已删除`
        });
      } catch (f) {
        console.error("Failed to delete provider:", f), i({
          title: "删除失败",
          description: f instanceof Error ? f.message : "未知错误",
          variant: "destructive"
        });
      }
  }, Y = async () => {
    B(!0);
    try {
      await m({ systemPrompt: E }), i({
        title: "保存成功",
        description: "系统提示词已更新"
      });
    } catch (t) {
      console.error("Failed to save system prompt:", t), i({
        title: "保存失败",
        description: t instanceof Error ? t.message : "未知错误",
        variant: "destructive"
      });
    } finally {
      B(!1);
    }
  };
  if (!r)
    return null;
  const V = de(u), q = ue(u), ae = K(u);
  return /* @__PURE__ */ e.jsxs("div", { className: "fixed inset-0 z-50 flex items-center justify-center", children: [
    /* @__PURE__ */ e.jsx(
      "div",
      {
        className: "absolute inset-0 bg-black/50",
        onClick: p
      }
    ),
    /* @__PURE__ */ e.jsxs(
      "div",
      {
        className: x(
          "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__ */ e.jsxs("div", { className: "flex items-center justify-between p-4 border-b", children: [
            /* @__PURE__ */ e.jsx("h2", { className: "text-lg font-semibold", children: "AI 设置" }),
            /* @__PURE__ */ e.jsx(
              "button",
              {
                onClick: p,
                className: "p-1.5 rounded hover:bg-muted transition-colors",
                children: /* @__PURE__ */ e.jsx(G, { className: "w-4 h-4" })
              }
            )
          ] }),
          /* @__PURE__ */ e.jsxs("div", { className: "flex border-b", children: [
            /* @__PURE__ */ e.jsx(
              "button",
              {
                onClick: () => h("providers"),
                className: x(
                  "flex-1 px-4 py-3 text-sm font-medium transition-colors",
                  T === "providers" ? "border-b-2 border-primary text-primary" : "text-muted-foreground hover:text-foreground"
                ),
                children: "Provider 配置"
              }
            ),
            /* @__PURE__ */ e.jsx(
              "button",
              {
                onClick: () => h("settings"),
                className: x(
                  "flex-1 px-4 py-3 text-sm font-medium transition-colors",
                  T === "settings" ? "border-b-2 border-primary text-primary" : "text-muted-foreground hover:text-foreground"
                ),
                children: "系统设置"
              }
            )
          ] }),
          /* @__PURE__ */ e.jsxs("div", { className: "flex-1 overflow-y-auto", children: [
            T === "providers" && /* @__PURE__ */ e.jsxs("div", { className: "p-4 space-y-6", children: [
              /* @__PURE__ */ e.jsxs("div", { className: "space-y-3", children: [
                /* @__PURE__ */ e.jsxs("div", { className: "flex items-center justify-between", children: [
                  /* @__PURE__ */ e.jsx("label", { className: "text-sm font-medium", children: "选择 Provider" }),
                  /* @__PURE__ */ e.jsxs(
                    "button",
                    {
                      onClick: () => N(!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__ */ e.jsx(ne, { className: "w-4 h-4" }),
                        "添加"
                      ]
                    }
                  )
                ] }),
                /* @__PURE__ */ e.jsx("div", { className: "grid grid-cols-2 gap-2", children: H.map((t) => /* @__PURE__ */ e.jsxs(
                  "div",
                  {
                    className: x(
                      "relative flex items-center justify-between p-3 rounded-lg border transition-colors",
                      u === t ? "bg-primary/10 border-primary" : "bg-background hover:bg-muted border-border"
                    ),
                    children: [
                      /* @__PURE__ */ e.jsxs(
                        "button",
                        {
                          onClick: () => Z(t),
                          className: "flex-1 text-left",
                          children: [
                            /* @__PURE__ */ e.jsx("div", { className: "font-medium text-sm", children: se(t) }),
                            n?.provider === t && /* @__PURE__ */ e.jsxs("div", { className: "text-xs text-primary flex items-center gap-1", children: [
                              /* @__PURE__ */ e.jsx(Ce, { className: "w-3 h-3" }),
                              "当前使用"
                            ] })
                          ]
                        }
                      ),
                      !ae && /* @__PURE__ */ e.jsx(
                        "button",
                        {
                          onClick: () => F(t),
                          className: "p-1.5 rounded hover:bg-muted text-destructive",
                          title: "删除",
                          children: /* @__PURE__ */ e.jsx(te, { className: "w-4 h-4" })
                        }
                      )
                    ]
                  },
                  t
                )) })
              ] }),
              !M && /* @__PURE__ */ e.jsxs("div", { className: "space-y-4", children: [
                /* @__PURE__ */ e.jsxs("h3", { className: "text-sm font-medium", children: [
                  se(u),
                  " 配置"
                ] }),
                /* @__PURE__ */ e.jsxs("div", { className: "space-y-2", children: [
                  /* @__PURE__ */ e.jsxs("div", { className: "flex items-center justify-between", children: [
                    /* @__PURE__ */ e.jsx("label", { className: "text-sm font-medium", children: "API 密钥" }),
                    V.apiKeyUrl && /* @__PURE__ */ e.jsxs(
                      "a",
                      {
                        href: V.apiKeyUrl,
                        target: "_blank",
                        rel: "noopener noreferrer",
                        className: "text-xs text-primary hover:underline flex items-center gap-1",
                        children: [
                          "获取密钥",
                          /* @__PURE__ */ e.jsx(Pe, { className: "w-3 h-3" })
                        ]
                      }
                    )
                  ] }),
                  /* @__PURE__ */ e.jsxs("div", { className: "relative", children: [
                    /* @__PURE__ */ e.jsx(
                      "input",
                      {
                        type: C ? "text" : "password",
                        value: o.apiKey,
                        onChange: (t) => s({ apiKey: t.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__ */ e.jsx(
                      "button",
                      {
                        type: "button",
                        onClick: () => c(!C),
                        className: "absolute right-2 top-1/2 -translate-y-1/2 p-1 text-muted-foreground hover:text-foreground",
                        children: C ? /* @__PURE__ */ e.jsx(Ie, { className: "w-4 h-4" }) : /* @__PURE__ */ e.jsx(Ee, { className: "w-4 h-4" })
                      }
                    )
                  ] })
                ] }),
                /* @__PURE__ */ e.jsxs("div", { className: "space-y-2", children: [
                  /* @__PURE__ */ e.jsx("label", { className: "text-sm font-medium", children: "API 地址" }),
                  /* @__PURE__ */ e.jsx(
                    "input",
                    {
                      type: "text",
                      value: o.baseUrl,
                      onChange: (t) => s({ baseUrl: t.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__ */ e.jsxs("div", { className: "space-y-2", children: [
                  /* @__PURE__ */ e.jsx("label", { className: "text-sm font-medium", children: "模型" }),
                  q.length > 0 ? /* @__PURE__ */ e.jsxs(
                    "select",
                    {
                      value: o.modelId,
                      onChange: (t) => s({ modelId: t.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__ */ e.jsx("option", { value: "", children: "选择模型" }),
                        q.map((t) => /* @__PURE__ */ e.jsx("option", { value: t, children: t }, t))
                      ]
                    }
                  ) : /* @__PURE__ */ e.jsx(
                    "input",
                    {
                      type: "text",
                      value: o.modelId,
                      onChange: (t) => s({ modelId: t.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__ */ e.jsxs("details", { className: "group", children: [
                  /* @__PURE__ */ e.jsxs("summary", { className: "cursor-pointer text-sm font-medium flex items-center gap-1", children: [
                    /* @__PURE__ */ e.jsx("span", { children: "高级设置" }),
                    /* @__PURE__ */ e.jsx("span", { className: "text-muted-foreground group-open:rotate-90 transition-transform", children: "▶" })
                  ] }),
                  /* @__PURE__ */ e.jsxs("div", { className: "mt-4 space-y-4 pl-2", children: [
                    /* @__PURE__ */ e.jsxs("div", { className: "space-y-2", children: [
                      /* @__PURE__ */ e.jsxs("div", { className: "flex items-center justify-between", children: [
                        /* @__PURE__ */ e.jsx("label", { className: "text-sm", children: "温度 (Temperature)" }),
                        /* @__PURE__ */ e.jsx("span", { className: "text-sm text-muted-foreground", children: o.temperature.toFixed(1) })
                      ] }),
                      /* @__PURE__ */ e.jsx(
                        "input",
                        {
                          type: "range",
                          min: "0",
                          max: "2",
                          step: "0.1",
                          value: o.temperature,
                          onChange: (t) => s({ temperature: parseFloat(t.target.value) }),
                          className: "w-full"
                        }
                      )
                    ] }),
                    /* @__PURE__ */ e.jsxs("div", { className: "space-y-2", children: [
                      /* @__PURE__ */ e.jsx("label", { className: "text-sm", children: "最大输出长度 (Max Tokens)" }),
                      /* @__PURE__ */ e.jsx(
                        "input",
                        {
                          type: "number",
                          min: "1",
                          max: "128000",
                          value: o.maxTokens,
                          onChange: (t) => s({ maxTokens: parseInt(t.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"
                        }
                      )
                    ] })
                  ] })
                ] }),
                y && /* @__PURE__ */ e.jsxs(
                  "div",
                  {
                    className: x(
                      "flex items-center gap-2 p-3 rounded-lg text-sm",
                      y.success ? "bg-green-500/10 text-green-600 dark:text-green-400" : "bg-destructive/10 text-destructive"
                    ),
                    children: [
                      y.success ? /* @__PURE__ */ e.jsx(oe, { className: "w-4 h-4 shrink-0" }) : /* @__PURE__ */ e.jsx(Se, { className: "w-4 h-4 shrink-0" }),
                      /* @__PURE__ */ e.jsx("span", { children: y.message })
                    ]
                  }
                ),
                /* @__PURE__ */ e.jsxs("div", { className: "flex items-center gap-2 pt-4 border-t", children: [
                  /* @__PURE__ */ e.jsx(
                    "button",
                    {
                      onClick: P,
                      disabled: !o.apiKey || !o.modelId || a,
                      className: x(
                        "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: a ? /* @__PURE__ */ e.jsxs(e.Fragment, { children: [
                        /* @__PURE__ */ e.jsx(X, { className: "w-4 h-4 animate-spin" }),
                        "测试中..."
                      ] }) : "测试连接"
                    }
                  ),
                  /* @__PURE__ */ e.jsx(
                    "button",
                    {
                      onClick: z,
                      disabled: A,
                      className: x(
                        "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: A ? /* @__PURE__ */ e.jsxs(e.Fragment, { children: [
                        /* @__PURE__ */ e.jsx(X, { className: "w-4 h-4 animate-spin" }),
                        "保存中..."
                      ] }) : "保存设置"
                    }
                  )
                ] })
              ] }),
              M && /* @__PURE__ */ e.jsxs("div", { className: "space-y-4", children: [
                /* @__PURE__ */ e.jsx("h3", { className: "text-sm font-medium", children: "添加自定义 Provider" }),
                /* @__PURE__ */ e.jsxs("div", { className: "space-y-2", children: [
                  /* @__PURE__ */ e.jsx("label", { className: "text-sm font-medium", children: "Provider 名称" }),
                  /* @__PURE__ */ e.jsx(
                    "input",
                    {
                      type: "text",
                      value: b,
                      onChange: (t) => L(t.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__ */ e.jsx("p", { className: "text-xs text-muted-foreground", children: "名称将用于标识此 Provider，不能与预定义 Provider 重复" })
                ] }),
                /* @__PURE__ */ e.jsxs("div", { className: "space-y-2", children: [
                  /* @__PURE__ */ e.jsx("label", { className: "text-sm font-medium", children: "API 密钥" }),
                  /* @__PURE__ */ e.jsx(
                    "input",
                    {
                      type: C ? "text" : "password",
                      value: v.apiKey,
                      onChange: (t) => w((f) => ({ ...f, apiKey: t.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__ */ e.jsxs("div", { className: "space-y-2", children: [
                  /* @__PURE__ */ e.jsx("label", { className: "text-sm font-medium", children: "API 地址" }),
                  /* @__PURE__ */ e.jsx(
                    "input",
                    {
                      type: "text",
                      value: v.baseUrl,
                      onChange: (t) => w((f) => ({ ...f, baseUrl: t.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__ */ e.jsxs("div", { className: "space-y-2", children: [
                  /* @__PURE__ */ e.jsx("label", { className: "text-sm font-medium", children: "模型" }),
                  /* @__PURE__ */ e.jsx(
                    "input",
                    {
                      type: "text",
                      value: v.modelId,
                      onChange: (t) => w((f) => ({ ...f, modelId: t.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__ */ e.jsxs("div", { className: "flex items-center gap-2 pt-4 border-t", children: [
                  /* @__PURE__ */ e.jsx(
                    "button",
                    {
                      onClick: () => N(!1),
                      className: "px-4 py-2 rounded-lg text-sm border border-border hover:bg-muted transition-colors",
                      children: "取消"
                    }
                  ),
                  /* @__PURE__ */ e.jsx(
                    "button",
                    {
                      onClick: R,
                      disabled: A,
                      className: x(
                        "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: A ? /* @__PURE__ */ e.jsxs(e.Fragment, { children: [
                        /* @__PURE__ */ e.jsx(X, { className: "w-4 h-4 animate-spin" }),
                        "添加中..."
                      ] }) : /* @__PURE__ */ e.jsxs(e.Fragment, { children: [
                        /* @__PURE__ */ e.jsx(ne, { className: "w-4 h-4" }),
                        "添加"
                      ] })
                    }
                  )
                ] })
              ] })
            ] }),
            T === "settings" && /* @__PURE__ */ e.jsxs("div", { className: "p-4 space-y-6", children: [
              /* @__PURE__ */ e.jsxs("div", { className: "space-y-2", children: [
                /* @__PURE__ */ e.jsx("label", { className: "text-sm font-medium", children: "系统提示词" }),
                /* @__PURE__ */ e.jsx(
                  "textarea",
                  {
                    value: E,
                    onChange: (t) => l(t.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__ */ e.jsx("p", { className: "text-xs text-muted-foreground", children: "系统提示词用于定义 AI 助手的行为和角色" })
              ] }),
              /* @__PURE__ */ e.jsx("div", { className: "flex items-center gap-2 pt-4 border-t", children: /* @__PURE__ */ e.jsx(
                "button",
                {
                  onClick: Y,
                  disabled: A,
                  className: x(
                    "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: A ? /* @__PURE__ */ e.jsxs(e.Fragment, { children: [
                    /* @__PURE__ */ e.jsx(X, { className: "w-4 h-4 animate-spin" }),
                    "保存中..."
                  ] }) : /* @__PURE__ */ e.jsxs(e.Fragment, { children: [
                    /* @__PURE__ */ e.jsx(Ae, { className: "w-4 h-4" }),
                    "保存提示词"
                  ] })
                }
              ) })
            ] })
          ] })
        ]
      }
    )
  ] });
}
export {
  $e as A,
  Ge as a,
  _e as b,
  Re as c,
  De as d
};
