import isEqual from 'lodash.isequal';
import { createReducer } from '@reduxjs/toolkit';
import { actionLeftIsOpen,actionRightIsOpen,actionScrollChat,actionOpenPinned } from '../action';
import { IControlState,IPayloadLeftIsOpen,IPayloadRightIsOpen,IPayloadScrollChat,IPayloadOpenPinned } from '../../../typescript/redux/control/interfaces'

const initialState: IControlState = {
  leftIsOpen: '',
  rightIsOpen: '',
  scrollChat: false,
  openPinned: false,
}

const reducerControl = createReducer(initialState, {
  [actionLeftIsOpen.type]: (state, { payload: leftIsOpen }: IPayloadLeftIsOpen) => {
    if (isEqual(state.leftIsOpen, leftIsOpen) === false) return {...state,leftIsOpen}
    return state;    
  },  
  [actionRightIsOpen.type]: (state, { payload: rightIsOpen }: IPayloadRightIsOpen) => {
      if (isEqual(state.rightIsOpen, rightIsOpen) === false) return {...state,rightIsOpen}
    return state;  
  },
  [actionScrollChat.type]: (state, { payload: scrollChat }: IPayloadScrollChat) => {
    if (isEqual(state.scrollChat, scrollChat) === false) return {...state,scrollChat}
    return state;    
  },
  [actionOpenPinned.type]: (state, { payload: openPinned }: IPayloadOpenPinned) => {
    if (isEqual(state.openPinned, openPinned) === false) return {...state,openPinned}
    return state;    
  },  
});

export default reducerControl;
