import { call, delay, put, select, takeEvery } from 'redux-saga/effects';
import { countdown } from '../countdown';
import { getPlaybackTimes } from '../selectors';
import { hideAudioError, playAudioFinished, playAudioStarted, showAudioError, updateRemaining } from './actions';
async function loadAndPlayRestrictedAudio(examServerApi, restrictedAudioId, audio, playbackTimes) {
    try {
        const response = await examServerApi.getRestrictedAudio('', restrictedAudioId, playbackTimes);
        const source = audio.querySelector('source');
        source.src = URL.createObjectURL(response);
        audio.load();
        void audio.play();
        return {
            response: 'ok',
            cleanup: () => {
                URL.revokeObjectURL(source.src);
            }
        };
    }
    catch (e) {
        console.log('Loading and playing restricted audio failed', e);
        return { response: 'other-error', cleanup: () => { } };
    }
}
function handleAbitti2Audio(examServerApi, audio, playbackTimes) {
    if (playbackTimes != null && audio.restrictedAudioId != null) {
        return loadAndPlayRestrictedAudio(examServerApi, audio.restrictedAudioId, audio.audioRef.current, playbackTimes);
    }
    else {
        void audio.audioRef.current.play();
        return { response: 'ok' };
    }
}
function* handleAbitti1Audio(examServerApi, audio, playbackTimes) {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
    const response = playbackTimes != null && audio.restrictedAudioId != null
        ? yield call(examServerApi.playRestrictedAudio, audio.src, audio.restrictedAudioId, playbackTimes)
        : yield call(examServerApi.playAudio, audio.src);
    return { response };
}
function* performPlayAudio(examServerApi, action) {
    const audio = action.payload;
    try {
        // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
        const playbackTimes = audio.restrictedAudioId != null ? yield select(getPlaybackTimes(audio.restrictedAudioId)) : undefined;
        // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
        const result = audio.audioRef && audio.audioRef.current
            ? yield handleAbitti2Audio(examServerApi, audio, playbackTimes)
            : yield handleAbitti1Audio(examServerApi, audio, playbackTimes);
        if (result.response === 'ok') {
            yield put(playAudioStarted(audio));
            yield call(countdown, audio.duration, updateRemaining);
            yield put(playAudioFinished());
        }
        else {
            yield put(showAudioError(audio, result.response));
            yield delay(5000);
            yield put(hideAudioError(audio));
        }
        if (result.cleanup) {
            result.cleanup();
        }
    }
    catch (error) {
        console.error('Playing audio failed', error);
    }
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export default function* audioSaga(examServerApi) {
    yield takeEvery('PLAY_AUDIO', performPlayAudio, examServerApi);
}
//# sourceMappingURL=sagas.js.map