import axios from "axios";
import {
  result
} from "lodash";

export default {
  namespaced: true,
  state: {
    menuOptions: [],
    menuOption: {},
    optionTypes: []
  },
  mutations: {
    setMenuOption(state, menuOption) {
      state.menuOption = menuOption;
      if (state.menuOption.options) {
        state.menuOptionItems = state.menuOption.options;
      }
    },
    setMenuOptions(state, menuOptions) {
      state.menuOptions = menuOptions;
    },
    setOptionTypes(state, types) {
      state.optionTypes = types;
    },
    newMenuOption(state, item) {
      state.menuOptions = item;
      state.menuOption.options.push(item);
    },
    updateMenuOption(state, item) {
      state.menuOptions = item;
      var temp = state.menuOption.options.find(m => m.Id === item.id);
      temp = item;
    },
    removeMenuOption(state, menuOption) {
      state.menuOptions.pop(menuOption);
    },
    removeMenuOptionItem(state, item) {
      state.menuOption.options.pop(item);
    }
  },
  getters: {},
  actions: {
    getMenuOption({
      commit
    }, id) {
      axios.get(`/api/MenuOptions/${id}`).then(result => commit("setMenuOption", result.data));
    },
    getMenuOptions({
      commit
    }, restaurantId) {
      axios
        .get(`/api/MenuOptions/GetRestaurantMenuOptions/${restaurantId}`)
        .then(result => commit("setMenuOptions", result.data));
    },
    getOptionTypes({
      commit
    }) {
      axios
        .get('/api/MenuOptions/GetOptionTypes')
        .then(result => commit("setOptionTypes", result.data));
    },
    addMenuOption({
      commit
    }, menuOption) {
      axios
        .post("/api/MenuOptions", menuOption)
        .then(result => commit("newMenuOption", result.data))
    },
    updateMenuOption({
      commit
    }, menuOption) {
      axios
        .put("/api/MenuOptions", menuOption)
        .then(result => commit("updateMenuOption", result.data))
        .catch(console.error);
    },
    deleteMenuOption({
      commit
    }, menuOption) {
      axios
        .delete(`/api/MenuOptions/${menuOption.id}`)
        .then(result => commit("removeMenuOption", menuOption));
    },
    deleteMenuOptionItem({
      commit
    }, item) {
      axios
        .delete(`/api/MenuOptions/DeleteMenuOptionItem/${item.id}`)
        .then(result => commit("removeMenuOptionItem", item));
    }
  }
};
