import {
	LOGOUT_SUCCESS,
	GET_SETTINGS_SUCCESS,
	SET_SETTINGS_SUCCESS,
	SET_LANGUAGE_SETTING,
} from '~/store/actionTypes';
import i18n, { updateComponentText } from '~/locale/i18n';


export const initialState = {
	optimizeUpload: false,
	autoAcceptFriend: false,
	languageCode: 'en',
	defaultPlaylistId: -1,
};
export const settings = (state = initialState, action) => {
	switch (action.type) {
		case GET_SETTINGS_SUCCESS:
			return { ...state, ...action.payload.settings };
		case SET_SETTINGS_SUCCESS:
			return { ...state, ...action.payload.settings };
		case SET_LANGUAGE_SETTING:
			i18n.prevLocale = i18n.locale;
			i18n.locale = action.payload.locale;
			updateComponentText();
			return { ...state, languageCode: action.payload.locale, meta: { loading: false, error: '' } };
		case LOGOUT_SUCCESS:
			return { ...initialState, languageCode: state.languageCode };
		default:
			return state;
	}
};
