import { createSlice } from '@reduxjs/toolkit';

const initialState = {
  isActive: false,
  isSelectedAll: false,
  // if isSelectedAll === false used for includes video
  videoIds: [],
  // if isSelectedAll === true used for exclude video
  excludeVideoIds: [],

  monitoringIds: [],
  monitoringState: [],

  hubOptions: null,
};

export const slice = createSlice({
  name: '@@bulkActions',
  initialState,
  reducers: {
    setAction: (state, action) => {
      Object.keys(action.payload).forEach((key) => {
        state[key] = action.payload[key];
      });
    },
    selectVideoAction: (state, action) => {
      const { isSelectedAll } = state;
      state.videoIds = [].concat(state.videoIds, action.payload);
      if (isSelectedAll) {
        state.excludeVideoIds = state.excludeVideoIds.filter((id) => id !== action.payload);
      }
    },
    unselectVideoAction: (state, action) => {
      const { isSelectedAll } = state;
      state.videoIds = state.videoIds.filter((id) => id !== action.payload);
      if (isSelectedAll) {
        state.excludeVideoIds = [].concat(state.excludeVideoIds, action.payload);
      }
    },
    closeAction: (state) => {
      Object.keys(initialState).forEach((key) => {
        if (!['monitoringIds', 'monitoringState'].includes(key)) {
          state[key] = initialState[key];
        }
      });
    },
    clearAction: (state) => {
      Object.keys(initialState).forEach((key) => {
        state[key] = initialState[key];
      });
    },
    archiveAction: (state) => state,
    addTagsAction: (state) => state,
    replaceTagsAction: (state) => state,
    shareWithUsersAction: (state) => state,
    changeAccessLevelAction: (state) => state,
    getMonitoringStateAction: (state) => state,
    replaceHubsAction: (state) => state,
    getHubsOptionsAction: (state) => state,
  },
});

export const {
  setAction,
  selectVideoAction,
  unselectVideoAction,
  closeAction,
  clearAction,
  archiveAction,
  addTagsAction,
  replaceTagsAction,
  shareWithUsersAction,
  changeAccessLevelAction,
  getMonitoringStateAction,
  replaceHubsAction,
  getHubsOptionsAction,
} = slice.actions;
