import { defineStore } from 'pinia'
import { reactive, toRefs } from 'vue'

export const useTabsStore = defineStore('tabs', {
    state: () => {
        return {
            activeTab: '/',
            visitedViews: [],
        }
    },
    actions: {
        setActiveTab(path) {
            this.activeTab = path
        },
        addTab(tab) {
            this.setActiveTab(tab.path)
            if (tab.path === '/home') return
            if (this.visitedViews.some((v) => v.path === tab.path)) return
            this.visitedViews.push(tab)
        },
        removeTab(tab) {
            //取得tab在visitedViews的index
            const index = this.visitedViews.indexOf(tab)
            this.visitedViews.splice(index, 1)

        },
        closeOtherTabs() {
            this.visitedViews = this.visitedViews.filter((v) => {
                return v.path === this.activeTab
            })
        },
        closeAllTabs() {
            this.visitedViews = [];
        },
    }
})
