export default {
    namespaced: true,
    state: {
        cachedViews: []
    },
    getters: {
        cachedViews: state => state.cachedViews,
    },
    mutations: {
        ADD_CACHED_VIEW: (state, view) => {
            if (state.cachedViews.includes(view.name)) return
            if ((!view.meta || !view.meta.noCache) && view.name) {
                state.cachedViews.push(view.name)
            }
        },

        DEL_CACHED_VIEW: (state, view) => {
            const index = state.cachedViews.indexOf(view.name)
            index > -1 && state.cachedViews.splice(index, 1)
        },

        DEL_ALL_CACHED_VIEWS: state => {
            state.cachedViews = []
        },
    },
    actions: {
        addCachedView({commit}, view) {
            console.log(view);
            commit('ADD_CACHED_VIEW', view)
        },
        delCachedView({commit, state}, view) {
            return new Promise(resolve => {
                commit('DEL_CACHED_VIEW', view)
                resolve([...state.cachedViews])
            })
        },
        delAllCachedViews({commit, state}) {
            return new Promise(resolve => {
                commit('DEL_ALL_CACHED_VIEWS')
                resolve([...state.cachedViews])
            })
        },
    },
}
