"use client";
import { cn } from "../../lib/utils.js";
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "../ui/collapsible.js";
import { _r } from "../../node_modules/streamdown/dist/chunk-RLXIAIE6.js";
import "../../node_modules/streamdown/dist/index.js";
import { Shimmer } from "./shimmer.js";
import { c } from "react/compiler-runtime";
import { createContext, memo, useContext, useEffect, useState } from "react";
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
import { BrainIcon, ChevronDownIcon } from "lucide-react";
import { useControllableState } from "@radix-ui/react-use-controllable-state";
var ReasoningContext = createContext(null), useReasoning = () => {
	let t = useContext(ReasoningContext);
	if (!t) throw Error("Reasoning components must be used within Reasoning");
	return t;
}, AUTO_CLOSE_DELAY = 1e3, MS_IN_S = 1e3;
const Reasoning = memo((o) => {
	let s = c(45), l, u, d, f, p, g, _, v;
	s[0] === o ? (l = s[1], u = s[2], d = s[3], f = s[4], p = s[5], g = s[6], _ = s[7], v = s[8]) : ({className: u, isStreaming: _, open: p, defaultOpen: v, onOpenChange: f, duration: d, children: l, ...g} = o, s[0] = o, s[1] = l, s[2] = u, s[3] = d, s[4] = f, s[5] = p, s[6] = g, s[7] = _, s[8] = v);
	let y = _ === void 0 ? !1 : _, S = v === void 0 ? !0 : v, C;
	s[9] !== S || s[10] !== f || s[11] !== p ? (C = {
		prop: p,
		defaultProp: S,
		onChange: f
	}, s[9] = S, s[10] = f, s[11] = p, s[12] = C) : C = s[12];
	let [w, T] = useControllableState(C), E;
	s[13] === d ? E = s[14] : (E = {
		prop: d,
		defaultProp: void 0
	}, s[13] = d, s[14] = E);
	let [D, O] = useControllableState(E), [k, A] = useState(!1), [j, M] = useState(null), N, P;
	s[15] !== y || s[16] !== O || s[17] !== j ? (N = () => {
		y ? j === null && M(Date.now()) : j !== null && (O(Math.ceil((Date.now() - j) / MS_IN_S)), M(null));
	}, P = [
		y,
		j,
		O
	], s[15] = y, s[16] = O, s[17] = j, s[18] = N, s[19] = P) : (N = s[18], P = s[19]), useEffect(N, P);
	let F, I;
	s[20] !== S || s[21] !== k || s[22] !== w || s[23] !== y || s[24] !== T ? (F = () => {
		if (S && !y && w && !k) {
			let t = setTimeout(() => {
				T(!1), A(!0);
			}, AUTO_CLOSE_DELAY);
			return () => clearTimeout(t);
		}
	}, I = [
		y,
		w,
		S,
		T,
		k
	], s[20] = S, s[21] = k, s[22] = w, s[23] = y, s[24] = T, s[25] = F, s[26] = I) : (F = s[25], I = s[26]), useEffect(F, I);
	let L;
	s[27] === T ? L = s[28] : (L = (t) => {
		T(t);
	}, s[27] = T, s[28] = L);
	let R = L, z;
	s[29] !== D || s[30] !== w || s[31] !== y || s[32] !== T ? (z = {
		isStreaming: y,
		isOpen: w,
		setIsOpen: T,
		duration: D
	}, s[29] = D, s[30] = w, s[31] = y, s[32] = T, s[33] = z) : z = s[33];
	let B;
	s[34] === u ? B = s[35] : (B = cn("not-prose mb-4", u), s[34] = u, s[35] = B);
	let V;
	s[36] !== l || s[37] !== R || s[38] !== w || s[39] !== g || s[40] !== B ? (V = /* @__PURE__ */ jsx(Collapsible, {
		className: B,
		onOpenChange: R,
		open: w,
		...g,
		children: l
	}), s[36] = l, s[37] = R, s[38] = w, s[39] = g, s[40] = B, s[41] = V) : V = s[41];
	let H;
	return s[42] !== z || s[43] !== V ? (H = /* @__PURE__ */ jsx(ReasoningContext.Provider, {
		value: z,
		children: V
	}), s[42] = z, s[43] = V, s[44] = H) : H = s[44], H;
});
var getThinkingMessage = (t, a) => t || a === 0 ? /* @__PURE__ */ jsx(Shimmer, {
	duration: 1,
	children: "Thinking..."
}) : a === void 0 ? /* @__PURE__ */ jsx("p", { children: "Thought for a few seconds" }) : /* @__PURE__ */ jsxs("p", { children: [
	"Thought for ",
	a,
	" seconds"
] });
const ReasoningTrigger = memo((a) => {
	let o = c(15), l, u, d;
	o[0] === a ? (l = o[1], u = o[2], d = o[3]) : ({className: u, children: l, ...d} = a, o[0] = a, o[1] = l, o[2] = u, o[3] = d);
	let { isStreaming: f, isOpen: p, duration: m } = useReasoning(), h;
	o[4] === u ? h = o[5] : (h = cn("text-muted-foreground hover:text-foreground flex w-full items-center gap-2 text-sm transition-colors", u), o[4] = u, o[5] = h);
	let b;
	o[6] !== l || o[7] !== m || o[8] !== p || o[9] !== f ? (b = l ?? /* @__PURE__ */ jsxs(Fragment$1, { children: [
		/* @__PURE__ */ jsx(BrainIcon, { className: "size-4" }),
		getThinkingMessage(f, m),
		/* @__PURE__ */ jsx(ChevronDownIcon, { className: cn("size-4 transition-transform", p ? "rotate-180" : "rotate-0") })
	] }), o[6] = l, o[7] = m, o[8] = p, o[9] = f, o[10] = b) : b = o[10];
	let x;
	return o[11] !== d || o[12] !== h || o[13] !== b ? (x = /* @__PURE__ */ jsx(CollapsibleTrigger, {
		className: h,
		...d,
		children: b
	}), o[11] = d, o[12] = h, o[13] = b, o[14] = x) : x = o[14], x;
}), ReasoningContent = memo((a) => {
	let s = c(13), u, d, f;
	s[0] === a ? (u = s[1], d = s[2], f = s[3]) : ({className: d, children: u, ...f} = a, s[0] = a, s[1] = u, s[2] = d, s[3] = f);
	let p;
	s[4] === d ? p = s[5] : (p = cn("mt-4 text-sm", "data-[state=closed]:fade-out-0 data-[state=closed]:slide-out-to-top-2 data-[state=open]:slide-in-from-top-2 text-muted-foreground data-[state=closed]:animate-out data-[state=open]:animate-in outline-none", d), s[4] = d, s[5] = p);
	let m;
	s[6] !== u || s[7] !== f ? (m = /* @__PURE__ */ jsx(_r, {
		...f,
		children: u
	}), s[6] = u, s[7] = f, s[8] = m) : m = s[8];
	let h;
	return s[9] !== f || s[10] !== p || s[11] !== m ? (h = /* @__PURE__ */ jsx(CollapsibleContent, {
		className: p,
		...f,
		children: m
	}), s[9] = f, s[10] = p, s[11] = m, s[12] = h) : h = s[12], h;
});
Reasoning.displayName = "Reasoning", ReasoningTrigger.displayName = "ReasoningTrigger", ReasoningContent.displayName = "ReasoningContent";
export { Reasoning, ReasoningContent, ReasoningTrigger };
