import * as api from '@/api/index'
import {RoutersMap} from "@/router";
import router from '@/router/index'
import TempCom from '@/views/global/temp.vue'
import RouterViewCom from '@/layout/router-view.vue'

const state = {
  menuRootName: '__root',
  homeRouterName: '3689eaa7-8203-4e4e-b8f8-d4286fc49d7f',
  allRouters: [],
  subMenuRouter: [],
  navigations: sessionStorage.getItem('navigations') ? JSON.parse(sessionStorage.getItem('navigations') as string) : [],
  menuExpend: sessionStorage.getItem('menuExpend') ? JSON.parse(sessionStorage.getItem('menuExpend') as string) : true,
}

const getters = {
}

const mutations = {
  SET_ROUTERS (state: any, data: Array<any>) {
    state.allRouters = data
  },
  SET_SUB_MENU_ROUTER(state: any, data: Array<any>) {
    state.subMenuRouter = data
  },
  SET_NAVIGATION(state: any, data: any) {
    if (state.navigations.every((v: any) => v.name !== data.name)) {
      state.navigations.push(data)
      sessionStorage.setItem('navigations', JSON.stringify(state.navigations))
    }
  },
  SET_NAVIGATION_DEL(state: any, name: string) {
    state.navigations = state.navigations.filter((v: any) => v.name !== name)
    sessionStorage.setItem('navigations', JSON.stringify(state.navigations))
  },
  SET_SWITCH_MENU_EXPEND(state: any) {
    state.menuExpend = !state.menuExpend
    sessionStorage.setItem('menuExpend', JSON.stringify(state.menuExpend))
  }
}

const actions = {
  LOAD_ROUTERS ({ commit }: any, data: Array<any>) {
    commit('SET_ROUTERS', data)
  },
  LOAD_SUB_MENU_ROUTER ({ commit }: any, data: Array<any>) {
    commit('SET_SUB_MENU_ROUTER', data)
  },
  LOAD_NAVIGATION ({ commit }: any, data: any) {
    commit('SET_NAVIGATION', data)
  },
  LOAD_NAVIGATION_DEL ({ commit }: any, name: string) {
    commit('SET_NAVIGATION_DEL', name)
  },
  LOAD_SWITCH_MENU_EXPEND ({ commit }: any) {
    commit('SET_SWITCH_MENU_EXPEND')
  },
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
