import routeOptions from './routeOptions.json'
// import Services from '@helpers/Services/Services.js';
// import {config} from './StaticData.js'
import {isEmpty} from "ramda";

const initState = {
  isSaveData: false,
  services: [],
  servicesState: "loading",

  selectedServiceState: "await",
  selectedService: null,

  newService: null,
};

const state = {
  ...initState
};

const getters = {
  isSaveData:         (state) => state.isSaveData,
  services:              (state) => state.newService ? [state.newService, ...state.services] : state.services,
  routes:             (state) => state.routes,
  routesLib:          (state) => state.routesLib,
  selectedServiceState:  (state) => state.selectedServiceState,
  servicesState:         (state) => state.servicesState,
  newRoute:           (state) => state.newRoute,
  routeOptions:       () => routeOptions,
  selectedService:       (state) => state.selectedService,
};

const mutations = {
  setIsSaveData: (state, payload) => state.isSaveData = payload,
  setServices: (state, payload) => state.services = payload,
  setServicesState: (state, payload) => state.servicesState = payload,
  setSelectedService: (state, payload) => state.selectedService = payload,
  setNewService: (state, payload) => state.newService = payload,
};


const actions = {
  addedNewServiceLayout: ({commit, getters}, newService) => {
    console.log('newSevice', newService)
    commit('setServices', [newService,...getters.services])
    commit('setSelectedService', newService)
    // commit('setRoutes', [])
    // commit('setRoutesState', "active")
  },
  createNewService: async ({commit}, updatedServices) => {
    if (!isEmpty(updatedServices)) {
      // const newService = updatedSites[0]
      // const updatedRoutes = addedNewRoute(newService, state.routes)
      // commit('setRoutes', updatedRoutes)
      // dispatch('updateRoutesWithApi', newService)
      return commit('setServices', updatedServices)
    }
  },
  breakeAddingSite: ({commit}) => {
    commit('setSelectedService', null)
  },
  changeSelectedService: async ({commit}, siteProps) => {
    commit('setSelectedService', siteProps)
  },
  editSelectedService: async ({commit}, selectedService) => {
    // const selectedSite = getters.selectedSite
    // selectedSite[payload.key] = payload.value
    console.log('selectedService', selectedService)
    commit('setSelectedService', selectedService)
  },
  changeIsSaveData: ({commit}, payload) => {
    commit('setIsSaveData', payload)
  },
  saveServices: async ({commit}, services) => {
    // const sites = await services.getServices()
    commit('setServices', services)
    commit('setServicesState', 'active')
    console.log('services', services)
  },
  saveService: async ({commit}, updatedServices) => {
    // commit('setIsSaveData', false)
    // commit('setSelectedService', null)
    
    commit('setServices', updatedServices)
  },
  cancelSelectedService: ({commit}, updatedServices) => {
    commit('setSelectedService', null)
    if (updatedServices) {
      commit('setServices', updatedServices)
      commit('setNewService', null)
    } 
    // commit('setRoutesState', "await")
  },
  removeService: async ({commit}, updatedSites) => {
    // const deletedSiteId = await services.deleteService(id)
    // const updatedSites = deletedSite(deletedSiteId, getters.sites)
    if (!isEmpty(updatedSites)) return commit('setServices', updatedSites)
  },

  uploadAndSelectService: ({state}, id) => {
    state.selectedSite = state.services.find(site => site.id === id)
    state.selectedSiteState = 'active'
  },
  resetStore: ({state}) => {
    Object.entries(initState).forEach(([k,v]) => {
      state[k] = v
    })
  },
};

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