<script lang="ts">
  import Footer from "./components/Footer.svelte";
  import Header from "./components/Header.svelte";
  import Wrapper from "./components/Wrapper.svelte";
  import { SharkGame } from "./shark/SharkGame";

  import { Modals, closeModal } from "svelte-modals";
  import { onDestroy, onMount } from "svelte";
  import type { Unsubscriber } from "svelte/store";

  let root: HTMLElement;
  let unsubscribeSettings: Unsubscriber;

  onMount(() => {
    root = document.documentElement;

    SharkGame.Settings.settings.subscribe((settings) => {
      root.classList.toggle("no-theme", !settings.appearance.enableThemes.current);
      settings.appearance.theme.options.forEach((theme) => {
        root.classList.toggle(theme, theme === settings.appearance.theme.current);
      });
    });
  });

  onDestroy(() => {
    unsubscribeSettings();
  });

  function handleKeyUp(event: KeyboardEvent) {
    if (event.key === "Escape") {
      closeModal();
    }
  }
</script>

<svelte:body on:keyup={handleKeyUp} />

<Modals>
  <div slot="backdrop" id="modal-backdrop" on:click={closeModal} />
</Modals>
{#await SharkGame.initialize()}
  Loading...
{:then}
  <Header game={SharkGame} title={SharkGame.title} />
  <Wrapper game={SharkGame} />
  <Footer />
{/await}
