use crate::Result;
use crate::currency::vocab::FI 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] = [
"",
"yksi",
"kaksi",
"kolme",
"neljä",
"viisi",
"kuusi",
"seitsemän",
"kahdeksan",
"yhdeksän",
];
const TEENS: [&str; 10] = [
"kymmenen",
"yksitoista",
"kaksitoista",
"kolmetoista",
"neljätoista",
"viisitoista",
"kuusitoista",
"seitsemäntoista",
"kahdeksantoista",
"yhdeksäntoista",
];
const TENS: [&str; 10] = [
"",
"",
"kaksikymmentä",
"kolmekymmentä",
"neljäkymmentä",
"viisikymmentä",
"kuusikymmentä",
"seitsemänkymmentä",
"kahdeksankymmentä",
"yhdeksänkymmentä",
];
const HUNDRED: &str = "sata";
const THOUSAND: &str = "tuhat";
const ZERO: &str = "nolla";
const NEGATIVE: &str = "miinus";
const DECIMAL_SEP: &str = "pilkku";
const SCALES: &[&str] = &["miljoona", "miljardi", "biljoona", "triljoona"];
pub const CARDINAL_MAX: Max = Max::western(SCALES.len() as u32 + 1);
pub const ORDINAL_MAX: Max = Max::western(SCALES.len() as u32 + 1);
pub const CURRENCY_MAX: Max = Max::western(SCALES.len() as u32 + 1);
const ORDINAL_FIRST: &str = "ensimmäinen";
const ORDINAL_SECOND: &str = "toinen";
const ORDINAL_SUFFIX: &str = "s";
const ORDINAL_ONES: [&str; 10] = [
"",
"ensimmäinen",
"toinen",
"kolmas",
"neljäs",
"viides",
"kuudes",
"seitsemäs",
"kahdeksas",
"yhdeksäs",
];
const ORDINAL_TEENS: [&str; 10] = [
"kymmenes",
"yhdestoista",
"kahdestoista",
"kolmastoista",
"neljästoista",
"viidestoista",
"kuudestoista",
"seitsemästoista",
"kahdeksastoista",
"yhdeksästoista",
];
const ORDINAL_TENS: [&str; 10] = [
"",
"",
"kahdeskymmenes",
"kolmaskymmenes",
"neljäskymmenes",
"viideskymmenes",
"kuudeskymmenes",
"seitsemäskymmenes",
"kahdeksaskymmenes",
"yhdeksäskymmenes",
];
fn build_segment(n: u32) -> String {
if n == 0 {
return String::new();
}
let ones = (n % 10) as usize;
let tens = ((n / 10) % 10) as usize;
let hundreds = (n / 100) as usize;
let mut parts: Vec<String> = Vec::new();
if hundreds > 0 {
if hundreds == 1 {
parts.push(HUNDRED.to_string());
} else {
parts.push(format!("{} {HUNDRED}", ONES[hundreds]));
}
}
let tens_ones = n % 100;
if tens_ones == 0 {
} else if tens_ones < 10 {
parts.push(ONES[ones].to_string());
} else if tens_ones < 20 {
parts.push(TEENS[ones].to_string());
} else if ones == 0 {
parts.push(TENS[tens].to_string());
} else {
parts.push(format!("{}{}", TENS[tens], ONES[ones]));
}
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);
}
if small < 1_000_000 {
let thousands = (small / 1000) as u32;
let remainder = (small % 1000) as u32;
let mut result = if thousands == 1 {
THOUSAND.to_string()
} else {
format!("{} {THOUSAND}", build_segment(thousands))
};
if remainder > 0 {
result.push(' ');
result.push_str(&build_segment(remainder));
}
return result;
}
}
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;
}
let segment_word = build_segment(segment);
if scale_index == 0 {
parts.push(segment_word);
} else if scale_index == 1 {
if segment == 1 {
parts.push(THOUSAND.to_string());
} else {
parts.push(format!("{segment_word} {THOUSAND}"));
}
} else {
parts.push(format!("{segment_word} {}", SCALES[scale_index - 2]));
}
}
parts.join(" ")
}
fn decimal_part_to_words(decimal: &str) -> String {
decimal
.bytes()
.map(|digit| {
let d = (digit - b'0') as usize;
if d == 0 { ZERO } else { ONES[d] }
})
.collect::<Vec<_>>()
.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 build_ordinal_segment(n: u32) -> String {
if n == 0 {
return String::new();
}
if n < 10 {
return ORDINAL_ONES[n as usize].to_string();
}
if n < 20 {
return ORDINAL_TEENS[(n - 10) as usize].to_string();
}
let ones = (n % 10) as usize;
let tens = (n / 10) as usize;
if ones == 0 {
return ORDINAL_TENS[tens].to_string();
}
format!("{}{}", TENS[tens], ORDINAL_ONES[ones])
}
fn integer_to_ordinal(n: &BigUint) -> String {
if n.is_one() {
return ORDINAL_FIRST.to_string();
}
if *n == BigUint::from(2u32) {
return ORDINAL_SECOND.to_string();
}
if let Some(small) = n.to_u64().filter(|&small| small < 100) {
return build_ordinal_segment(small as u32);
}
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::EUR, 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(if money.major.is_one() { 0 } else { 1 }));
}
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(if money.minor == 1 { 0 } else { 1 }));
}
Ok(result)
}
pub static LANGUAGE: Language = Language {
code: "fi-FI",
name: "Finnish (Finland)",
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)),
};