import { a4 as ensure_array_like, $ as store_get, a3 as escape_html, a8 as unsubscribe_stores, _ as derived, a2 as attr_class, a9 as stringify, a1 as attr, Z as onDestroy, Y as writable, ab as get$1 } from './internal-B3VUlLoc.js';
import { c as activeProjectId, p as projects, A as subscribe, C as connected } from './Header-DF-atx4l.js';
import { W as WorkspaceEmptyState } from './WorkspaceEmptyState-CpWtvgum.js';

//#region src/lib/stores/terminals.ts
var terminals = writable([]);
var activeTerminalId = writable(null);
var dataListeners = /* @__PURE__ */ new Map();
var exitListeners = /* @__PURE__ */ new Map();
function upsert(session) {
	if (!session.alive) {
		removeLocalTerminal(session.id);
		return;
	}
	terminals.update((items) => {
		const idx = items.findIndex((item) => item.id === session.id);
		if (idx < 0) return [...items, session];
		const next = items.slice();
		next[idx] = session;
		return next;
	});
	const activeId = get$1(activeTerminalId);
	const active = get$1(terminals).find((terminal) => terminal.id === activeId);
	if (!activeId || !active || !active.alive && session.alive) activeTerminalId.set(session.id);
}
function removeLocalTerminal(terminalId) {
	terminals.update((items) => items.filter((item) => item.id !== terminalId));
	if (get$1(activeTerminalId) === terminalId) {
		const remaining = get$1(terminals);
		const live = remaining.find((item) => item.alive);
		activeTerminalId.set(live?.id ?? remaining[remaining.length - 1]?.id ?? null);
	}
}
subscribe((msg) => {
	if (msg.type === "terminal.output") {
		const fns = dataListeners.get(msg.terminalId);
		if (fns) fns.forEach((fn) => fn(msg.data, msg.replay));
	} else if (msg.type === "terminal.status") {
		upsert(msg.session);
		if (!msg.session.alive) {
			const fns = exitListeners.get(msg.session.id);
			if (fns) fns.forEach((fn) => fn(msg.session.exitCode ?? null));
		}
	}
});
//#endregion
//#region src/lib/stores/keybar-mods.ts
var oneShotMods = writable(/* @__PURE__ */ new Set());
var lockedMods = writable(/* @__PURE__ */ new Set());
//#endregion
//#region src/lib/components/terminal/MobileKeybar.svelte
function MobileKeybar($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		var $$store_subs;
		const primary = [
			{
				label: "Esc",
				value: "\x1B"
			},
			{
				label: "Tab",
				value: "	"
			},
			{
				label: "^C",
				value: ""
			},
			{
				label: "^D",
				value: ""
			},
			{
				label: "Copy",
				action: "copy"
			},
			{
				label: "Paste",
				action: "paste"
			},
			{
				label: "Ctrl",
				mod: "ctrl"
			},
			{
				label: "Alt",
				mod: "alt"
			},
			{
				label: "Shift",
				mod: "shift"
			},
			{
				label: "←",
				arrow: "D"
			},
			{
				label: "↓",
				arrow: "B"
			},
			{
				label: "↑",
				arrow: "A"
			},
			{
				label: "→",
				arrow: "C"
			},
			{
				label: "-",
				value: "-"
			},
			{
				label: "|",
				value: "|"
			},
			{
				label: "/",
				value: "/"
			},
			{
				label: "~",
				value: "~"
			},
			{
				label: "Fn",
				toggle: true
			}
		];
		let secondaryOpen = false;
		const rowKeys = derived(() => primary);
		$$renderer.push(`<div class="keybar md:hidden svelte-yty2fv" role="toolbar" aria-label="Terminal keys"><div class="row svelte-yty2fv"><!--[-->`);
		const each_array = ensure_array_like(rowKeys());
		for (let $$index = 0, $$length = each_array.length; $$index < $$length; $$index++) {
			let key = each_array[$$index];
			$$renderer.push(`<button type="button"${attr_class("key svelte-yty2fv", void 0, {
				"active": key.mod && (store_get($$store_subs ??= {}, "$oneShotMods", oneShotMods).has(key.mod) || store_get($$store_subs ??= {}, "$lockedMods", lockedMods).has(key.mod)),
				"locked": key.mod && store_get($$store_subs ??= {}, "$lockedMods", lockedMods).has(key.mod),
				"toggle-on": key.toggle && secondaryOpen
			})}>${escape_html(key.label)}</button>`);
		}
		$$renderer.push(`<!--]--></div></div>`);
		if ($$store_subs) unsubscribe_stores($$store_subs);
	});
}
//#endregion
//#region src/lib/components/terminal/Terminal.svelte
function Terminal_1($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		let { visible = true } = $$props;
		let unsubs = [];
		onDestroy(() => {
			for (const unsub of unsubs) unsub();
		});
		$$renderer.push(`<div${attr_class("terminal-container svelte-1fgrhx", void 0, { "hidden": !visible })} role="presentation"></div>`);
	});
}
//#endregion
//#region src/lib/components/terminal/TerminalTabs.svelte
function TerminalTabs($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		var $$store_subs;
		let { projectId } = $$props;
		let projectTerminals = derived(() => store_get($$store_subs ??= {}, "$terminals", terminals).filter((t) => t.projectId === projectId && t.alive));
		$$renderer.push(`<div class="flex items-center h-9 bg-bg-secondary border-b border-border overflow-x-auto shrink-0"><!--[-->`);
		const each_array = ensure_array_like(projectTerminals());
		for (let i = 0, $$length = each_array.length; i < $$length; i++) {
			let term = each_array[i];
			$$renderer.push(`<div${attr_class(`flex items-center gap-1.5 px-3 h-full text-xs whitespace-nowrap border-r border-border transition-colors cursor-pointer ${stringify(store_get($$store_subs ??= {}, "$activeTerminalId", activeTerminalId) === term.id ? "bg-bg text-text" : "text-text-secondary hover:bg-bg-hover")}`)} role="tab" tabindex="0"${attr("aria-selected", store_get($$store_subs ??= {}, "$activeTerminalId", activeTerminalId) === term.id)}><span${attr_class(`inline-block w-1.5 h-1.5 rounded-full ${stringify(term.alive ? "bg-success" : "bg-text-muted")}`)}></span> <span>${escape_html(term.name || `Shell ${i + 1}`)}</span> <button class="ml-1 p-0.5 rounded hover:bg-bg-tertiary text-text-muted hover:text-text" aria-label="Close terminal"><svg class="w-3 h-3" viewBox="0 0 12 12" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M3 3l6 6M9 3l-6 6"></path></svg></button></div>`);
		}
		$$renderer.push(`<!--]--> <button class="flex items-center justify-center w-9 h-full text-text-muted hover:text-text hover:bg-bg-hover transition-colors" aria-label="New terminal"><svg class="w-4 h-4" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M8 3v10M3 8h10"></path></svg></button></div>`);
		if ($$store_subs) unsubscribe_stores($$store_subs);
	});
}
//#endregion
//#region src/lib/components/panels/TerminalPanel.svelte
function TerminalPanel($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		var $$store_subs;
		let projectId = derived(() => store_get($$store_subs ??= {}, "$activeProjectId", activeProjectId) ?? "");
		let project = derived(() => store_get($$store_subs ??= {}, "$projects", projects).find((item) => item.id === projectId()) ?? null);
		let projectTerminals = derived(() => store_get($$store_subs ??= {}, "$terminals", terminals).filter((terminal) => terminal.projectId === projectId() && terminal.alive));
		let emptyTitle = derived(() => store_get($$store_subs ??= {}, "$projects", projects).length === 0 ? "No projects yet" : "No project selected");
		if (projectId() && project()) {
			$$renderer.push("<!--[0-->");
			$$renderer.push(`<div class="terminal-page svelte-slj7fa">`);
			TerminalTabs($$renderer, { projectId: projectId() });
			$$renderer.push(`<!----> <div class="terminal-stage svelte-slj7fa"><!--[-->`);
			const each_array = ensure_array_like(projectTerminals());
			for (let $$index = 0, $$length = each_array.length; $$index < $$length; $$index++) {
				let term = each_array[$$index];
				Terminal_1($$renderer, {
					terminalId: term.id,
					visible: store_get($$store_subs ??= {}, "$activeTerminalId", activeTerminalId) === term.id
				});
			}
			$$renderer.push(`<!--]--> `);
			if (projectTerminals().length === 0) {
				$$renderer.push("<!--[0-->");
				$$renderer.push(`<div class="empty-terminal svelte-slj7fa">${escape_html(store_get($$store_subs ??= {}, "$connected", connected) ? "No terminal open" : "Connecting...")}</div>`);
			} else $$renderer.push("<!--[-1-->");
			$$renderer.push(`<!--]--></div></div> `);
			MobileKeybar($$renderer);
			$$renderer.push(`<!---->`);
		} else {
			$$renderer.push("<!--[-1-->");
			WorkspaceEmptyState($$renderer, {
				title: emptyTitle(),
				description: "Select or create a folder with the + button in the project bar, then switch to Terminal."
			});
		}
		$$renderer.push(`<!--]-->`);
		if ($$store_subs) unsubscribe_stores($$store_subs);
	});
}

export { TerminalPanel as T, terminals as t };
//# sourceMappingURL=TerminalPanel-B_T52Ad_.js.map
