const DEFAULT_POLLING_INTERVAL_MS = 500;
export class ReviewContextWatcherService {
    gateway;
    pollingIntervalMs;
    watchers = new Map();
    lastProgress = new Map();
    constructor(gateway, pollingIntervalMs = DEFAULT_POLLING_INTERVAL_MS) {
        this.gateway = gateway;
        this.pollingIntervalMs = pollingIntervalMs;
    }
    start(localPath, mergeRequestId, callback) {
        const interval = setInterval(() => {
            const context = this.gateway.read(localPath, mergeRequestId);
            if (context) {
                const progressKey = JSON.stringify(context.progress);
                if (progressKey !== this.lastProgress.get(mergeRequestId)) {
                    this.lastProgress.set(mergeRequestId, progressKey);
                    callback(context.progress);
                    if (context.progress.phase === 'completed') {
                        this.stop(mergeRequestId);
                    }
                }
            }
        }, this.pollingIntervalMs);
        this.watchers.set(mergeRequestId, interval);
    }
    isWatching(mergeRequestId) {
        return this.watchers.has(mergeRequestId);
    }
    stop(mergeRequestId) {
        const interval = this.watchers.get(mergeRequestId);
        if (interval) {
            clearInterval(interval);
            this.watchers.delete(mergeRequestId);
            this.lastProgress.delete(mergeRequestId);
        }
    }
    stopAll() {
        for (const [mergeRequestId] of this.watchers) {
            this.stop(mergeRequestId);
        }
    }
}
//# sourceMappingURL=reviewContextWatcher.service.js.map