import {createStore} from "vuex";
import {adapterOfUsers, adapterOfServices} from "@mocks/adapters.js";
import ServiceOfUsers from "@services/serviceOfUsers/Users";
import ServiceOfServices from "@services/serviceOfServices/Services";

const services = {
  namespaced: true,
  state: {
    services: [],
  },
  mutations: {
    setServices(state, services) {
      state.services = services
    },
  },
  getters: {
    selectedService: () => {
      return {id: 1, name: 'service1'}
    }
  },
  actions: {
    resetStore({state}) {
      state.services = []
    },
    saveServices({commit}, services) {
      commit('setServices', services)
    }
  },
}

const users = {
  namespaced: true,
  state: {
    siteUsers: [],
    usersWithoutSite: [],
  },
  mutations: {
    setSiteUsers(state, users) {
      state.siteUsers = users
    },
    setUsersWithoutSite(state, users) {
      state.usersWithoutSite = users
    },
  },
  getters: {
    siteUsers: state => {
      return state.siteUsers
    },
    usersWithoutSite: state => {
      return state.usersWithoutSite
    }
  },
  actions: {
    resetStore({state}) {
      state.siteUsers = []
    },
    saveSiteUsers({commit}, users) {
      commit('setSiteUsers', users)
    },
    saveUsersWithoutSite({commit}, users) {
      commit('setUsersWithoutSite', users)
    }
  },
}

const store = createStore({
  modules: {
    services,
    users,
  },
})

const serviceOfUsers    = new ServiceOfUsers(adapterOfUsers, store)
const serviceOfServices = new ServiceOfServices(adapterOfServices, store)

export {serviceOfUsers, serviceOfServices, store}