import Cookies from 'js-cookie'
import config from '../config'
import {USER_MAP_LOCAL,USER_MAP_REGISTER} from '../mock/login'


export const title = function (title) {
    title = title ? title + ' - Home' : 'iView project';
    window.document.title = title;
};

export const TOKEN_KEY = 'token'
const {cookieExpires} = config;

export function  handleUserInfo(){
    let USER_MAP_TEMP = getRegisterUserMap()
    USER_MAP_TEMP=JSON.parse(USER_MAP_TEMP)
    if(USER_MAP_TEMP.length<2){
        USER_MAP_TEMP = getLocalUserMap()
        USER_MAP_TEMP=JSON.parse(USER_MAP_TEMP)
        // console.log(USER_MAP_TEMP)
    }
    return USER_MAP_TEMP;
}
export const local = "MAP_LOCAL"
export const setLocalUserMap = () => {
    Cookies.set(local, USER_MAP_LOCAL, { expires: cookieExpires || 1 })
}

export const getLocalUserMap = () => {
    const USER_MAP_LOCAL = Cookies.get(local)
    if (USER_MAP_LOCAL) return USER_MAP_LOCAL
    else return false
}

export const register = "MAP_REGISTER"
export const setRegisterUserMap = () => {
    Cookies.set(register,USER_MAP_REGISTER, { expires: cookieExpires || 1 })
}

export const getRegisterUserMap = () => {
    const USER_MAP_REGISTER= Cookies.get(register)
    if (USER_MAP_REGISTER) return USER_MAP_REGISTER
    else return false
}

export const setToken = (token) => {
    Cookies.set(TOKEN_KEY, token, { expires: cookieExpires || 1 })
}

export const getToken = () => {
    const token = Cookies.get(TOKEN_KEY)
    if (token) return token
    else return false
}



