<script setup lang="ts">
import { IonApp, IonRouterOutlet } from '@ionic/vue';
import { useRouter } from 'vue-router';
import { useStore } from 'vuex'
import { AppStore } from '@/store';
import { onMounted, onBeforeUnmount, ref } from "vue";
import EventBus from "./common/EventBus";

const router = useRouter();
const store = useStore<AppStore>();

const logout = () => {
  store.dispatch('auth/logout');
  router.push({path: '/login'});
}

const isOnline = ref(navigator.onLine);

onMounted(() => {
  EventBus.on("logout", logout);

  window.addEventListener("online", () => isOnline.value = true);
  window.addEventListener("offline", () => isOnline.value = false);

  if (isOnline.value) {
    router.push({path: '/login'});
  } else {
    // checkStoredUserData();
  }
});

onBeforeUnmount(() => {
  EventBus.remove("logout", logout);
})
</script>

<template>
  <ion-app>
    <ion-router-outlet />
  </ion-app>
</template>
