import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex);

export default new Vuex.Store({
	state: {
		count: 0,
		message: 'Get out me yard ya wankers',
		token: null
	},
	mutations: {
		increment (state) {
			state.count++
		},
		setLoginToken(state, token){
			
			localStorage.removeItem('loginToken') //We only want one login token per computer
			localStorage.setItem('loginToken', token)

			//Set default token to axios, every request will have header
			axios.defaults.headers.common['Authorization'] = token

			state.token = token
		},
		destroyLoginToken(state){

			//Remove login token from local storage and from headers
			localStorage.removeItem('loginToken')
			delete axios.defaults.headers.common['Authorization']
			state.token = null
		}
	},
	getters: {
		getRudeMessage: state => {
			return state.message
		},
		getLoginToken: state => {
			return state.token
		},
		getLoggedIn: state => {
			let weIn = (state.token !== null && state.token.length > 0)
			return weIn
		}
	}
})