import {Services, Service} from './helpers'

const initState = {
  
  isLoading: false,

  selectedService: null,

  servicesDefault: [],
  services: [],
};

const state = {
  ...initState
};

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

  selectedService: (state) => state.selectedService,

  servicesDefault: (state) => state.servicesDefault,
  services: (state) => state.services,
};

const mutations = {
  setIsLoading: (state, updval) =>  state.isLoading = updval,

  setSelectedService: (state, updval) =>  state.selectedService = updval,

  setServicesDefault: (state, updval) =>  state.servicesDefault = updval,
  setServices: (state, updval) =>  state.services = updval,

  resetStore: (state) => {
    Object.entries(initState).forEach(([k,v]) => {
      state[k] = v
    })
  },
};

const actions = {
  uploadData: async ({commit}) => {
    try{
      const servicesNew = new Services()
      const services = servicesNew.getServices()
      const updatedServices = servicesNew.serviceDetales(services)

      commit('setServicesDefault', services)
      commit('setServices', updatedServices)
    } catch (err) {
      console.error(err)
    }
  },

  uploadServiceData: async (service) => {
    try{
      const serviceNew = new Service(service)
      serviceNew.getCashe(service)
    } catch (err) {
      console.error(err)
    }
  },

  resetStore: ({commit}) => {
    commit('resetStore')
  }
};

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