<script setup lang="ts">
    import { onExit } from '@dcloudio/uni-app'
    import { isDevMode } from '@/utils/env'
    import { usePermissions } from '@/hooks/app/usePermissions'
    import * as UserInfoAPI from '@/api/model/userInfo'
    import { useDictStore } from '@/store/modules/dict'
    import { useUserStore } from '@/store/modules/user'

    const userStore = useUserStore()
    const dictStore = useDictStore()

    // import * as Push from '@/utils/push'

    // 监听权限获取
    const { listen: startPermissionListener, stop: stopPermissionListener } = usePermissions()

    onLaunch(() => {
        console.log('App Launch')

        // 监听权限获取
        startPermissionListener()

        // 清除消息角标
        // Push.setBadge(0)

        // 监听在线消息
        // Push.listen()

        // FIXED: 开发模式，关闭启动页，防止调试内部页面时无法进入的问题
        // #ifdef APP-PLUS
        if (isDevMode()) {
            setTimeout(() => {
                plus.navigator.closeSplashscreen()
            }, 3000)
        }
        // #endif
    })
    function getDictList() {
        if (!userStore.getToken) return
        if (Object.keys(dictStore.getDictList).length !== 0) return
        UserInfoAPI.dictList().then((res) => {
            dictStore.setDictList(res)
        })
    }

    onExit(() => {
        // 停用监听权限
        stopPermissionListener()
    })

    onShow(() => {
        console.log('App Show')
        getDictList()
    })

    onHide(() => {
        console.log('App Hide')
    })

    onError((err) => {
        // Message.toast(`App Error: ${err}`)
        console.error('App Error:', err)
    })

    onUnhandledRejection((err) => {
        // Message.toast(`App UnhandledRejection: ${err}`)
        console.error('App UnhandledRejection:', err)
    })
</script>

<style lang="less">
    /* common css */
    /* #ifndef APP-PLUS-NVUE */
    /* uni.css - 通用组件、模板样式库，可以当作一套ui库应用 */
    @import url('./common/uni.css');
    /* #endif */

    /* 每个页面公共css */
    @import url('./common/fui-app.css');
    @import url('./components/FirstUI/fui-theme/fui-theme.css');

    // 全局样式
    @import url('./common/public.less');

    // codefun 全局样式
    @import url('./common/codefun.less');
</style>
