import { getCheckpointManager } from './checkpoint-manager.js';
export async function resumablePromiseAll(promises, parallelId) {
    const checkpointManager = getCheckpointManager();
    const checkpoint = await checkpointManager.load(parallelId);
    const startIndex = checkpoint?.currentIndex || 0;
    const results = checkpoint?.results || [];
    for (let i = startIndex; i < promises.length; i++) {
        results[i] = (await promises[i]);
        const newCheckpoint = {
            loopId: parallelId,
            currentIndex: i + 1,
            results: results,
            timestamp: Date.now(),
        };
        await checkpointManager.save(newCheckpoint);
    }
    await checkpointManager.clear(parallelId);
    return results;
}
export async function resumablePromiseAllSettled(promises, parallelId) {
    const checkpointManager = getCheckpointManager();
    const checkpoint = await checkpointManager.load(parallelId);
    const startIndex = checkpoint?.currentIndex || 0;
    const results = checkpoint?.results || [];
    for (let i = startIndex; i < promises.length; i++) {
        try {
            const value = (await promises[i]);
            results[i] = { status: 'fulfilled', value };
        }
        catch (reason) {
            results[i] = { status: 'rejected', reason };
        }
        const newCheckpoint = {
            loopId: parallelId,
            currentIndex: i + 1,
            results: results,
            timestamp: Date.now(),
        };
        await checkpointManager.save(newCheckpoint);
    }
    await checkpointManager.clear(parallelId);
    return results;
}
//# sourceMappingURL=resumable-parallel.js.map