const ls = window.localStorage
const ss = window.sessionStorage
import { storagePrefixName } from "@/config/settings"

export const Local = {
  get(key, def) {
    let value = null
    try {
      value = JSON.parse(ls.getItem(storagePrefixName + "-" + key))
    } catch (e) {
      console.log(" ")
    }
    return value == null && def != null ? def : value
  },
  set(key, val) {
    try {
      const setting = arguments[0]
      if (Object.prototype.toString.call(setting).slice(8, -1) === "Object") {
        for (const i in setting) {
          ls.setItem(i, JSON.stringify(setting[i]))
        }
      } else {
        ls.setItem(storagePrefixName + "-" + key, JSON.stringify(val))
      }
    } catch (e) {
      console.log("'")
    }
  },
  remove(key) {
    ls.removeItem(storagePrefixName + "-" + key)
  },
  clear() {
    ls.clear()
  },
}

export const Session = {
  get(key) {
    try {
      return JSON.parse(ss.getItem(storagePrefixName + "-" + key))
    } catch (e) {
      return null
    }
  },
  set(key, val) {
    const setting = arguments[0]
    if (Object.prototype.toString.call(setting).slice(8, -1) === "Object") {
      for (const i in setting) {
        ss.setItem(i, JSON.stringify(setting[i]))
      }
    } else {
      ss.setItem(storagePrefixName + "-" + key, JSON.stringify(val))
    }
  },
  remove(key) {
    ss.removeItem(storagePrefixName + "-" + key)
  },
  clear() {
    ss.clear()
  },
}
