/*
 * @Author: micky 1254597151@qq.com
 * @Date: 2023-08-14 15:56:31
 * @LastEditors: micky 1254597151@qq.com
 * @LastEditTime: 2023-11-08 19:10:33
 * @FilePath: \housekeeping-admin\src\hooks\useMultipleTabs.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import useTabsStore from '@/stores/modules/multipleTabs'
import useSettingStore from '@/stores/modules/setting'
import { objectToQuery } from '@/utils/util'

export default function useMultipleTabs() {
    const router = useRouter()
    const route = useRoute()
    const tabsStore = useTabsStore()
    const settingStore = useSettingStore()

    const tabsLists = computed(() => {
        return tabsStore.getTabList
    })

    const currentTab = computed(() => {
        return Object.keys(route.query).length > 0 ? `${route.path}?${objectToQuery(route.query)}` : route.path
        // return route.path
    })

    const addTab = () => {
        if (!settingStore.openMultipleTabs) return
        tabsStore.addTab(router)
    }

    const removeTab = (fullPath?: any) => {
        if (!settingStore.openMultipleTabs) return
        fullPath = fullPath ?? route.fullPath
        tabsStore.removeTab(fullPath, router)
    }

    const removeOtherTab = () => {
        if (!settingStore.openMultipleTabs) return
        tabsStore.removeOtherTab(route)
    }

    const removeAllTab = () => {
        if (!settingStore.openMultipleTabs) return
        tabsStore.removeAllTab(router)
    }

    return {
        tabsLists,
        currentTab,
        addTab,
        removeTab,
        removeOtherTab,
        removeAllTab
    }
}
