import * as _ from 'lodash-es';
const initialState = {
    errors: {},
    nowPlaying: null,
    playbackTimes: {}
};
export default function audioReducer(state = initialState, action) {
    switch (action.type) {
        case 'PLAY_AUDIO': {
            const audio = action.payload;
            const nowPlaying = { audio, durationRemaining: audio.duration };
            return { ...state, nowPlaying };
        }
        case 'PLAY_AUDIO_STARTED': {
            const audio = action.payload;
            if (audio.restrictedAudioId != null) {
                const listened = state.playbackTimes[audio.restrictedAudioId];
                const playbackTimes = {
                    ...state.playbackTimes,
                    [audio.restrictedAudioId]: listened ? listened + 1 : 1
                };
                const errors = _.omit(state.errors, [audio.src]);
                return { ...state, playbackTimes, errors };
            }
            else {
                return state;
            }
        }
        case 'SHOW_AUDIO_ERROR': {
            const { audio, error } = action.payload;
            const key = _.get(audio, 'restrictedAudioId', audio.src);
            const errors = {
                ...state.errors,
                [key]: error
            };
            return { ...state, errors, nowPlaying: null };
        }
        case 'HIDE_AUDIO_ERROR': {
            const audio = action.payload;
            const key = _.get(audio, 'restrictedAudioId', audio.src);
            const errors = _.omit(state.errors, [key]);
            return { ...state, errors };
        }
        case 'PLAY_AUDIO_FINISHED':
            return { ...state, nowPlaying: null };
        case 'UPDATE_REMAINING': {
            const durationRemaining = action.payload;
            const nowPlaying = { ...state.nowPlaying, durationRemaining };
            return { ...state, nowPlaying };
        }
        default:
            return state;
    }
}
//# sourceMappingURL=reducer.js.map