/**
 * Top-level command spellcheck (RUSH-2329).
 *
 * Candidates are plain strings from KNOWN_TOP_LEVEL_COMMANDS — never the live
 * commander registry — so an unknown / typo'd invocation does not pay for
 * registerAllEagerCommands (~250-330ms of dynamic import + module eval).
 */
/** Calculate the Levenshtein edit distance between two strings. */
export function levenshtein(a, b) {
    const m = a.length;
    const n = b.length;
    const dp = Array(m + 1)
        .fill(null)
        .map(() => Array(n + 1).fill(0));
    for (let i = 0; i <= m; i++)
        dp[i][0] = i;
    for (let j = 0; j <= n; j++)
        dp[0][j] = j;
    for (let i = 1; i <= m; i++) {
        for (let j = 1; j <= n; j++) {
            dp[i][j] =
                a[i - 1] === b[j - 1]
                    ? dp[i - 1][j - 1]
                    : 1 + Math.min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]);
        }
    }
    return dp[m][n];
}
/**
 * Closest top-level command name by Levenshtein distance.
 * Iterates candidates in first-seen order so ties break the same way as the
 * historical registerAllEagerCommands registration order (RUSH-2329).
 */
export function closestTopLevelCommand(unknown, candidates) {
    let closest = null;
    let minDist = Infinity;
    for (const cmd of candidates) {
        const dist = levenshtein(unknown, cmd);
        if (dist < minDist) {
            minDist = dist;
            closest = cmd;
        }
    }
    return { closest, minDist };
}
