import { tabStore } from "@/store/tabs";
import { ref, computed, onDeactivated, getCurrentInstance, watchEffect } from "vue"

/**
 * 删除路由缓存组件
 */
export const useDeleteCache = () => {
    const cacheList = ref()
    const store = tabStore();
    //获取tabs数据
    const tabsText = computed(() => {
        return store.getDeleteTable
    })
    const instance = getCurrentInstance();
    watchEffect(() => {
        if (cacheList.value) {
            cacheList.value.forEach((value, key) => {
                // 判断缓存有数据删除的对应缓存
                if (tabsText.value === key) {
                    cacheList.value.delete(key);
                    store.deleteCurrentTabel("")
                }
            });
        }
    })
    onDeactivated(() => {
        // @ts-ignore
        cacheList.value = instance?.parent.__v_cache;//keep-alive缓存的数组
        // const cache = instance?.parent.__v_cache;
        // console.log(cache, ":cache");
        // cache.forEach((value, key) => {
        //     // console.log(key, ":key", excludeRouter);
        //     if (!excludeRouter.includes(key)) {
        //         cache.delete(key);
        //     }
        // });
    });
    return instance
}