import Cookies from 'js-cookie';
const TokenKey = 'Admin-Token';


const state = {
    token:getToken(),
    name: '',
    avatar: '',
    roles: [],
    permissions: []
};
const mutations = {
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
        state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
        state.permissions = permissions
    }
};
const actions = {
    setToken({ commit }, data){
        commit('SET_TOKEN', data)
    },
    setName({ commit }, data){
        commit('SET_NAME', data)
    },
    setAvatar({ commit }, data){
        commit('SET_AVATAR', data)
    },
    delToken({ commit, state }){
        commit('SET_TOKEN', '')
        commit('SET_NAME', '')
        commit('SET_AVATAR', '')
        commit('SET_ROLES', [])
        commit('SET_PERMISSIONS', [])
        removeToken()
    },
};
export default {
    state,
    mutations,
    actions
}
export function getToken() {
    return Cookies.get(TokenKey)
}
export function removeToken() {
    return Cookies.remove(TokenKey)
}
