use std::sync::LazyLock;
use crate::common::word_boundary::{WithWordBoundary, WordBoundaryParser};
use crate::core::chrono::{Extracted, Parser};
use crate::core::components::Component;
use crate::core::configuration::Flags;
use crate::core::context::ParsingContext;
use crate::core::regex::{JsRegex, RegexMatch};
use crate::core::utils::pattern::{dict_get, dict_keys, match_any_pattern};
use crate::locales::vi::constants::{parse_year, MONTH_DICTIONARY, YEAR_PATTERN};
pub const UPSTREAM: &str = "VIMonthYearParser";
pub fn create(_flags: &Flags) -> Box<dyn Parser> {
Box::new(WithWordBoundary::new(ViMonthYearParser))
}
static PATTERN: LazyLock<JsRegex> = LazyLock::new(|| {
JsRegex::new(
&format!(
r"({})(?:\s*(?:năm|/)\s*({}))?(?=\W|$)",
match_any_pattern(dict_keys(MONTH_DICTIONARY)),
YEAR_PATTERN
),
"i",
)
});
const MONTH_GROUP: usize = 1;
const YEAR_GROUP: usize = 2;
struct ViMonthYearParser;
impl WordBoundaryParser for ViMonthYearParser {
fn inner_pattern(&self, _context: &ParsingContext) -> JsRegex {
PATTERN.clone()
}
fn inner_extract(&self, context: &ParsingContext, m: &mut RegexMatch) -> Extracted {
let Some(month) = dict_get(MONTH_DICTIONARY, &m.get(MONTH_GROUP).unwrap_or("").to_lowercase()) else {
return Extracted::None;
};
let mut result = context.create_parsing_result(m.index, m.text(), None, None);
result.start.assign(Component::Month, month);
result.start.imply(Component::Day, 1);
if let Some(year_text) = m.get(YEAR_GROUP).filter(|s| !s.is_empty()) {
match parse_year(year_text) {
Some(year) => {
result.start.assign(Component::Year, year);
}
None => return Extracted::None,
}
} else {
result.start.imply(
Component::Year,
context.reference.get_date_with_adjusted_timezone().get_full_year(),
);
}
Extracted::Result(result)
}
}