<template>
  <Modal />
  <ContextMenu />

  <Header @contextmenu.prevent />
  <div class="grow overflow-x-hidden overflow-y-auto p-2 md:p-4" @contextmenu.prevent>
    <RouterView />
  </div>
  <Footer @contextmenu.prevent />
  <Notification />

  <Teleport to="head">
    <title>{{ clubName }}</title>
    <link rel="icon" type="image/ico" :href="config.server_address + '/api/public/favicon.ico'" />
    <link rel="manifest" :href="config.server_address + '/api/public/manifest.webmanifest'" />
  </Teleport>
</template>

<script setup lang="ts">
import { defineComponent } from "vue";
import { RouterView } from "vue-router";
import Header from "./components/Header.vue";
import Footer from "./components/Footer.vue";
import { mapActions, mapState } from "pinia";
import { useAuthStore } from "./stores/auth";
import { isAuthenticatedPromise } from "./router/authGuard";
import ContextMenu from "./components/ContextMenu.vue";
import Modal from "./components/Modal.vue";
import Notification from "./components/Notification.vue";
import { config } from "./config";
import { useConfigurationStore } from "@/stores/configuration";
import { resetAllPiniaStores } from "@/helpers/piniaReset";
</script>

<script lang="ts">
export default defineComponent({
  computed: {
    ...mapState(useAuthStore, ["authCheck"]),
    ...mapState(useConfigurationStore, ["clubName"]),
  },
  mounted() {
    resetAllPiniaStores();
    this.configure();

    if (!this.authCheck && localStorage.getItem("access_token")) {
      isAuthenticatedPromise().catch(() => {
        localStorage.removeItem("access_token");
        localStorage.removeItem("refresh_token");
      });
    }
  },
  methods: {
    ...mapActions(useConfigurationStore, ["configure"]),
  },
});
</script>
