import consts from "@/common/mx-const.js"

// TOKEN
function getToken() {
  return localStorage.getItem(consts.keys.token)
}

function setToken(token) {
  localStorage.setItem(consts.keys.token, token)
}

function removeToken() {
  removeMenuTab()
  removeMenuList()
  removeMenuListAction()
  removeActiveMenu()
  removeUserInfo()
  localStorage.removeItem(consts.keys.token)
}

// menu tab
function getMenuTab() {
  return JSON.parse(localStorage.getItem(consts.keys.menuTab) || '[]')
}

function setMenuTab(routes) {
  localStorage.setItem(consts.keys.menuTab, JSON.stringify(routes || []))
}

function removeMenuTab() {
  localStorage.removeItem(consts.keys.menuTab)
}

//menu list
function getMenuList() {
  return JSON.parse(localStorage.getItem(consts.keys.menuList) || '[]')
}

function setMenuList(menus) {
  localStorage.setItem(consts.keys.menuList, JSON.stringify(menus || []))
}

function removeMenuList() {
  localStorage.removeItem(consts.keys.menuList)
}

//menu list action
function getMenuListAction() {
  return localStorage.getItem(consts.keys.menuListAction)
}

function setMenuListAction(path) {
  localStorage.setItem(consts.keys.menuListAction, path)
}

function removeMenuListAction() {
  localStorage.removeItem(consts.keys.menuListAction)
}

// active menu
function getActiveMenu() {
  return localStorage.getItem(consts.keys.activeMenu)
}

function setActiveMenu(path) {
  localStorage.setItem(consts.keys.activeMenu, path)
}

function removeActiveMenu() {
  localStorage.removeItem(consts.keys.activeMenu)
}

// user info
function getUserInfo() {
  return JSON.parse(localStorage.getItem(consts.keys.userInfo) || "{}")
}

function setUserInfo(user) {
  localStorage.setItem(consts.keys.userInfo, JSON.stringify(user || {}))
}

function removeUserInfo() {
  localStorage.removeItem(consts.keys.userInfo)
}

export default {
  getToken,
  setToken,
  removeToken,
  getMenuTab,
  setMenuTab,
  removeMenuTab,
  getMenuList,
  setMenuList,
  removeMenuList,
  getMenuListAction,
  setMenuListAction,
  removeMenuListAction,
  getActiveMenu,
  setActiveMenu,
  removeActiveMenu,
  getUserInfo,
  setUserInfo,
  removeUserInfo
}
