import { t as e } from "../../utils-B7XIe3qq.js";
import * as t from "react";
import { jsx as n, jsxs as r } from "react/jsx-runtime";
//#region src/react/primitives/terminal.tsx
var i = {
	command: "text-foreground",
	output: "text-foreground-secondary",
	error: "text-accent-rose",
	greeting: "text-accent-cyan"
};
function a({ prompt: a = "$ ", greeting: o, onCommand: s, maxHistory: c = 100, maxLines: l = 1e3, fontSize: u = 13, fontFamily: d = "Menlo, Monaco, Consolas, monospace", className: f, ...p }) {
	let [m, h] = t.useState(() => o ? [{
		text: o,
		type: "greeting"
	}] : []), [g, _] = t.useState(""), [v, y] = t.useState(!1), b = t.useRef([]), x = t.useRef(-1), S = t.useRef(""), C = t.useRef(null), w = t.useRef(null), T = t.useCallback((e, t) => {
		let n = e.split("\n");
		h((e) => {
			let r = [...e, ...n.map((e) => ({
				text: e,
				type: t
			}))];
			return r.length > l ? r.slice(r.length - l) : r;
		});
	}, [l]);
	t.useEffect(() => {
		C.current && (C.current.scrollTop = C.current.scrollHeight);
	}, [m]);
	let E = t.useCallback(() => {
		w.current?.focus();
	}, []), D = t.useCallback(async () => {
		let e = g.trim();
		if (_(""), x.current = -1, S.current = "", T(`${a}${e}`, "command"), e.length === 0) return;
		let t = b.current;
		if (t[t.length - 1] !== e && (t.push(e), t.length > c && t.shift()), e === "clear") {
			h([]);
			return;
		}
		if (s) {
			y(!0);
			try {
				let t = await s(e);
				t.length > 0 && T(t, "output");
			} catch (e) {
				T(e instanceof Error ? e.message : String(e), "error");
			} finally {
				y(!1), E();
			}
		}
	}, [
		g,
		a,
		s,
		c,
		T,
		E
	]), O = t.useCallback((e) => {
		let t = b.current;
		if (t.length === 0) return;
		x.current === -1 && e === -1 && (S.current = g, x.current = t.length);
		let n = x.current + e;
		if (!(n < 0)) {
			if (n >= t.length) {
				x.current = -1, _(S.current);
				return;
			}
			x.current = n, _(t[n] ?? "");
		}
	}, [g]), k = t.useCallback((e) => {
		switch (e.key) {
			case "Enter":
				e.preventDefault(), v || D();
				break;
			case "ArrowUp":
				e.preventDefault(), O(-1);
				break;
			case "ArrowDown":
				e.preventDefault(), O(1);
				break;
			case "c":
				e.ctrlKey && (e.preventDefault(), T(`${a}${g}^C`, "command"), _(""), x.current = -1);
				break;
			case "l":
				e.ctrlKey && (e.preventDefault(), h([]));
				break;
		}
	}, [
		v,
		D,
		O,
		T,
		a,
		g
	]);
	return /* @__PURE__ */ r("div", {
		"data-slot": "terminal",
		className: e("bg-glass border-border-glass flex flex-col rounded-lg border backdrop-blur-[20px]", "overflow-hidden", f),
		style: {
			fontFamily: d,
			fontSize: `${u}px`
		},
		onClick: E,
		...p,
		children: [/* @__PURE__ */ n("div", {
			ref: C,
			className: "flex-1 overflow-auto p-3",
			role: "log",
			"aria-live": "polite",
			children: m.map((t, r) => /* @__PURE__ */ n("div", {
				className: e("whitespace-pre-wrap", i[t.type]),
				children: t.text
			}, r))
		}), /* @__PURE__ */ r("div", {
			className: "border-border-glass flex items-center border-t px-3 py-2",
			children: [/* @__PURE__ */ n("span", {
				className: "text-accent-cyan shrink-0",
				children: a
			}), /* @__PURE__ */ n("input", {
				ref: w,
				type: "text",
				className: "text-foreground min-w-0 flex-1 bg-transparent outline-none",
				value: g,
				onChange: (e) => _(e.target.value),
				onKeyDown: k,
				disabled: v,
				autoComplete: "off",
				autoCorrect: "off",
				autoCapitalize: "off",
				spellCheck: !1,
				"aria-label": "Terminal input"
			})]
		})]
	});
}
a.displayName = "Terminal";
//#endregion
export { a as Terminal };

//# sourceMappingURL=terminal.js.map