import {
	GET_AVATAR_UPLOADPOLICY,
	GET_AVATAR_UPLOADPOLICY_SUCCESS,
	GET_AVATAR_UPLOADPOLICY_ERROR,
	GET_AVATAR,
	GET_AVATAR_SUCCESS,
	GET_AVATAR_ERROR,
	ACTIVATE_AVATAR,
	ACTIVATE_AVATAR_SUCCESS,
	ACTIVATE_AVATAR_ERROR,
	DELETE_AVATAR,
	DELETE_AVATAR_SUCCESS,
	DELETE_AVATAR_ERROR,
} from '~/store/actionTypes';
// import _ from 'lodash';
import { handleAvatarUriCache } from './helper';

export const initialState = {
	uploadPolicy: {},
	source: {},
	meta: {
		loading: false,
		error: '',
	},
};

export const avatar = (state = initialState, { type, payload }) => {
	switch (type) {
		case GET_AVATAR:
			return { ...state, meta: { ...state.meta, loading: true, error: '' } };
		case GET_AVATAR_ERROR:
			return { ...state, meta: { ...state.meta, loading: false, error: payload.error } };
		case GET_AVATAR_SUCCESS: {
			const url = handleAvatarUriCache(payload.url);
			return { ...state, source: { uri: url }, meta: { error: '', loading: false } };
		}
		case ACTIVATE_AVATAR:
			return { ...state, meta: { ...state.meta, loading: true, error: '' } };
		case ACTIVATE_AVATAR_ERROR:
			return { ...state, meta: { ...state.meta, loading: false, error: payload.error } };
		case ACTIVATE_AVATAR_SUCCESS:
			return { ...state, source: { activateResponse: payload.response } };
		case GET_AVATAR_UPLOADPOLICY:
			return { ...state, meta: { ...state.meta, loading: true, error: '' } };
		case GET_AVATAR_UPLOADPOLICY_ERROR:
			return { ...state, meta: { ...state.meta, loading: false, error: payload.error } };
		case GET_AVATAR_UPLOADPOLICY_SUCCESS:
			return { ...state, uploadPolicy: payload };
		case DELETE_AVATAR:
			return { ...state, meta: { ...state.meta, loading: true, error: '' } };
		case DELETE_AVATAR_ERROR:
			return { ...state, meta: { ...state.meta, loading: false, error: payload.error } };
		case DELETE_AVATAR_SUCCESS:
			return initialState;
		default:
			return state;
	}
};
