const defaultState = {
  snackbar: {
    type: null,
    value: false,
    message: 'No message.',
    absolute: false,
    timeout: 3000,
    left: false
  },
  isLoading: false
}

const mutations = {
  SET_SNACKBAR (state, payload) {
    state.snackbar = {
      ...defaultState.snackbar,
      ...state.snackbar,
      ...payload
    }
  },
  SET_LOADING (state, payload) {
    state.isLoading = payload
  }
}

const actions = {
  setLoading ({ commit }, payload) {
    commit('SET_LOADING', payload)
  },
  setSnackbar ({ commit }, payload) {
    commit('SET_SNACKBAR', {
      ...defaultState.snackbar,
      ...payload
    })
  },
  showErrorMessage ({ commit }, payload) {
    commit('SET_SNACKBAR', {
      ...payload,
      value: true
    })
  },
  closeErrorMessage ({ commit }) {
    commit('SET_SNACKBAR', {
      value: false
    })
  }
}

const getters = {
  snackbar: (state) => state.snackbar,
  isLoading: (state) => state.isLoading
}

export default {
  namespaced: true,
  state: defaultState,
  actions,
  mutations,
  getters
}
