import { cn } from "../../utils/cn.mjs";
import { Container } from "../Container/index.mjs";
import { Input } from "../Input/Input.mjs";
import { useMemo, useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { useIntlayer } from "react-intlayer";

//#region src/components/Terminal/Terminal.tsx
const ANSI_COLORS = {
	"\x1B[0m": {
		light: "",
		dark: ""
	},
	"\x1B[90m": {
		light: "text-gray-500",
		dark: "text-gray-400"
	},
	"\x1B[38;5;239m": {
		light: "text-gray-600",
		dark: "text-gray-500"
	},
	"\x1B[38;5;252m": {
		light: "text-gray-300",
		dark: "text-gray-300"
	},
	"\x1B[34m": {
		light: "text-blue-600",
		dark: "text-blue-400"
	},
	"\x1B[31m": {
		light: "text-red-600",
		dark: "text-red-400"
	},
	"\x1B[32m": {
		light: "text-green-600",
		dark: "text-green-400"
	},
	"\x1B[38;5;226m": {
		light: "text-yellow-500",
		dark: "text-yellow-300"
	},
	"\x1B[35m": {
		light: "text-purple-600",
		dark: "text-purple-400"
	},
	"\x1B[38;5;3m": {
		light: "text-amber-600",
		dark: "text-amber-300"
	},
	"\x1B[38;5;208m": {
		light: "text-orange-600",
		dark: "text-orange-400"
	},
	"\x1B[36m": {
		light: "text-cyan-600",
		dark: "text-cyan-400"
	},
	"\x1B[37m": {
		light: "text-gray-800",
		dark: "text-gray-200"
	},
	"\x1B[1m": {
		light: "font-bold",
		dark: "font-bold"
	}
};
const parseAnsiCodes = (text, isDarkMode) => {
	const segments = [];
	const ansiRegex = /(\x1b\[[0-9;]*m)/g;
	const parts = text.split(ansiRegex);
	let currentColor;
	let isBold = false;
	for (const part of parts) if (ansiRegex.test(part)) {
		const colorMapping = ANSI_COLORS[part];
		if (colorMapping) if (part === "\x1B[0m") {
			currentColor = void 0;
			isBold = false;
		} else if (part === "\x1B[1m") isBold = true;
		else currentColor = isDarkMode ? colorMapping.dark : colorMapping.light;
	} else if (part) segments.push({
		text: part,
		color: currentColor,
		isBold
	});
	return segments;
};
const Terminal = ({ className, children, isDarkMode = false, title = "bash", onClose, onSubmit, ...props }) => {
	const content = useIntlayer("terminal");
	const lines = useMemo(() => children.split("\n"), [children]);
	const [inputValue, setInputValue] = useState("");
	const handleKeyDown = (e) => {
		if (e.key === "Enter" && inputValue.trim()) {
			onSubmit?.(inputValue);
			setInputValue("");
		}
	};
	const containerProps = props;
	return /* @__PURE__ */ jsxs(Container, {
		roundedSize: "2xl",
		className: cn("flex min-w-0 max-w-full flex-col overflow-hidden font-mono", className),
		...containerProps,
		children: [
			/* @__PURE__ */ jsxs("div", {
				className: "flex w-full flex-row items-center justify-start gap-1 bg-neutral-200 text-neutral text-xs dark:bg-neutral-950",
				children: [/* @__PURE__ */ jsxs("div", {
					className: "mx-2 flex items-center justify-start gap-2 p-1",
					children: [
						/* @__PURE__ */ jsx("div", { className: "size-3 rounded-full bg-red-500" }),
						/* @__PURE__ */ jsx("div", { className: "size-3 rounded-full bg-yellow-500" }),
						/* @__PURE__ */ jsx("div", { className: "size-3 rounded-full bg-green-500" })
					]
				}), /* @__PURE__ */ jsx("div", {
					className: "flex size-full overflow-y-auto",
					children: /* @__PURE__ */ jsxs("div", {
						className: "flex h-8 min-w-20 items-center justify-between gap-2 bg-card px-3 py-1",
						children: [/* @__PURE__ */ jsx("span", { children: title }), onClose && /* @__PURE__ */ jsx("button", {
							type: "button",
							onClick: onClose,
							className: "text-neutral transition-colors hover:text-text",
							"aria-label": content.closeTab.value,
							children: "×"
						})]
					})
				})]
			}),
			/* @__PURE__ */ jsx("pre", {
				className: "min-w-0 max-w-full overflow-x-auto overflow-y-auto border-neutral/30 border-b p-3 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden",
				children: /* @__PURE__ */ jsx("code", { children: lines.map((line, lineIndex) => {
					const segments = parseAnsiCodes(line, isDarkMode);
					return /* @__PURE__ */ jsx("span", {
						className: "line block w-full",
						children: segments.length === 0 ? "\n" : segments.map((segment, segIndex) => /* @__PURE__ */ jsx("span", {
							className: cn(segment.color, { "font-bold": segment.isBold }),
							children: segment.text
						}, `seg-${lineIndex}-${segIndex}`))
					}, `line-${lineIndex}`);
				}) })
			}),
			/* @__PURE__ */ jsxs(Container, {
				className: "p-2",
				children: [/* @__PURE__ */ jsx("span", {
					className: "text-neutral",
					children: "~/Desktop/MyApp"
				}), /* @__PURE__ */ jsx(Input, {
					className: "m-0.5 w-full",
					variant: "invisible",
					value: inputValue,
					"aria-label": content.terminalInput.value,
					onChange: (e) => setInputValue(e.target.value),
					onKeyDown: handleKeyDown
				})]
			})
		]
	});
};

//#endregion
export { Terminal };
//# sourceMappingURL=Terminal.mjs.map