import { randomBytes } from 'crypto';
export const PAIRING_CODE_LENGTH = 6;
/** @see connect/src/pairing-code.ts — keep in sync */
export const PAIRING_CODE_ALPHABET = 'ACDEFHJKMNPRTUVWXY3479';
const ALPHABET_SET = new Set(PAIRING_CODE_ALPHABET.split(''));
export function generatePairingCode() {
    const bytes = randomBytes(PAIRING_CODE_LENGTH);
    const n = PAIRING_CODE_ALPHABET.length;
    let out = '';
    for (let i = 0; i < PAIRING_CODE_LENGTH; i++) {
        out += PAIRING_CODE_ALPHABET[bytes[i] % n];
    }
    return out;
}
export function normalizePairingCode(raw) {
    const code = raw.replace(/[^a-zA-Z0-9]/g, '').toUpperCase();
    if (code.length !== PAIRING_CODE_LENGTH)
        return null;
    for (const ch of code) {
        if (!ALPHABET_SET.has(ch))
            return null;
    }
    return code;
}
