
export type Message =
    | ErrorMessage
    | InputMessage
    | OutputMessage
    | SaveFileMessage
    | SessionStartMessage
    | SessionUpdateMessage
    | GameStartMessage
    | StreamMessage
    | CommandsMessage
    | AppsMessage

export type CommandOutput = string | string[]
    | { text: string, script?: string }
    | { html: string, script?: string }
    | { script: string }
    | { game: string }

export type CommandResult = {
    status: "ok" | "error"
    output: CommandOutput
}

export type ErrorMessage = {
    type: "error"
    data: string
}

export type CommandsMessage = {
    type: "commands"
    data: Commands
}

export type AppsMessage = {
    type: "apps"
    data: string[]
}

export type OutputMessage = {
    type: "output"
    id?: string
    data: CommandResult
}

export type InputMessage = {
    type: "input"
    id: string
    session: string
    data: CommandResult | CommandOutput
}

export type SaveFileMessage = {
    type: "save-file"
    id: string
    session: string
    data: CommandResult | CommandOutput
}

export type SessionStartMessage = {
    type: "session:start"
    data: {
        NOSE_DIR: string
        project: string
        cwd: string
        mode: string
        hostname: string
    }
}

export type SessionUpdateMessage = {
    type: "session:update"
    data: Record<string, string>
}

export type GameStartMessage = {
    type: "game:start"
    id: string
    data: string
}

export type StreamMessage = {
    type: "stream:start" | "stream:end" | "stream:append" | "stream:replace"
    id: string
    session: string
    data: CommandOutput
}

export type Commands = Record<string, Command>

export type Command = {
    name: string
    type: "server" | "browser" | "game"
}
