import { reducer as network } from 'react-native-offline';
import ReduxPersist from '~/config/ReduxPersist';
import { PURGE_STORE } from '~/store/actionTypes';
import { reducer as form } from 'redux-form';
import { locale } from '~/reducers/locale';
import { app } from '~/reducers/app';
import { user } from '~/reducers/user';
import { photos } from '~/reducers/photos';
import { users } from '~/reducers/users';
import { settings } from '~/reducers/settings';
import { google } from '~/reducers/google';
import { badges } from '~/reducers/badges';
import { avatar } from '~/reducers/avatar';
import { nav } from '~/reducers/nav';
import { loading } from '~/reducers/loading';
import { onboarding } from '~/reducers/onboarding';
import { socialData } from '~/reducers/socialData';
import { redirect } from '~/reducers/redirect';
import { beneficiaries } from '~/reducers/beneficiaries';

import { persistReducer } from 'redux-persist';
import { combineReducers } from 'redux';

// If we add a new reducer to this file then we should also update reducerVersion in
// src/config/ReduxPersis.js to prevent the app from crashing
// See here for more details: https://www.wrike.com/open.htm?id=294081663

const appReducer = combineReducers({
	app,
	user,
	users,
	locale,
	photos,
	form,
	google,
	settings,
	nav,
	network,
	badges,
	avatar,
	loading,
	onboarding,
	socialData,
	redirect,
	beneficiaries,
});

const rootReducer = persistReducer(ReduxPersist.persistConfig, appReducer);

/* eslint-disable */
// I added eslint disable to this block of code to prevent no param reassign.
// No other way to do it. Don't want to remove the no-assign-param in the eslint config.
export const reducer = (state, action) => {
	if (action.type === PURGE_STORE) {
		const { users, user, beneficiaries, photos } = action.payload;
		state = { user, users, beneficiaries, photos };
	}
	return rootReducer(state, action);
};
/* eslint-enable */
