import { relative } from 'node:path';
export function toRepoRelativePath(repoRoot, targetPath) {
    return relative(repoRoot, targetPath).replaceAll('\\', '/');
}
export function toCleanupMode(dryRun) {
    return dryRun ? 'dry-run' : 'apply';
}
export function normalizeCleanupActions(actions) {
    return [...actions].sort((left, right) => {
        return (left.target.localeCompare(right.target) ||
            left.phase.localeCompare(right.phase) ||
            left.type.localeCompare(right.type) ||
            left.result.localeCompare(right.result) ||
            left.reason.localeCompare(right.reason));
    });
}
export function buildCleanupSummary({ scanned, issuesFound, actions, }) {
    const normalizedActions = normalizeCleanupActions(actions);
    return {
        scanned,
        issuesFound,
        planned: normalizedActions.filter((action) => action.result === 'planned')
            .length,
        applied: normalizedActions.filter((action) => action.result === 'applied')
            .length,
        skipped: normalizedActions.filter((action) => action.result === 'skipped')
            .length,
        blocked: normalizedActions.filter((action) => action.result === 'blocked')
            .length,
    };
}
export function createCleanupPayload({ status, dryRun, scanned, issuesFound, actions, }) {
    const normalizedActions = normalizeCleanupActions(actions);
    return {
        status,
        mode: toCleanupMode(dryRun),
        summary: buildCleanupSummary({
            scanned,
            issuesFound,
            actions: normalizedActions,
        }),
        actions: normalizedActions,
    };
}
