import axios from 'axios'
import {
  sha256
} from 'js-sha256'

export default {
  namespaced: true,
  state: {
    userContainer: {},
    restaurantContainer: {}
  },
  mutations: {
    addUserContainer(state, userContainer) {
      state.userContainer = userContainer
    },
    addRestaurantContainer(state, restaurantContainer) {
      state.restaurantContainer = restaurantContainer;
    },
  },
  getters: {},
  actions: {
    getUserContainer({
      commit
    }) {
      axios
        .get('/api/registration/GetUserContainer')
        .then((result) => commit('addUserContainer', result.data))
        .catch(console.error)
    },
    regUser({
      commit
    }, user) {
      user.password = sha256(user.password + '≡∆≤≥√∞ProVision')
      axios
        .post('/api/registration/resgisterUser', user)
        .then((result) => commit('addUserContainer', result.data))
        .catch(console.error)
    },
    getRestaurantContainer({
      commit
    }) {
      axios
        .get('/api/registration/GetRestaurantContainer')
        .then((result) => commit('addRestaurantContainer', result.data))
        .catch(console.error)
    },
    regRestaurant({
      commit
    }, restaurant) {
      restaurant.userPassword = sha256(restaurant.userPassword + '≡∆≤≥√∞ProVision')
      axios
        .post('/api/registration/resgisterRestaurant', restaurant)
        .then((result) => commit('addRestaurantContainer', result.data))
        .catch(console.error)
    }
  }
}
