const initState = {
  modalMachines: [],
  lastMachinesByPackNumber: [],
};

const state = {
  ...initState
};

const getters = {
  modalMachines: (state) => state.modalMachines,
  lastMachinesByPackNumber: (state) => state.lastMachinesByPackNumber,
};

const mutations = {
  saveModalMachines: (state, updval) =>  state.modalMachines = updval,
  saveLastMachinesByPackNumber: (state, updval) =>  state.lastMachinesByPackNumber = updval,
  resetStore: (state) => {
    for (let key in initState) {
      state[key] = initState[key]
    }
  }
};


const actions = {
  setModalMachines: ({commit}, machines) => {
    try {
      commit('saveModalMachines', machines)
      return machines
    } catch (err) {
      console.error(err)
    }
  },
  setLastMachinesByPackNumber: async ({commit}, packs) => {
    try {
      commit('saveLastMachinesByPackNumber', packs)
    } catch (err) {
      console.error(err)
    }
  },
  resetStore: ({commit}) => {
    commit('resetStore')
  }
};

export const store = {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
};
