import { n as e, t } from "../../utils-C_mWfcFX.js";
//#region src/visualization/terminal/index.ts
var n = "$ ", r = 100, i = 1e3, a = 13, o = "Menlo, Monaco, Consolas, monospace", s = class {
	id;
	container;
	config;
	prompt;
	maxHistory;
	maxLines;
	outputEl = null;
	inputEl = null;
	history = [];
	historyIndex = -1;
	currentInput = "";
	lines = [];
	processing = !1;
	constructor(a) {
		this.id = e("cmm-terminal"), this.container = t(a.container), this.config = a, this.prompt = a.prompt ?? n, this.maxHistory = a.maxHistory ?? r, this.maxLines = a.maxLines ?? i, this.render(), a.greeting !== void 0 && this.addLine(a.greeting, "greeting"), this.focusInput();
	}
	write(e) {
		this.addLine(e, "output");
	}
	writeLine(e) {
		this.addLine(e, "output");
	}
	writeError(e) {
		this.addLine(e, "error");
	}
	clear() {
		this.lines.length = 0, this.renderOutput();
	}
	focus() {
		this.focusInput();
	}
	destroy() {
		this.container.innerHTML = "", this.outputEl = null, this.inputEl = null, this.history.length = 0, this.lines.length = 0;
	}
	render() {
		this.container.innerHTML = "";
		let e = this.config.fontSize ?? a, t = this.config.fontFamily ?? o, n = document.createElement("div");
		n.className = "cmm-terminal", n.id = this.id, n.style.fontFamily = t, n.style.fontSize = `${String(e)}px`, this.config.className !== void 0 && n.classList.add(this.config.className), n.addEventListener("click", () => {
			this.focusInput();
		});
		let r = document.createElement("div");
		r.className = "cmm-terminal__output", r.setAttribute("role", "log"), r.setAttribute("aria-live", "polite"), n.appendChild(r), this.outputEl = r;
		let i = document.createElement("div");
		i.className = "cmm-terminal__input-line";
		let s = document.createElement("span");
		s.className = "cmm-terminal__prompt", s.textContent = this.prompt, i.appendChild(s);
		let c = document.createElement("input");
		c.className = "cmm-terminal__input", c.type = "text", c.setAttribute("autocomplete", "off"), c.setAttribute("autocorrect", "off"), c.setAttribute("autocapitalize", "off"), c.setAttribute("spellcheck", "false"), c.setAttribute("aria-label", "Terminal input"), c.addEventListener("keydown", this.handleInputKeyDown.bind(this)), i.appendChild(c), n.appendChild(i), this.inputEl = c, this.container.appendChild(n);
	}
	renderOutput() {
		if (this.outputEl !== null) {
			this.outputEl.innerHTML = "";
			for (let e of this.lines) {
				let t = document.createElement("div");
				t.className = `cmm-terminal__line cmm-terminal__line--${e.type}`, t.textContent = e.text, this.outputEl.appendChild(t);
			}
			this.scrollToBottom();
		}
	}
	addLine(e, t) {
		let n = e.split("\n");
		for (let e of n) this.lines.push({
			text: e,
			type: t
		});
		for (; this.lines.length > this.maxLines;) this.lines.shift();
		if (this.outputEl !== null) {
			for (let e of n) {
				let n = document.createElement("div");
				n.className = `cmm-terminal__line cmm-terminal__line--${t}`, n.textContent = e, this.outputEl.appendChild(n);
			}
			for (; this.outputEl.childNodes.length > this.maxLines;) {
				let e = this.outputEl.firstChild;
				e !== null && this.outputEl.removeChild(e);
			}
			this.scrollToBottom();
		}
	}
	scrollToBottom() {
		this.outputEl !== null && (this.outputEl.scrollTop = this.outputEl.scrollHeight);
	}
	focusInput() {
		this.inputEl !== null && this.inputEl.focus();
	}
	handleInputKeyDown(e) {
		switch (e.key) {
			case "Enter":
				e.preventDefault(), this.processing || this.submitCommand();
				break;
			case "ArrowUp":
				e.preventDefault(), this.navigateHistory(-1);
				break;
			case "ArrowDown":
				e.preventDefault(), this.navigateHistory(1);
				break;
			case "c":
				e.ctrlKey && (e.preventDefault(), this.inputEl !== null && (this.addLine(`${this.prompt}${this.inputEl.value}^C`, "command"), this.inputEl.value = "", this.historyIndex = -1, this.currentInput = ""));
				break;
			case "l":
				e.ctrlKey && (e.preventDefault(), this.clear());
				break;
		}
	}
	async submitCommand() {
		if (this.inputEl === null) return;
		let e = this.inputEl.value.trim();
		if (this.inputEl.value = "", this.historyIndex = -1, this.currentInput = "", this.addLine(`${this.prompt}${e}`, "command"), e.length !== 0) {
			if (this.pushHistory(e), e === "clear") {
				this.clear();
				return;
			}
			if (this.config.onCommand !== void 0) {
				this.processing = !0, this.setInputDisabled(!0);
				try {
					let t = this.config.onCommand(e), n;
					n = t instanceof Promise ? await t : t, n.length > 0 && this.addLine(n, "output");
				} catch (e) {
					let t = e instanceof Error ? e.message : String(e);
					this.addLine(t, "error");
				} finally {
					this.processing = !1, this.setInputDisabled(!1), this.focusInput();
				}
			}
		}
	}
	pushHistory(e) {
		if (this.history[this.history.length - 1] !== e) for (this.history.push(e); this.history.length > this.maxHistory;) this.history.shift();
	}
	navigateHistory(e) {
		if (this.inputEl === null || this.history.length === 0) return;
		this.historyIndex === -1 && e === -1 && (this.currentInput = this.inputEl.value, this.historyIndex = this.history.length);
		let t = this.historyIndex + e;
		if (t < 0) return;
		if (t >= this.history.length) {
			this.historyIndex = -1, this.inputEl.value = this.currentInput;
			return;
		}
		this.historyIndex = t;
		let n = this.history[this.historyIndex];
		n !== void 0 && (this.inputEl.value = n);
		let r = this.inputEl.value.length;
		this.inputEl.setSelectionRange(r, r);
	}
	setInputDisabled(e) {
		this.inputEl !== null && (this.inputEl.disabled = e);
	}
};
//#endregion
export { s as Terminal };

//# sourceMappingURL=index.js.map