import {
  getBreadCrumbList,
  // setTagNavListInLocalstorage,
  getMenuByRouter,
  getHomeRoute,
  getCookies,
  setCookies
} from '@/libs/util'
// import { saveErrorLogger } from '@/api/data'
import routers from '@/router/routers'
import config from '@/config'
const { homeName } = config

export default {
  state: {
    breadCrumbList: [],
    homeRoute: {},
    local: getCookies('language'),
    errorList: [],
    hasReadErrorPage: false,
    menuList: [],
    gotPermission: false
  },
  getters: {
    // menuList: (state, getters, rootState) => getMenuByRouter(routers, rootState.user.access),
    errorCount: state => state.errorList.length
  },
  mutations: {
    setMenuList (state, routes) {
      state.menuList = getMenuByRouter(routes)
      state.gotPermission = true
    },
    setBreadCrumb (state, route) {
      state.breadCrumbList = getBreadCrumbList(route, state.homeRoute)
    },
    setHomeRoute (state, routes) {
      state.homeRoute = getHomeRoute(routes, homeName)
    },
    setLocal (state, lang) {
      setCookies('language', lang)
      state.local = lang
    },
    addError (state, error) {
      state.errorList.push(error)
    },
    setHasReadErrorLoggerStatus (state, status = true) {
      state.hasReadErrorPage = status
    }
  },
  actions: {
    // addErrorLog ({ commit, rootState }, info) {
    //   if (!window.location.href.includes('error_logger_page')) commit('setHasReadErrorLoggerStatus', false)
    //   const { user: { token, userId, username } } = rootState
    //   let data = {
    //     ...info,
    //     time: Date.parse(new Date()),
    //     token,
    //     userId,
    //     username
    //   }
    //   // saveErrorLogger(info).then(() => {
    //   //   commit('addError', data)
    //   // })
    // }
  }
}
