import isEqual from 'lodash.isequal';
import { createReducer } from '@reduxjs/toolkit';
import { IMessagesState, IMessagesPayload} from '../../../typescript/redux/messages/interfaces';
import {
  actionGetMessagesSuccess,
  actionGetMessagesReject
} from '../action';

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

const reducerMessages = createReducer(initialState, {
  [actionGetMessagesSuccess.type]: (state, { payload }: IMessagesPayload) => {
    if (isEqual(state, payload) === false) return payload
    return state;
  },
  [actionGetMessagesReject.type]: (state, _payload_) => {
    return state;
  },
});

export default reducerMessages;
