//! US EIN (employer ID) prefix validator.

use super::digits::collect_digits;
use crate::ValidationOutcome;

/// IRS-published valid EIN prefixes, sorted ascending for binary search.
const EIN_VALID_PREFIXES: &[u32] = &[
    1, 2, 3, 4, 5, 6, 10, 11, 12, 13, 14, 15, 16, 20, 21, 22, 23, 24, 25, 26, 27, 30, 31, 32, 33, 34, 35, 36, 37, 38,
    39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,
    71, 72, 73, 74, 75, 76, 77, 80, 81, 82, 83, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 98, 99,
];

/// US EIN (employer ID) prefix validator.
///
/// Format `NN-NNNNNNN`. The first two digits MUST appear in the IRS-published
/// valid-prefix list. Out-of-list prefixes are rejected.
pub(super) fn validate(candidate: &str) -> ValidationOutcome {
    let Some(d) = collect_digits::<9>(candidate) else {
        return ValidationOutcome::Invalid;
    };
    let prefix = d[0] * 10 + d[1];
    ValidationOutcome::from_bool(EIN_VALID_PREFIXES.binary_search(&prefix).is_ok())
}
