import Users from '@helpers/Users/Users.js';

const path = import.meta.env.VITE_API_ADDR
const UsersService = new Users(path)

const initState = {
  usersList: [],
  componentState: 'disabled',
};

const state = {
  ...initState
};

const getters = {
  usersList:      (state) => state.usersList,
  componentState: (state) => state.componentState
};

const mutations = {
  setUsersList:      (state, payload) => state.usersList = payload,
  setComponentState: (state, payload) => state.componentState = payload
};

const actions = {
  fetchUsersList: async ({commit}, {siteId, mode}) => {
    const usersList = await UsersService.getUsersBySiteId(siteId, mode)
    commit('setUsersList', usersList)
    commit('setComponentState', 'active')
  },
  resetStore: ({state}) => {
    Object.entries(initState).forEach(([k, v]) => {
      state[k] = v
    })
  },
};

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