import { END, eventChannel } from 'redux-saga';
import { call, cancelled, put, take } from 'redux-saga/effects';
const countdownChannel = (duration) => eventChannel(emit => {
    const intervalId = setInterval(() => {
        duration--;
        if (duration > 0) {
            emit(duration);
        }
        else {
            emit(END);
            clearInterval(intervalId);
        }
    }, 1000);
    return () => clearInterval(intervalId);
});
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function* countdown(duration, updateAction) {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
    const channel = yield call(countdownChannel, duration);
    try {
        while (true) {
            // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
            const remaining = yield take(channel);
            yield put(updateAction(remaining));
        }
    }
    finally {
        if ((yield cancelled())) {
            channel.close();
        }
    }
}
//# sourceMappingURL=countdown.js.map