////
// Each browser tab is a shell session. This means you can run multiple sessions
// in the same browser.

import type { SessionStartMessage, SessionUpdateMessage } from "@/shared/types"
import { randomId } from "../shared/utils"
import { mode } from "./commands"
import { fire } from "./event"

export const sessionId = randomId()
export const sessionStore = new Map<string, string>()

export function initSession() { }

export function handleSessionStart(msg: SessionStartMessage) {
    for (const key of Object.keys(msg.data))
        sessionStore.set(key, (msg.data as any)[key] || "")

    fire("session:update", msg.data)
    mode(msg.data.mode)
}

export function handleSessionUpdate(msg: SessionUpdateMessage) {
    for (const key of Object.keys(msg.data))
        sessionStore.set(key, msg.data[key] || "")

    fire("session:update", msg.data)
}
