import { useLayoutConfig } from '../index';
import { computed } from 'vue'

export const StoreName = 'if-layout-config';

export const useConfig = () => {
    return useLayoutConfig();
}

export const getConfig = () => {
    const $ = useLayoutConfig();
    return $.config
}

export const getStore = () => {
    const $ = getConfig();
    return $.store
}


export const getState = () => {
    const $ = getStore();
    return $.state[StoreName]
}

export const getGetters = (keys) => {
    const $ = getStore();
    const obj = {};
    keys.map(key => {
        obj[key] = computed(() => $.getters[`${StoreName}/${key}`])
    })
    return obj
}

// 推荐配合If-Auth
export const getRoutes = () => {
    const $ = getStore();
    return $.state['ifauth'] || $.state[StoreName]
}

export const pageClose = (page) => {
    const $ = getStore();
    $.commit(`${StoreName}/flushPage`, page)
}

// 设置标题
export const setTitle = (title) => {
    const $ = getStore();
    $.commit(`${StoreName}/setTitle`, title)
}

export const closePage = () => {
    const $ = getState();
    pageClose($.tagsView.activeIndex)
}
