<script lang="ts">
    import { goto } from "$app/navigation";
    import DisplayStateSvelte from "$lib/DisplayState.svelte";
    import { MessageType } from "$lib/MessageType";
    import ws from "$lib/websocket.svelte";

    let { children } = $props();

    $effect(() => {
        if (ws.messageNum <= 0) return;
        console.log(ws.message);
        if (ws.message == "HOST-DISCONNECTED") {
            ws.nextMessage();
            goto("/connected/display");
        }
        if (ws.message == "HOST-CONNECTED") {
            ws.nextMessage();
        }
        try {
            let json = JSON.parse(ws.message);
            if (json.type == MessageType.START) {
                ws.nextMessage();
                goto(DisplayStateSvelte.baseRoute);
                return;
            }
        } catch (e) {}
    });
</script>

{@render children?.()}
