import Vue from 'vue'
import Vuex from 'vuex'
import storage from '@/framework/utils/storage'
import layout from '@/framework/layout/layout-store'
import project from "./modules/project"
import alarm from './modules/alarm'
Vue.use(Vuex)
const KEY_LAST_ROUTE = 'last_route'

export default new Vuex.Store({
  state: {
    flag: 'sagacloud-admin',
    ssoToken: null,
    lastRoute: null
  },
  getters: {
    flag: state => state.flag,
    ssoToken: state => state.ssoToken,
    lastRoute: state => {
      if (!state.lastRoute) {
        let lastRoute = storage.get(KEY_LAST_ROUTE)
        if (lastRoute) {
          state.lastRoute = lastRoute
        }
      }
      return state.lastRoute
    }
  },
  mutations: {
    setSsoToken: (state, val) => (state.ssoToken = val),
    setLastRoute: (state, val) => {
      state.lastRoute = val
      storage.set(KEY_LAST_ROUTE, val)
    }
  },
  actions: {},
  modules: {
    layout,
    project,
    alarm
  }
})