async function collectPatchEntries(source) {
    const entries = [];
    for await (const entry of source) {
        entries.push(entry);
    }
    return entries;
}
function patchWithinCeiling(entry, ceiling) {
    return ceiling === null || entry.patch.lamport <= ceiling;
}
function validTipSha(tipSha) {
    return typeof tipSha === 'string' && tipSha.length > 0;
}
/**
 * Collects patches for materialization.
 *
 * Abstracts away the patch loading, writer discovery, and checkpoint
 * resolution that MaterializeController needs. The adapter wraps
 * WarpRuntime's internal methods.
 */
export default class PatchCollector {
    /** Stream all patches for a single writer. */
    async *streamWriterPatches(writerId) {
        for (const entry of await this.loadWriterPatches(writerId)) {
            yield entry;
        }
    }
    /** Load patches for a frontier, filtered by optional ceiling. */
    async collectForFrontier(frontier, ceiling) {
        return await collectPatchEntries(this.streamForFrontier(frontier, ceiling));
    }
    /** Stream patches for a frontier, filtered by optional ceiling. */
    async *streamForFrontier(frontier, ceiling) {
        for (const writerId of frontier.keys()) {
            const tipSha = frontier.get(writerId);
            if (!validTipSha(tipSha)) {
                continue;
            }
            for (const entry of await this.loadPatchChain(tipSha)) {
                if (patchWithinCeiling(entry, ceiling)) {
                    yield entry;
                }
            }
        }
    }
    collectForFrontierSinceCoordinate(frontier, ceiling, baseCoordinate) {
        return collectPatchEntries(this.streamForFrontierSinceCoordinate(frontier, ceiling, baseCoordinate));
    }
    async *streamForFrontierSinceCoordinate(frontier, ceiling, _baseCoordinate) {
        yield* this.streamForFrontier(frontier, ceiling);
    }
    /** Stream patches since a checkpoint. */
    async *streamPatchesSince(checkpoint) {
        for (const entry of await this.loadPatchesSince(checkpoint)) {
            yield entry;
        }
    }
}
