"use client";
import { Button as e } from "./buttons/Button.js";
import { twMerge as t } from "./helpers/twMerge.js";
import { useEffect as n, useRef as r, useState as i } from "react";
import { jsx as a, jsxs as o } from "react/jsx-runtime";
//#region lib/ui/ChatInput.tsx
var s = 12, c = 48, l = 8;
function u(e) {
	let t = r(e);
	return n(() => {
		t.current = e;
	}), t;
}
var d = 3e3;
function f(e) {
	let [t, a] = i(!1), [o, s] = i(!1), c = r(null), l = r(null), f = r(!1), p = u(e);
	function m() {
		l.current && clearTimeout(l.current);
	}
	function h() {
		m(), l.current = setTimeout(() => {
			f.current = !1, s(!1), c.current?.stop();
		}, d);
	}
	n(() => {
		let e = window.webkitSpeechRecognition ?? window.SpeechRecognition;
		if (!e) return;
		a(!0);
		let t = new e();
		return t.continuous = !0, t.interimResults = !0, t.lang = navigator.language, t.onresult = (e) => {
			let t = Array.from(e.results).map((e) => e[0].transcript).join("");
			p.current(t), h();
		}, t.onend = () => {
			if (m(), f.current) try {
				t.start();
			} catch {}
			else s(!1);
		}, t.onerror = (e) => {
			(e.error === "not-allowed" || e.error === "aborted") && (f.current = !1, s(!1), m());
		}, c.current = t, () => {
			t.abort(), m();
		};
	}, []);
	function g() {
		let e = c.current;
		e && (f.current ? (f.current = !1, e.stop()) : (f.current = !0, s(!0), e.start(), h()));
	}
	return {
		isSupported: t,
		isListening: o,
		toggle: g
	};
}
function p(e) {
	let t = r(null);
	return n(() => {
		let n = t.current;
		if (!e) {
			n && (n.style.height = "0px");
			return;
		}
		let r, i = null, a = null;
		return navigator.mediaDevices.getUserMedia({
			audio: !0,
			video: !1
		}).then((e) => {
			i = e, a = new AudioContext();
			let n = a.createMediaStreamSource(i), o = a.createAnalyser();
			o.fftSize = 256, n.connect(o);
			let s = new Uint8Array(o.frequencyBinCount);
			function c() {
				o.getByteFrequencyData(s);
				let e = s.reduce((e, t) => e + t, 0) / s.length, n = t.current;
				n && (n.style.height = `${Math.sqrt(e / 255) * 10}px`), r = requestAnimationFrame(c);
			}
			r = requestAnimationFrame(c);
		}), () => {
			cancelAnimationFrame(r), i?.getTracks().forEach((e) => e.stop()), a?.close();
		};
	}, [e]), t;
}
function m({ value: n, placeholder: r = "Ask a question?", onValueChange: u, onSubmit: d, onAudioClick: m, className: h, ...g }) {
	let [_, v] = i(!1), y = n.trim(), b = _ || y.length > 0, { isSupported: x, isListening: S, toggle: C } = f(u), w = p(S), T = x || !!m, E = T ? c + l + c : c;
	function D() {
		x ? C() : m?.();
	}
	function O(e) {
		e.preventDefault(), y && d();
	}
	return /* @__PURE__ */ o("form", {
		onSubmit: O,
		onMouseDown: (e) => {
			e.target.tagName !== "TEXTAREA" && e.preventDefault();
		},
		...g,
		style: { paddingBottom: b ? s + l + c : s },
		className: t("relative flex items-start overflow-hidden bg-white border border-light-grey p-12 w-full", "transition-[padding-bottom,border-radius] duration-250 ease-out rounded-b-[24]", b ? "rounded-32" : "rounded-[40]", h),
		children: [/* @__PURE__ */ a("textarea", {
			rows: b ? 3 : 1,
			value: n,
			onChange: (e) => u(e.target.value),
			onKeyDown: (e) => {
				e.key === "Enter" && (e.metaKey || e.ctrlKey) && y && (e.preventDefault(), d());
			},
			onFocus: () => v(!0),
			onBlur: () => v(!1),
			placeholder: r,
			"aria-label": r,
			style: { marginInlineEnd: b ? 0 : E + l },
			className: t("scrollbar-hidden bg-transparent text-b3 py-12 outline-none resize-none w-full px-20 border border-middleGrey", "transition-[margin-inline-end,border-radius] duration-250 ease-out", b ? "text-black rounded-[20]" : "text-darkGrey placeholder:text-darkGrey rounded-[40]")
		}), /* @__PURE__ */ o("div", {
			style: {
				insetInlineStart: b ? `calc(50% - ${E / 2}px)` : `calc(100% - ${s + E}px)`,
				bottom: s
			},
			className: "absolute flex gap-8 items-center transition-[inset-inline-start] duration-250 ease-out",
			children: [/* @__PURE__ */ a(e, {
				type: "submit",
				"aria-label": "Send message",
				disabled: !y,
				variant: "circle",
				color: "saffron",
				icon: "ArrowTailRight",
				iconWidth: "24px"
			}), T && /* @__PURE__ */ a(e, {
				type: "button",
				"aria-label": S ? "Stop recording" : "Use microphone",
				"aria-pressed": S,
				onClick: D,
				variant: "circle",
				theme: S ? "solid" : "outline",
				color: S ? "sienna" : "black",
				icon: "Audio",
				iconWidth: "24px",
				className: t("relative", S && "animate-pulse", !S && "border-lightGrey"),
				children: /* @__PURE__ */ a("span", {
					ref: w,
					"aria-hidden": "true",
					className: "bg-current absolute w-5 bottom-21",
					style: { height: "0px" }
				})
			})]
		})]
	});
}
//#endregion
export { m as ChatInput };

//# sourceMappingURL=ChatInput.js.map