use crate::Result;
use crate::currency::vocab::AM_LATN as CURRENCY_VOCAB;
use crate::currency::{Currency, assert_minor_unit};
use crate::language::{Kind, Language, form};
use crate::max::Max;
use crate::number::{Number, parse_cardinal, parse_currency, parse_ordinal, split_groups};
use crate::options::options;
use num_bigint::BigUint;
use num_traits::{One, ToPrimitive, Zero};
const ONES: [&str; 10] = [
"", "and", "hulet", "sost", "arat", "amist", "siddist", "sebat", "siment", "zeteny",
];
const TEENS: [&str; 10] = [
"asir",
"asra and",
"asra hulet",
"asra sost",
"asra arat",
"asra amist",
"asra siddist",
"asra sebat",
"asra siment",
"asra zeteny",
];
const TENS: [&str; 10] = [
"", "", "haya", "selasa", "arba", "hamsa", "silsa", "seba", "semanya", "zetena",
];
const HUNDRED: &str = "meto";
const THOUSAND: &str = "shi";
const ZERO: &str = "zero";
const NEGATIVE: &str = "asitegna";
const DECIMAL_SEP: &str = "netib";
const ORDINAL_SUFFIX: &str = "nya";
const FIRST: &str = "andenya";
const SCALE_WORDS: &[&str] = &["", THOUSAND, "miliyon", "billiyon"];
pub const CARDINAL_MAX: Max = Max::western(SCALE_WORDS.len() as u32 - 1);
pub const ORDINAL_MAX: Max = Max::western(SCALE_WORDS.len() as u32 - 1);
pub const CURRENCY_MAX: Max = Max::western(SCALE_WORDS.len() as u32 - 1);
fn build_segment(n: u32) -> String {
if n == 0 {
return String::new();
}
let ones = (n % 10) as usize;
let tens_digit = ((n / 10) % 10) as usize;
let hundreds_digit = (n / 100) as usize;
let mut parts: Vec<String> = Vec::new();
if hundreds_digit > 0 {
parts.push(format!("{} {HUNDRED}", ONES[hundreds_digit]));
}
if tens_digit == 1 {
parts.push(TEENS[ones].to_string());
} else {
if tens_digit > 1 {
parts.push(TENS[tens_digit].to_string());
}
if ones > 0 {
parts.push(ONES[ones].to_string());
}
}
parts.join(" ")
}
fn integer_to_words(n: &BigUint) -> String {
if n.is_zero() {
return ZERO.to_string();
}
if let Some(small) = n.to_u64() {
if small < 1000 {
return build_segment(small as u32);
}
}
build_large_number_words(n)
}
fn build_large_number_words(n: &BigUint) -> String {
let segments = split_groups(n, 1000);
let mut parts: Vec<String> = Vec::new();
for (scale_index, &segment) in segments.iter().enumerate().rev() {
if segment == 0 {
continue;
}
if scale_index == 0 {
parts.push(build_segment(segment));
} else {
let scale_word = SCALE_WORDS.get(scale_index).copied().unwrap_or("");
parts.push(format!("{} {scale_word}", build_segment(segment)));
}
}
parts.join(" ")
}
fn decimal_part_to_words(decimal: &str) -> String {
let digits: Vec<&str> = decimal
.bytes()
.map(|b| {
let d = (b - b'0') as usize;
if d == 0 { ZERO } else { ONES[d] }
})
.collect();
digits.join(" ")
}
pub fn to_cardinal<'a>(value: impl Into<Number<'a>>) -> Result<String> {
let number = parse_cardinal(&value.into())?;
CARDINAL_MAX.check(&number.integer, None)?;
let mut result = String::new();
if number.negative {
result.push_str(NEGATIVE);
result.push(' ');
}
result.push_str(&integer_to_words(&number.integer));
if let Some(fraction) = &number.fraction {
result.push(' ');
result.push_str(DECIMAL_SEP);
result.push(' ');
result.push_str(&decimal_part_to_words(fraction));
}
Ok(result)
}
fn integer_to_ordinal(n: &BigUint) -> String {
if n.is_one() {
return FIRST.to_string();
}
let mut cardinal = integer_to_words(n);
cardinal.push_str(ORDINAL_SUFFIX);
cardinal
}
pub fn to_ordinal<'a>(value: impl Into<Number<'a>>) -> Result<String> {
let n = parse_ordinal(&value.into())?;
ORDINAL_MAX.check(&n, None)?;
Ok(integer_to_ordinal(&n))
}
options! {
pub struct CurrencyOptions {
currency: Currency = Currency::ETB, table = CURRENCY_VOCAB;
}
}
pub fn to_currency<'a>(value: impl Into<Number<'a>>) -> Result<String> {
to_currency_with(value, &CurrencyOptions::default())
}
pub fn to_currency_with<'a>(
value: impl Into<Number<'a>>,
options: &CurrencyOptions,
) -> Result<String> {
let words = CURRENCY_VOCAB.words(options.currency)?;
let money = parse_currency(&value.into(), options.currency.minor_unit_digits())?;
CURRENCY_MAX.check(&money.major, None)?;
assert_minor_unit(money.minor, options.currency)?;
let mut result = String::new();
if money.negative {
result.push_str(NEGATIVE);
result.push(' ');
}
if !money.major.is_zero() || money.minor == 0 {
result.push_str(&integer_to_words(&money.major));
result.push(' ');
result.push_str(words.major_at(0));
}
if money.minor > 0 {
if !money.major.is_zero() {
result.push(' ');
}
result.push_str(&integer_to_words(&BigUint::from(money.minor)));
result.push(' ');
result.push_str(words.minor_at(0));
}
Ok(result)
}
pub static LANGUAGE: Language = Language {
code: "am-Latn-ET",
name: "Amharic (Latin, Ethiopia)",
kind: Kind::Full,
cardinal: Some(form!(plain to_cardinal, CARDINAL_MAX)),
ordinal: Some(form!(plain to_ordinal, ORDINAL_MAX)),
currency: Some(form!(with_options to_currency_with, CurrencyOptions, CURRENCY_MAX)),
};