"use client";
import { cn } from "../../lib/utils.js";
import "../ui/button.js";
import "../ui/dropdown-menu.js";
import "../ui/select.js";
import { nanoid } from "../../node_modules/nanoid/index.browser.js";
import "../ui/command.js";
import "../ui/hover-card.js";
import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupTextarea } from "../ui/input-group.js";
import { c } from "react/compiler-runtime";
import { Fragment, createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
import { CornerDownLeftIcon, Loader2Icon, SquareIcon, XIcon } from "lucide-react";
var PromptInputController = createContext(null), ProviderAttachmentsContext = createContext(null), useOptionalPromptInputController = () => useContext(PromptInputController), useOptionalProviderAttachments = () => useContext(ProviderAttachmentsContext), LocalAttachmentsContext = createContext(null);
const usePromptInputAttachments = () => {
	let e = useOptionalProviderAttachments(), t = useContext(LocalAttachmentsContext), n = e ?? t;
	if (!n) throw Error("usePromptInputAttachments must be used within a PromptInput or PromptInputProvider");
	return n;
}, PromptInput = (r) => {
	let i = c(94), a, o, s, l, u, f, _, v, y, b, x;
	i[0] === r ? (a = i[1], o = i[2], s = i[3], l = i[4], u = i[5], f = i[6], _ = i[7], v = i[8], y = i[9], b = i[10], x = i[11]) : ({className: s, accept: a, multiple: _, globalDrop: l, syncHiddenInput: x, maxFiles: f, maxFileSize: u, onError: v, onSubmit: y, children: o, ...b} = r, i[0] = r, i[1] = a, i[2] = o, i[3] = s, i[4] = l, i[5] = u, i[6] = f, i[7] = _, i[8] = v, i[9] = y, i[10] = b, i[11] = x);
	let S = useOptionalPromptInputController(), C = !!S, T = useRef(null), le = useRef(null), ue = useRef(null), de, fe;
	i[12] === Symbol.for("react.memo_cache_sentinel") ? (de = () => {
		let e = le.current?.closest("form");
		e instanceof HTMLFormElement && (ue.current = e);
	}, fe = [], i[12] = de, i[13] = fe) : (de = i[12], fe = i[13]), useEffect(de, fe);
	let pe;
	i[14] === Symbol.for("react.memo_cache_sentinel") ? (pe = [], i[14] = pe) : pe = i[14];
	let [me, he] = useState(pe), E = C ? S.attachments.files : me, D;
	i[15] === Symbol.for("react.memo_cache_sentinel") ? (D = () => {
		T.current?.click();
	}, i[15] = D) : D = i[15];
	let ge = D, O;
	i[16] === a ? O = i[17] : (O = (e) => !a || a.trim() === "" ? !0 : a.includes("image/*") ? e.type.startsWith("image/") : !0, i[16] = a, i[17] = O);
	let _e = O, k;
	i[18] !== _e || i[19] !== u || i[20] !== f || i[21] !== v ? (k = (e) => {
		let n = Array.from(e), r = n.filter((e) => _e(e));
		if (n.length && r.length === 0) {
			v?.({
				code: "accept",
				message: "No files match the accepted types."
			});
			return;
		}
		let i = r.filter((e) => u ? e.size <= u : !0);
		if (r.length > 0 && i.length === 0) {
			v?.({
				code: "max_file_size",
				message: "All files exceed the maximum size."
			});
			return;
		}
		he((e) => {
			let n = typeof f == "number" ? Math.max(0, f - e.length) : void 0, r = typeof n == "number" ? i.slice(0, n) : i;
			typeof n == "number" && i.length > n && v?.({
				code: "max_files",
				message: "Too many files. Some were not added."
			});
			let a = [];
			for (let e of r) a.push({
				id: nanoid(),
				type: "file",
				url: URL.createObjectURL(e),
				mediaType: e.type,
				filename: e.name
			});
			return e.concat(a);
		});
	}, i[18] = _e, i[19] = u, i[20] = f, i[21] = v, i[22] = k) : k = i[22];
	let ve = k, A;
	i[23] !== ve || i[24] !== S || i[25] !== C ? (A = C ? (e) => S.attachments.add(e) : ve, i[23] = ve, i[24] = S, i[25] = C, i[26] = A) : A = i[26];
	let j = A, M;
	i[27] !== S || i[28] !== C ? (M = C ? (e) => S.attachments.remove(e) : (e) => he((t) => {
		let n = t.find((t) => t.id === e);
		return n?.url && URL.revokeObjectURL(n.url), t.filter((t) => t.id !== e);
	}), i[27] = S, i[28] = C, i[29] = M) : M = i[29];
	let ye = M, N;
	i[30] !== S || i[31] !== C ? (N = C ? () => S.attachments.clear() : () => he(_temp4), i[30] = S, i[31] = C, i[32] = N) : N = i[32];
	let P = N, F;
	i[33] !== S || i[34] !== C ? (F = C ? () => S.attachments.openFileDialog() : ge, i[33] = S, i[34] = C, i[35] = F) : F = i[35];
	let be = F, I, L;
	i[36] !== S || i[37] !== C ? (I = () => {
		C && S.__registerFileInput(T, () => T.current?.click());
	}, L = [C, S], i[36] = S, i[37] = C, i[38] = I, i[39] = L) : (I = i[38], L = i[39]), useEffect(I, L);
	let R;
	i[40] !== E.length || i[41] !== x ? (R = () => {
		x && T.current && E.length === 0 && (T.current.value = "");
	}, i[40] = E.length, i[41] = x, i[42] = R) : R = i[42];
	let z;
	i[43] !== E || i[44] !== x ? (z = [E, x], i[43] = E, i[44] = x, i[45] = z) : z = i[45], useEffect(R, z);
	let B, V;
	i[46] === j ? (B = i[47], V = i[48]) : (B = () => {
		let e = ue.current;
		if (!e) return;
		let t = _temp5, n = (e) => {
			e.dataTransfer?.types?.includes("Files") && e.preventDefault(), e.dataTransfer?.files && e.dataTransfer.files.length > 0 && j(e.dataTransfer.files);
		};
		return e.addEventListener("dragover", t), e.addEventListener("drop", n), () => {
			e.removeEventListener("dragover", t), e.removeEventListener("drop", n);
		};
	}, V = [j], i[46] = j, i[47] = B, i[48] = V), useEffect(B, V);
	let H, U;
	i[49] !== j || i[50] !== l ? (H = () => {
		if (!l) return;
		let e = _temp6, t = (e) => {
			e.dataTransfer?.types?.includes("Files") && e.preventDefault(), e.dataTransfer?.files && e.dataTransfer.files.length > 0 && j(e.dataTransfer.files);
		};
		return document.addEventListener("dragover", e), document.addEventListener("drop", t), () => {
			document.removeEventListener("dragover", e), document.removeEventListener("drop", t);
		};
	}, U = [j, l], i[49] = j, i[50] = l, i[51] = H, i[52] = U) : (H = i[51], U = i[52]), useEffect(H, U);
	let W, G;
	i[53] !== E || i[54] !== C ? (W = () => () => {
		if (!C) for (let e of E) e.url && URL.revokeObjectURL(e.url);
	}, G = [C, E], i[53] = E, i[54] = C, i[55] = W, i[56] = G) : (W = i[55], G = i[56]), useEffect(W, G);
	let K;
	i[57] === j ? K = i[58] : (K = (e) => {
		e.currentTarget.files && j(e.currentTarget.files);
	}, i[57] = j, i[58] = K);
	let xe = K, Se = _temp7, q;
	i[59] === E ? q = i[60] : (q = E.map(_temp8), i[59] = E, i[60] = q);
	let Ce;
	i[61] !== j || i[62] !== P || i[63] !== be || i[64] !== ye || i[65] !== q ? (Ce = {
		files: q,
		add: j,
		remove: ye,
		clear: P,
		openFileDialog: be,
		fileInputRef: T
	}, i[61] = j, i[62] = P, i[63] = be, i[64] = ye, i[65] = q, i[66] = Ce) : Ce = i[66];
	let we = Ce, Te;
	i[67] !== P || i[68] !== S || i[69] !== E || i[70] !== y || i[71] !== C ? (Te = (e) => {
		e.preventDefault();
		let t = e.currentTarget, n = C ? S.textInput.value : (() => new FormData(t).get("message") || "")();
		C || t.reset(), Promise.all(E.map(async (e) => {
			let { id: t, ...n } = e;
			return n.url && n.url.startsWith("blob:") ? {
				...n,
				url: await Se(n.url)
			} : n;
		})).then((t) => {
			try {
				let r = y({
					text: n,
					files: t
				}, e);
				r instanceof Promise ? r.then(() => {
					P(), C && S.textInput.clear();
				}).catch(_temp9) : (P(), C && S.textInput.clear());
			} catch {}
		});
	}, i[67] = P, i[68] = S, i[69] = E, i[70] = y, i[71] = C, i[72] = Te) : Te = i[72];
	let Ee = Te, J;
	i[73] === Symbol.for("react.memo_cache_sentinel") ? (J = /* @__PURE__ */ jsx("span", {
		"aria-hidden": "true",
		className: "hidden",
		ref: le
	}), i[73] = J) : J = i[73];
	let Y;
	i[74] !== a || i[75] !== xe || i[76] !== _ ? (Y = /* @__PURE__ */ jsx("input", {
		accept: a,
		"aria-label": "Upload files",
		className: "hidden",
		multiple: _,
		onChange: xe,
		ref: T,
		title: "Upload files",
		type: "file"
	}), i[74] = a, i[75] = xe, i[76] = _, i[77] = Y) : Y = i[77];
	let X;
	i[78] === s ? X = i[79] : (X = cn("w-full", s), i[78] = s, i[79] = X);
	let Z;
	i[80] === o ? Z = i[81] : (Z = /* @__PURE__ */ jsx(InputGroup, {
		className: "overflow-hidden",
		children: o
	}), i[80] = o, i[81] = Z);
	let Q;
	i[82] !== Ee || i[83] !== b || i[84] !== X || i[85] !== Z ? (Q = /* @__PURE__ */ jsx("form", {
		className: X,
		onSubmit: Ee,
		...b,
		children: Z
	}), i[82] = Ee, i[83] = b, i[84] = X, i[85] = Z, i[86] = Q) : Q = i[86];
	let De;
	i[87] !== Y || i[88] !== Q ? (De = /* @__PURE__ */ jsxs(Fragment$1, { children: [
		J,
		Y,
		Q
	] }), i[87] = Y, i[88] = Q, i[89] = De) : De = i[89];
	let Oe = De, $;
	return i[90] !== we || i[91] !== Oe || i[92] !== C ? ($ = C ? Oe : /* @__PURE__ */ jsx(LocalAttachmentsContext.Provider, {
		value: we,
		children: Oe
	}), i[90] = we, i[91] = Oe, i[92] = C, i[93] = $) : $ = i[93], $;
}, PromptInputBody = (t) => {
	let n = c(8), r, i;
	n[0] === t ? (r = n[1], i = n[2]) : ({className: r, ...i} = t, n[0] = t, n[1] = r, n[2] = i);
	let a;
	n[3] === r ? a = n[4] : (a = cn("contents", r), n[3] = r, n[4] = a);
	let o;
	return n[5] !== i || n[6] !== a ? (o = /* @__PURE__ */ jsx("div", {
		className: a,
		...i
	}), n[5] = i, n[6] = a, n[7] = o) : o = n[7], o;
}, PromptInputTextarea = (t) => {
	let n = c(24), r, i, o, s;
	n[0] === t ? (r = n[1], i = n[2], o = n[3], s = n[4]) : ({onChange: i, className: r, placeholder: s, ...o} = t, n[0] = t, n[1] = r, n[2] = i, n[3] = o, n[4] = s);
	let l = s === void 0 ? "What would you like to know?" : s, u = useOptionalPromptInputController(), d = usePromptInputAttachments(), [f, p] = useState(!1), h;
	n[5] !== d || n[6] !== f ? (h = (e) => {
		if (e.key === "Enter") {
			if (f || e.nativeEvent.isComposing || e.shiftKey) return;
			e.preventDefault();
			let t = e.currentTarget.form;
			if (t?.querySelector("button[type=\"submit\"]")?.disabled) return;
			t?.requestSubmit();
		}
		if (e.key === "Backspace" && e.currentTarget.value === "" && d.files.length > 0) {
			e.preventDefault();
			let t = d.files.at(-1);
			t && d.remove(t.id);
		}
	}, n[5] = d, n[6] = f, n[7] = h) : h = n[7];
	let g = h, _;
	n[8] === d ? _ = n[9] : (_ = (e) => {
		let t = e.clipboardData?.items;
		if (!t) return;
		let n = [];
		for (let e of t) if (e.kind === "file") {
			let t = e.getAsFile();
			t && n.push(t);
		}
		n.length > 0 && (e.preventDefault(), d.add(n));
	}, n[8] = d, n[9] = _);
	let v = _, y;
	n[10] !== u || n[11] !== i ? (y = u ? {
		value: u.textInput.value,
		onChange: (e) => {
			u.textInput.setInput(e.currentTarget.value), i?.(e);
		}
	} : { onChange: i }, n[10] = u, n[11] = i, n[12] = y) : y = n[12];
	let b = y, x;
	n[13] === r ? x = n[14] : (x = cn("field-sizing-content max-h-48 min-h-16 text-base md:text-base", r), n[13] = r, n[14] = x);
	let S, C;
	n[15] === Symbol.for("react.memo_cache_sentinel") ? (S = () => p(!1), C = () => p(!0), n[15] = S, n[16] = C) : (S = n[15], C = n[16]);
	let w;
	return n[17] !== b || n[18] !== g || n[19] !== v || n[20] !== l || n[21] !== o || n[22] !== x ? (w = /* @__PURE__ */ jsx(InputGroupTextarea, {
		className: x,
		name: "message",
		onCompositionEnd: S,
		onCompositionStart: C,
		onKeyDown: g,
		onPaste: v,
		placeholder: l,
		...o,
		...b
	}), n[17] = b, n[18] = g, n[19] = v, n[20] = l, n[21] = o, n[22] = x, n[23] = w) : w = n[23], w;
}, PromptInputFooter = (t) => {
	let n = c(8), i, a;
	n[0] === t ? (i = n[1], a = n[2]) : ({className: i, ...a} = t, n[0] = t, n[1] = i, n[2] = a);
	let o;
	n[3] === i ? o = n[4] : (o = cn("justify-between gap-1", i), n[3] = i, n[4] = o);
	let s;
	return n[5] !== a || n[6] !== o ? (s = /* @__PURE__ */ jsx(InputGroupAddon, {
		align: "block-end",
		className: o,
		...a
	}), n[5] = a, n[6] = o, n[7] = s) : s = n[7], s;
}, PromptInputSubmit = (t) => {
	let n = c(19), r, a, o, s, l, u;
	n[0] === t ? (r = n[1], a = n[2], o = n[3], s = n[4], l = n[5], u = n[6]) : ({className: a, variant: l, size: u, status: s, children: r, ...o} = t, n[0] = t, n[1] = r, n[2] = a, n[3] = o, n[4] = s, n[5] = l, n[6] = u);
	let d = l === void 0 ? "default" : l, f = u === void 0 ? "icon-sm" : u, p;
	n[7] === Symbol.for("react.memo_cache_sentinel") ? (p = /* @__PURE__ */ jsx(CornerDownLeftIcon, { className: "size-4" }), n[7] = p) : p = n[7];
	let m = p;
	if (s === "submitted") {
		let e;
		n[8] === Symbol.for("react.memo_cache_sentinel") ? (e = /* @__PURE__ */ jsx(Loader2Icon, { className: "size-4 animate-spin" }), n[8] = e) : e = n[8], m = e;
	} else if (s === "streaming") {
		let e;
		n[9] === Symbol.for("react.memo_cache_sentinel") ? (e = /* @__PURE__ */ jsx(SquareIcon, { className: "size-4" }), n[9] = e) : e = n[9], m = e;
	} else if (s === "error") {
		let e;
		n[10] === Symbol.for("react.memo_cache_sentinel") ? (e = /* @__PURE__ */ jsx(XIcon, { className: "size-4" }), n[10] = e) : e = n[10], m = e;
	}
	let h;
	n[11] === a ? h = n[12] : (h = cn(a), n[11] = a, n[12] = h);
	let g = r ?? m, x;
	return n[13] !== o || n[14] !== f || n[15] !== h || n[16] !== g || n[17] !== d ? (x = /* @__PURE__ */ jsx(InputGroupButton, {
		"aria-label": "Submit",
		className: h,
		size: f,
		type: "submit",
		variant: d,
		...o,
		children: g
	}), n[13] = o, n[14] = f, n[15] = h, n[16] = g, n[17] = d, n[18] = x) : x = n[18], x;
};
function _temp4(e) {
	for (let t of e) t.url && URL.revokeObjectURL(t.url);
	return [];
}
function _temp5(e) {
	e.dataTransfer?.types?.includes("Files") && e.preventDefault();
}
function _temp6(e) {
	e.dataTransfer?.types?.includes("Files") && e.preventDefault();
}
async function _temp7(e) {
	let t = await (await fetch(e)).blob();
	return new Promise((e, n) => {
		let r = new FileReader();
		r.onloadend = () => e(r.result), r.onerror = n, r.readAsDataURL(t);
	});
}
function _temp8(e) {
	return {
		...e,
		id: e.id
	};
}
function _temp9() {}
export { PromptInput, PromptInputBody, PromptInputFooter, PromptInputSubmit, PromptInputTextarea };
