<template>
  <router-view/>
  
</template>

<script lang="ts" setup>
import { onMounted, ref,provide } from 'vue';
import { deleteDiscardedDB } from './utils/database';
import { useFontStore, useMainStore, useSnapshotStore } from '@/store'
import { LOCALSTORAGE_KEY_DISCARDED_DB } from '@/configs/storage';
import { storeToRefs } from 'pinia';
import { injectKeyTemplate } from './types/injectKey';

const snapshotStore = useSnapshotStore()
const mainStore = useMainStore()
const { databaseId } = storeToRefs(mainStore)
const searchData = ref({} as any)
provide(injectKeyTemplate,searchData)
const autoSave = ref(0)
provide("SellTravelSave",autoSave)

onMounted(async () => {
  await deleteDiscardedDB()
  snapshotStore.initSnapshotDatabase()
  mainStore.setAvailableFonts()
})
if (process.env.NODE_ENV === 'production') {
    window.onbeforeunload = () => false
}
// 应用注销时向 localStorage 中记录下本次 indexedDB 的数据库ID，用于之后清除数据库
window.addEventListener('unload', () => {
  const discardedDB = localStorage.getItem(LOCALSTORAGE_KEY_DISCARDED_DB)
  const discardedDBList: string[] = discardedDB ? JSON.parse(discardedDB) : []

  discardedDBList.push(databaseId.value)

  const newDiscardedDB = JSON.stringify(discardedDBList)
  localStorage.setItem(LOCALSTORAGE_KEY_DISCARDED_DB, newDiscardedDB)
})
useFontStore().loadAllFonts()
</script>

<style lang="scss">
@import url('@/assets/styles/common.css');
#app {
  height: 100%;
}
</style>