import isEqual from 'lodash.isequal';
import { createReducer } from '@reduxjs/toolkit';
import { IChatPayload } from '../../../typescript/redux/chat/interfaces';
import { TChat} from '../../../typescript/redux/chat/types';

import {
  actionSelectChat,
  actionGetChatById,
  actionRemoveChat
} from '../action';

const initialState: TChat = {
     name: '',
     lastName: '',
     originalName: '',
     originalLastName: '',
     avatarUrl: '',
     avatarsArr: [],
     color: '',
     online: '',
     lastMessage: '',
     lastMessageCreatedAt:'',
     mute: false,
     sort:false,
     seen: 0,
     seenCompanion: 0,
     total: 0,
     watched: false,
     typing: false,
     number: '',
     country: '',
     pinned: false,
     socketId: "",
     _id: '',
     companionId: '',
     owner: '',
     createdAt: '',
     updatedAt: '',
    __v: 0,
}

const reducerChat = createReducer(initialState, {
  [actionSelectChat.type]: (state, { payload }: IChatPayload) => {
    if (isEqual(state, payload) === false) return payload
    return state;
  },
  [actionGetChatById.type]: (state, { payload }: IChatPayload) => {
    if (isEqual(state, payload) === false) return payload
    return state;
  },
  [actionRemoveChat.type]: (_state,_payload) => {
    return initialState
  },    
});

export default reducerChat;
