export const cookie = {
  set(cname, cvalue, exHours) {
    const d = new Date()
    d.setTime(d.getTime() + exHours * 60 * 60 * 1000)
    const expires = 'expires=' + d.toUTCString()
    document.cookie = cname + '=' + cvalue + '; ' + expires + '; Domain=http://10.137.193.5:8092; path=/'
  },
  get(cname) {
    const name = cname + '='
    const ca = document.cookie.split(';')
    for (let i = 0; i < ca.length; i++) {
      let c = ca[i]
      while (c.charAt(0) === ' ') c = c.substring(1)
      if (c.indexOf(name) !== -1) {
        return c.substring(name.length, c.length)
      }
    }
    return ''
  },
  clear(cname) {
    const expires = -1 * 24 * 60 * 60 * 1000
    const date = new Date(+new Date() + expires)
    document.cookie = cname + "='';expires=" + date.toUTCString()
  },
}

const locals = {
  get(key) {
    return JSON.parse(localStorage.getItem(key)!)
  },
  set(key, data) {
    return localStorage.setItem(key, JSON.stringify(data))
  },
  remove(key) {
    return localStorage.removeItem(key)
  },
  clearAll() {
    return localStorage.clear()
  },
}
const sessions = {
  get(key) {
    return JSON.parse(sessionStorage.getItem(key)!)
  },
  set(key, data) {
    return sessionStorage.setItem(key, JSON.stringify(data))
  },
  remove(key) {
    return sessionStorage.removeItem(key)
  },
  clearAll() {
    return sessionStorage.clear()
  },
}

export default {
  locals,
  sessions,
  cookie,
}
