import {
  mapGetters,
  mapActions,
  GetterTree,
  MutationTree,
  ActionTree,
} from 'vuex';

interface State {
  loading: boolean;
  objects: Array<any>;
  allChecksHaveBeenPassed: boolean;
}

export const state: State = {
  loading: false,
  objects: [],
  allChecksHaveBeenPassed: false,
};

export const getters: GetterTree<State, State> = {
  loading(state) {
    return state.loading;
  },
  objects(state) {
    return state.objects;
  },
  allChecksHaveBeenPassed(state) {
    return state.allChecksHaveBeenPassed;
  },
};

export const mutations: MutationTree<State> = {
  setLoading(state: State, payload: boolean) {
    state.loading = payload;
  },
  setObjects(state: State, payload) {
    state.objects = payload;
  },
  setPassedChecks(state, payload) {
    state.allChecksHaveBeenPassed = payload;
  },
};

export const actions: ActionTree<State, State> = {
  setLoading({ commit }, payload) {
    commit('setLoading', payload);
  },
  setObjects({ commit }, payload) {
    commit('setObjects', payload);
  },
  setPassedChecks({ commit }, payload) {
    commit('setPassedChecks', payload);
  },
};

export const baseCheckTableComputed = {
  ...mapGetters('commonComponents/baseChecksTable', [
    'loading',
    'objects',
    'allChecksHaveBeenPassed',
  ]),
};

export const baseCheckTableMethods = mapActions(
  'commonComponents/baseChecksTable',
  ['setLoading', 'setObjects', 'setPassedChecks']
);
