import type { Session } from "../api/sessions";

const tabRefs = new Map<string, Window | null>();

export function openSession(session: Session): void {
	const key = session.id;
	const existing = tabRefs.get(key);
	if (existing && !existing.closed) {
		existing.focus();
		return;
	}

	let url: string;
	if (session.url) {
		url = session.url;
	} else if (session.tool_type_interfaces?.includes("terminal")) {
		url = `/instances/${session.id}/terminal`;
	} else {
		url = `/projects/${session.project_id}`;
	}

	const w = window.open(url, `session-${session.id}`);
	tabRefs.set(key, w);
}
