import { defineStore } from 'pinia';

interface AppState {
    pageLoading: boolean;
}
let timeId: TimeoutHandle;

/**
 * 用于存储全局的store
 */
export const useAppStore = defineStore('app', {
    state: (): AppState => {
        return {
            pageLoading: false,//页面加载状态
        }
    },
    getters: {
        getPageLoading(state): boolean {
            return state.pageLoading;
        },
    },
    actions: {
        setPageLoading(loading: boolean): void {
            this.pageLoading = loading;
        },
        async setPageLoadingAction(loading: boolean): Promise<void> {
            if (loading) {
                clearTimeout(timeId);
                // Prevent flicker
                timeId = setTimeout(() => {
                    this.setPageLoading(loading);
                }, 50);
            } else {
                this.setPageLoading(loading);
                clearTimeout(timeId);
            }
        },
    },

})