"use client";
import { cn as e } from "../../lib/utils.js";
import "../ui/button.js";
import "../ui/dropdown-menu.js";
import "../ui/select.js";
import { nanoid as t } from "../../node_modules/nanoid/index.browser.js";
import "../ui/command.js";
import "../ui/hover-card.js";
import { InputGroup as n, InputGroupAddon as r, InputGroupButton as i, InputGroupTextarea as a } from "../ui/input-group.js";
import { createContext as o, useCallback as s, useContext as c, useEffect as l, useMemo as u, useRef as d, useState as f } from "react";
import { Fragment as p, jsx as m, jsxs as h } from "react/jsx-runtime";
import { CornerDownLeftIcon as g, Loader2Icon as _, SquareIcon as v, XIcon as y } from "lucide-react";
//#region src/components/ai-elements/prompt-input.tsx
var b = o(null), x = o(null), S = () => c(b), C = () => c(x), w = o(null), T = () => {
	let e = C(), t = c(w), n = e ?? t;
	if (!n) throw Error("usePromptInputAttachments must be used within a PromptInput or PromptInputProvider");
	return n;
}, E = ({ className: r, accept: i, multiple: a, globalDrop: o, syncHiddenInput: c, maxFiles: g, maxFileSize: _, onError: v, onSubmit: y, children: b, ...x }) => {
	let C = S(), T = !!C, E = d(null), D = d(null), O = d(null);
	l(() => {
		let e = D.current?.closest("form");
		e instanceof HTMLFormElement && (O.current = e);
	}, []);
	let [k, A] = f([]), j = T ? C.attachments.files : k, M = s(() => {
		E.current?.click();
	}, []), N = s((e) => !i || i.trim() === "" ? !0 : i.includes("image/*") ? e.type.startsWith("image/") : !0, [i]), P = s((e) => {
		let n = Array.from(e), r = n.filter((e) => N(e));
		if (n.length && r.length === 0) {
			v?.({
				code: "accept",
				message: "No files match the accepted types."
			});
			return;
		}
		let i = r.filter((e) => _ ? e.size <= _ : !0);
		if (r.length > 0 && i.length === 0) {
			v?.({
				code: "max_file_size",
				message: "All files exceed the maximum size."
			});
			return;
		}
		A((e) => {
			let n = typeof g == "number" ? Math.max(0, g - 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: t(),
				type: "file",
				url: URL.createObjectURL(e),
				mediaType: e.type,
				filename: e.name
			});
			return e.concat(a);
		});
	}, [
		N,
		g,
		_,
		v
	]), F = T ? (e) => C.attachments.add(e) : P, I = T ? (e) => C.attachments.remove(e) : (e) => A((t) => {
		let n = t.find((t) => t.id === e);
		return n?.url && URL.revokeObjectURL(n.url), t.filter((t) => t.id !== e);
	}), L = T ? () => C.attachments.clear() : () => A((e) => {
		for (let t of e) t.url && URL.revokeObjectURL(t.url);
		return [];
	}), R = T ? () => C.attachments.openFileDialog() : M;
	l(() => {
		T && C.__registerFileInput(E, () => E.current?.click());
	}, [T, C]), l(() => {
		c && E.current && j.length === 0 && (E.current.value = "");
	}, [j, c]), l(() => {
		let e = O.current;
		if (!e) return;
		let t = (e) => {
			e.dataTransfer?.types?.includes("Files") && e.preventDefault();
		}, n = (e) => {
			e.dataTransfer?.types?.includes("Files") && e.preventDefault(), e.dataTransfer?.files && e.dataTransfer.files.length > 0 && F(e.dataTransfer.files);
		};
		return e.addEventListener("dragover", t), e.addEventListener("drop", n), () => {
			e.removeEventListener("dragover", t), e.removeEventListener("drop", n);
		};
	}, [F]), l(() => {
		if (!o) return;
		let e = (e) => {
			e.dataTransfer?.types?.includes("Files") && e.preventDefault();
		}, t = (e) => {
			e.dataTransfer?.types?.includes("Files") && e.preventDefault(), e.dataTransfer?.files && e.dataTransfer.files.length > 0 && F(e.dataTransfer.files);
		};
		return document.addEventListener("dragover", e), document.addEventListener("drop", t), () => {
			document.removeEventListener("dragover", e), document.removeEventListener("drop", t);
		};
	}, [F, o]), l(() => () => {
		if (!T) for (let e of j) e.url && URL.revokeObjectURL(e.url);
	}, [T, j]);
	let z = (e) => {
		e.currentTarget.files && F(e.currentTarget.files);
	}, B = async (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);
		});
	}, V = u(() => ({
		files: j.map((e) => ({
			...e,
			id: e.id
		})),
		add: F,
		remove: I,
		clear: L,
		openFileDialog: R,
		fileInputRef: E
	}), [
		j,
		F,
		I,
		L,
		R
	]), H = /* @__PURE__ */ h(p, { children: [
		/* @__PURE__ */ m("span", {
			"aria-hidden": "true",
			className: "hidden",
			ref: D
		}),
		/* @__PURE__ */ m("input", {
			accept: i,
			"aria-label": "Upload files",
			className: "hidden",
			multiple: a,
			onChange: z,
			ref: E,
			title: "Upload files",
			type: "file"
		}),
		/* @__PURE__ */ m("form", {
			className: e("w-full", r),
			onSubmit: (e) => {
				e.preventDefault();
				let t = e.currentTarget, n = T ? C.textInput.value : new FormData(t).get("message") || "";
				T || t.reset(), Promise.all(j.map(async ({ id: e, ...t }) => t.url && t.url.startsWith("blob:") ? {
					...t,
					url: await B(t.url)
				} : t)).then((t) => {
					try {
						let r = y({
							text: n,
							files: t
						}, e);
						r instanceof Promise ? r.then(() => {
							L(), T && C.textInput.clear();
						}).catch(() => {}) : (L(), T && C.textInput.clear());
					} catch {}
				});
			},
			...x,
			children: /* @__PURE__ */ m(n, {
				className: "overflow-hidden",
				children: b
			})
		})
	] });
	return T ? H : /* @__PURE__ */ m(w.Provider, {
		value: V,
		children: H
	});
}, D = ({ className: t, ...n }) => /* @__PURE__ */ m("div", {
	className: e("contents", t),
	...n
}), O = ({ onChange: t, className: n, placeholder: r = "What would you like to know?", ...i }) => {
	let o = S(), s = T(), [c, l] = f(!1), u = (e) => {
		if (e.key === "Enter") {
			if (c || 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 === "" && s.files.length > 0) {
			e.preventDefault();
			let t = s.files.at(-1);
			t && s.remove(t.id);
		}
	}, d = (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(), s.add(n));
	}, p = o ? {
		value: o.textInput.value,
		onChange: (e) => {
			o.textInput.setInput(e.currentTarget.value), t?.(e);
		}
	} : { onChange: t };
	return /* @__PURE__ */ m(a, {
		className: e("field-sizing-content max-h-48 min-h-16 text-base md:text-base", n),
		name: "message",
		onCompositionEnd: () => l(!1),
		onCompositionStart: () => l(!0),
		onKeyDown: u,
		onPaste: d,
		placeholder: r,
		...i,
		...p
	});
}, k = ({ className: t, ...n }) => /* @__PURE__ */ m(r, {
	align: "block-end",
	className: e("justify-between gap-1", t),
	...n
}), A = ({ className: t, variant: n = "default", size: r = "icon-sm", status: a, children: o, ...s }) => {
	let c = /* @__PURE__ */ m(g, { className: "size-4" });
	return a === "submitted" ? c = /* @__PURE__ */ m(_, { className: "size-4 animate-spin" }) : a === "streaming" ? c = /* @__PURE__ */ m(v, { className: "size-4" }) : a === "error" && (c = /* @__PURE__ */ m(y, { className: "size-4" })), /* @__PURE__ */ m(i, {
		"aria-label": "Submit",
		className: e(t),
		size: r,
		type: "submit",
		variant: n,
		...s,
		children: o ?? c
	});
};
//#endregion
export { E as PromptInput, D as PromptInputBody, k as PromptInputFooter, A as PromptInputSubmit, O as PromptInputTextarea };
