/* eslint-disable prefer-destructuring */
import { SET_PHOTOS_TAB_BADGE } from '~/store/actionTypes';

const initialState = {
	photosTab: false,
	playlistThumbnail: false,
	playlistContent: false,
	playlistLastSeenTimestamp: '',
};

export const badges = (state = initialState, action) => {
	const { payload } = action;
	switch (action.type) {
		case SET_PHOTOS_TAB_BADGE: {
			let playlistUpdate;
			let playlistLike;
			let playlistLastSeenTimestamp;
			try {
				playlistUpdate = JSON.parse(payload.playlistUpdate || '{}');
				playlistLike = JSON.parse(payload.playlistLike || '{}');
			} catch (e) {
				playlistUpdate = payload.playlistUpdate;
				playlistLike = payload.playlistLike;
			}
			try {
				playlistLastSeenTimestamp = JSON.parse(payload.playlistLastSeenTimestamp || '{}');
			} catch (e) { playlistLastSeenTimestamp = payload.playlistLastSeenTimestamp; }
			return {
				...state,
				...payload,
				playlistUpdate,
				playlistLike,
				playlistLastSeenTimestamp,
			};
		}
		default:
			return state;
	}
};
