import { h as e } from "./shiki-highlighter-BmQSSJpS.js";
import { t } from "./utils-Ct96Mtjw.js";
import { n } from "./AIProvider-C1_b1iKH.js";
import { useCallback as r, useEffect as i, useRef as a, useState as o } from "react";
import { Bot as s } from "lucide-react";
import { jsx as c } from "react/jsx-runtime";
//#region src/components/ai/AISelectionTrigger.tsx
var l = /* @__PURE__ */ e({ AISelectionTrigger: () => d });
function u(e, t) {
	let n = a(null);
	return r((...r) => {
		n.current && clearTimeout(n.current), n.current = setTimeout(() => e(...r), t);
	}, [e, t]);
}
function d() {
	let { openDialog: e, isConfigured: l, isDialogOpen: d, openSettings: f } = n(), [p, m] = o(""), [h, g] = o(!1), [_, v] = o(!1), [y, b] = o({
		top: 0,
		left: 0
	}), x = a(null), S = a(null), C = r(() => {
		if (d) {
			g(!1);
			return;
		}
		let e = window.getSelection(), t = e?.toString().trim() || "";
		if (t.length < 5) {
			m(""), g(!1);
			return;
		}
		if (m(t), e && e.rangeCount > 0) {
			let t = e.getRangeAt(0).getBoundingClientRect();
			b({
				top: t.bottom + window.scrollY + 8,
				left: t.right + window.scrollX - 40
			}), v(!0), g(!0);
		}
	}, [d]), w = u(C, 150);
	i(() => {
		let e = !1, t = () => {
			e = !0, x.current &&= (clearTimeout(x.current), null);
		}, n = () => {
			e = !1, setTimeout(() => {
				C();
			}, 10);
		}, r = () => {
			e || w();
		}, i = (e) => {
			S.current && !S.current.contains(e.target) && (x.current = setTimeout(() => {
				g(!1);
			}, 200));
		};
		return document.addEventListener("mousedown", t), document.addEventListener("mouseup", n), document.addEventListener("selectionchange", r), document.addEventListener("click", i), () => {
			document.removeEventListener("mousedown", t), document.removeEventListener("mouseup", n), document.removeEventListener("selectionchange", r), document.removeEventListener("click", i), x.current && clearTimeout(x.current);
		};
	}, [C, w]);
	let T = r(() => {
		if (p) {
			if (!l) {
				f();
				return;
			}
			e({
				selectedText: p,
				pageTitle: document.title,
				pageUrl: window.location.href
			}), window.getSelection()?.removeAllRanges(), g(!1), m("");
		}
	}, [
		p,
		l,
		e,
		f
	]);
	return !h || d ? null : /* @__PURE__ */ c("button", {
		ref: S,
		onClick: T,
		className: t("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", _ && "animate-in zoom-in-50 duration-200"),
		style: {
			top: y.top,
			left: y.left
		},
		title: "向AI询问选中内容",
		children: /* @__PURE__ */ c(s, { className: "w-5 h-5" })
	});
}
//#endregion
export { l as n, d as t };
