import { normalizeToPosixPath } from '../fs/paths.js';
function normalizeProviderPath(pathValue) {
    const trimmed = pathValue.trim();
    if (!trimmed) {
        return undefined;
    }
    const normalized = normalizeToPosixPath(trimmed)
        .replace(/\/+$/, '')
        .replace(/^\.\//, '');
    return normalized && normalized !== '.' ? normalized : undefined;
}
function resolveKnownStrayPaths(knownStrays) {
    const paths = [...(knownStrays?.project ?? []), ...(knownStrays?.user ?? [])];
    return new Set(paths
        .map((pathValue) => normalizeProviderPath(pathValue))
        .filter((pathValue) => pathValue !== undefined));
}
function isKnownStrayReport(report, knownStrayPaths) {
    if (report.state.status !== 'stray') {
        return false;
    }
    const providerPath = normalizeProviderPath(report.providerPath);
    return providerPath !== undefined && knownStrayPaths.has(providerPath);
}
export function filterKnownStrays(options) {
    const knownStrayPaths = resolveKnownStrayPaths(options.knownStrays);
    if (knownStrayPaths.size === 0) {
        return {
            reports: [...options.reports],
            candidates: [...options.candidates],
        };
    }
    return {
        reports: options.reports.filter((report) => !isKnownStrayReport(report, knownStrayPaths)),
        candidates: options.candidates.filter((candidate) => !isKnownStrayReport(candidate.report, knownStrayPaths)),
    };
}
