import { call, put, race, take, takeLatest } from 'redux-saga/effects';
import { countdown } from '../countdown';
import { allowCasCancelled, allowCasCountdown, allowCasSucceeded, updateCasRemaining } from './actions';
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function* performEnableCas(examServerApi, { payload }) {
    try {
        // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
        const status1 = yield call(examServerApi.setCasStatus, 'allowing');
        if (status1 === 'allowed') {
            yield put(allowCasSucceeded());
            return;
        }
        yield put(allowCasCountdown(payload));
        // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
        const { cancelled } = yield race({
            finished: call(countdown, payload, updateCasRemaining),
            cancelled: take('ALLOW_CAS_CANCELLED')
        });
        // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
        const status2 = yield call(examServerApi.setCasStatus, cancelled ? 'forbidden' : 'allowed');
        if (status2 === 'allowed') {
            yield put(allowCasSucceeded());
        }
        else if (!cancelled) {
            yield put(allowCasCancelled());
        }
    }
    catch (error) {
        console.error('Enabling CAS failed', error);
    }
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export default function* casSaga(examServerApi) {
    yield takeLatest('ALLOW_CAS', performEnableCas, examServerApi);
}
//# sourceMappingURL=sagas.js.map