import { createStore } from "vuex";

export default createStore({
  state: {
    user: {},
    isLoading: false,
    loadingTask: null,
    flashMessage: null,
    selectedCompany: JSON.parse(localStorage.getItem("_company")) || null,
    companies:[],
    routes:{}
  },
  getters: {
    user: (state) => state.user || {},
  },
  mutations: {
    SET_CURRENT_USER(state, user) {
      state.user = user;
    },
    SET_AJAX_TASK(state, task) {
      state.loadingTask = task;
    },
    SET_FLASH_MESSAGE(state, message) {
      state.flashMessage = message;
    },
    REQUEST_RESOLVED(state) {
      state.isLoading = false;
      state.loadingTask = null;
      if (state.hasActiveModal) {
        state.hasActiveModal = false;
        document.querySelector("#alert-container").remove();
      }
    },
    REGISTER_CONFIRM_MODAL(state) {
      state.loadingTask = "HANDLING_ALERT_ACTION";
      state.hasActiveModal = true;
    },
  },
  actions: {},
  modules: {},
});
