use std::collections::HashMap;
use num_bigint::BigUint;
use crate::error::{BasehError, ErrorCode};
pub(crate) fn encode_base_n(value: &BigUint, alphabet: &[char], length: usize) -> String {
let base = BigUint::from(alphabet.len() as u64);
let mut out = vec!['0'; length];
let mut v = value.clone();
for pos in (0..length).rev() {
let digit = (&v % &base)
.try_into()
.map(|d: u64| d as usize)
.unwrap_or(0);
out[pos] = alphabet[digit];
v /= &base;
}
out.into_iter().collect()
}
pub(crate) fn decode_base_n(
text: &[char],
alphabet_len: usize,
index: &HashMap<char, u32>,
) -> Result<BigUint, BasehError> {
let base = BigUint::from(alphabet_len as u64);
let mut value = BigUint::from(0u64);
for ch in text {
let digit = index.get(ch).ok_or_else(|| {
BasehError::customer(
ErrorCode::InvalidCharacter,
format!("Symbol {ch:?} is not in the alphabet"),
)
})?;
value = value * &base + BigUint::from(*digit);
}
Ok(value)
}