const ONE_DAY_MS = 24 * 60 * 60 * 1000;
const STALE_THRESHOLD_MS = 7 * ONE_DAY_MS;
function buildTrackedMrId(identity) {
    return `${identity.platform}-${identity.projectPath}-${identity.mrNumber}`;
}
function findTrackedMr(identity, trackingGateway, repositories) {
    const mrId = buildTrackedMrId(identity);
    for (const repository of repositories) {
        if (!repository.enabled)
            continue;
        const tracked = trackingGateway.getById(repository.localPath, mrId);
        if (tracked)
            return tracked;
    }
    return null;
}
function isClosedOrMergedOverThreshold(tracked, now) {
    if (tracked.state !== 'merged' && tracked.state !== 'closed')
        return false;
    const mergedAt = tracked.mergedAt;
    if (mergedAt === null)
        return true;
    const mergedTime = Date.parse(mergedAt);
    if (!Number.isFinite(mergedTime))
        return true;
    return now.getTime() - mergedTime > ONE_DAY_MS;
}
function isStale(entry, now) {
    return now.getTime() - entry.mtime.getTime() > STALE_THRESHOLD_MS;
}
export async function sweepStaleWorktrees(deps) {
    const entries = await deps.listEntries();
    const repositories = deps.getRepositories();
    const now = deps.now();
    let removedCount = 0;
    let failures = 0;
    for (const entry of entries) {
        const tracked = findTrackedMr(entry.identity, deps.trackingGateway, repositories);
        const shouldRemove = tracked === null
            || isClosedOrMergedOverThreshold(tracked, now)
            || isStale(entry, now);
        if (!shouldRemove)
            continue;
        try {
            const result = await deps.removeWorktree(entry.identity);
            if (result.status === 'removed') {
                removedCount += 1;
            }
            else if (result.status === 'failed') {
                failures += 1;
            }
        }
        catch {
            failures += 1;
        }
    }
    return {
        scanned: entries.length,
        removed: removedCount,
        failures,
    };
}
//# sourceMappingURL=sweepStaleWorktrees.usecase.js.map