import pinia from '~src/store/init'
import { useAppStore } from '~src/store'
import { getButtons } from '~api/menu'
import { getStore } from 'hc-vue3-ui'
import { ArrToOneObj, getArrValue } from 'js-fast-way'

const store = useAppStore(pinia)

//按钮初始化
export const initButtons = async () => {
    const value = getStore('buttons')
    if (!value) {
        const { error, data } = await getButtons()
        if (error) return Promise.reject('error')
        const buttons = getArrValue(data)
        const buttonsArr = await setButtonsData(buttons)
        store.setButtons(buttonsArr)
        return Promise.resolve(true)
    } else {
        return Promise.resolve(true)
    }
}

//设置按钮
const setButtonsData = async (data) => {
    let buttonsArr = {}
    await ArrToOneObj(data, 'code', buttonsArr)
    return buttonsArr
}
