import { WindowsAction , WindowsActionTypes , WindowsState , } from '../../types/redux/windows-reducer-types' ; const initialWindowsState = { openedWindows : [ ] , } ; function param { WindowsState } state param { object } action returns { object } export const windowsReducer = ( state = initialWindowsState , action ) : WindowsState => { switch ( action . type ) { case WindowsActionTypes . OPEN_WINDOW : const windowIsAlreadyOpened = state . openedWindows . some ( ( window ) => window . windowName === action . payload . windowName ) ; if ( windowIsAlreadyOpened ) return state ; return { ... state , openedWindows : [ ... state . openedWindows , action . payload ] , } ; case WindowsActionTypes . CLOSE_WINDOW : const withoutClosedWindow = state . openedWindows . map ( ( window ) => window . windowName === action . payload ? { ... window , isOpen : false , } : window ) ; return { ... state , openedWindows : withoutClosedWindow } ; case WindowsActionTypes . CLEAR_CLOSED_WINDOWS : const withoutRemovedWindows = state . openedWindows . filter ( ( window ) => window . windowName !== action . payload ) ; return { ... state , openedWindows : withoutRemovedWindows } ; default : return state ; } } ;