/**
 * Created by yaohx_169 on 2017/6/8.
 */
import { cookie } from './config';
import store from './store';

export async function getToken() {
  return store.getItem(cookie.token);
}

export async function setToken(token) {
  return store.setItem(cookie.token, token);
}

export async function delToken() {
  return store.removeItem(cookie.token);
}

export async function getUser() {
  return {
    id: await store.getItem(cookie.userId),
    name: await store.getItem(cookie.userName),
  };
}

export async function setUser(id, name) {
  await store.setItem(cookie.userId, id);
  await store.setItem(cookie.userName, name);
}

export async function delUser() {
  await store.removeItem(cookie.userId);
  await store.removeItem(cookie.userName);
}

export async function getDomain() {
  return {
    name: await store.getItem(cookie.domainName),
    path: await store.getItem(cookie.domainPath),
  };
}

export async function setDomain(name, path) {
  await store.setItem(cookie.domainName, name);
  await store.setItem(cookie.domainPath, path);
}

export async function delDomain() {
  await store.removeItem(cookie.domainName);
  await store.removeItem(cookie.domainPath);
}

export async function isAuthed() {
  return getToken().then(result => !!result);
}

export async function hasDomain() {
  return getDomain().then(result => !!result);
}
