import isEqual from 'lodash.isequal';
import { createReducer } from '@reduxjs/toolkit';
import { IAllMessagesState, IAllMessagesPayload} from '../../../typescript/redux/allMessages/interfaces';
import {
  actionGetAllMessagesSuccess,
  actionGetAllMessagesReject,
} from '../action';

const initialState: IAllMessagesState = {
  total: "0",
  limit: "0",
  page: "0",
  messages: [],
}

const reducerAllMessages = createReducer(initialState, {
  [actionGetAllMessagesSuccess.type]: (state, { payload }: IAllMessagesPayload) => {
    if (isEqual(state, payload) === false) return payload
    return state;
  },
  [actionGetAllMessagesReject.type]: (state, _payload) => {
    return state;
  },
});

export default reducerAllMessages;
