/* ENMonthNameParser.c generated by valac 0.56.19, the Vala compiler
 * generated from ENMonthNameParser.vala, do not modify */

/*
 * Copyright © 2025 Alain M. (https://github.com/alainm23/planify)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * Authored by: Alain M. <alainmh23@gmail.com>
 */

#include "chrono.h"
#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <math.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

enum  {
	CHRONO_EN_MONTH_NAME_PARSER_0_PROPERTY,
	CHRONO_EN_MONTH_NAME_PARSER_NUM_PROPERTIES
};
static GParamSpec* chrono_en_month_name_parser_properties[CHRONO_EN_MONTH_NAME_PARSER_NUM_PROPERTIES];
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_match_info_unref0(var) ((var == NULL) ? NULL : (var = (g_match_info_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_date_time_unref0(var) ((var == NULL) ? NULL : (var = (g_date_time_unref (var), NULL)))

struct _ChronoENMonthNameParserPrivate {
	GRegex* month_regex;
};

static gint ChronoENMonthNameParser_private_offset;
static gpointer chrono_en_month_name_parser_parent_class = NULL;

static void chrono_en_month_name_parser_finalize (GObject * obj);
static GType chrono_en_month_name_parser_get_type_once (void);

static inline gpointer
chrono_en_month_name_parser_get_instance_private (ChronoENMonthNameParser* self)
{
	return G_STRUCT_MEMBER_P (self, ChronoENMonthNameParser_private_offset);
}

ChronoENMonthNameParser*
chrono_en_month_name_parser_construct (GType object_type)
{
	ChronoENMonthNameParser * self = NULL;
	GError* _inner_error0_ = NULL;
	self = (ChronoENMonthNameParser*) g_object_new (object_type, NULL);
	{
		GRegex* _tmp0_ = NULL;
		GRegex* _tmp1_;
		GRegex* _tmp2_;
		_tmp1_ = g_regex_new ("\\b(\\d{1,2})\\s+(january|february|march|april|may|june|july|august|se" \
"ptember|october|november|december|jan|feb|mar|apr|jun|jul|aug|sep|sept" \
"|oct|nov|dec)\\b(?:\\s+(\\d{2,4}))?|" "\\b(january|february|march|april|may|june|july|august|september|octobe" \
"r|november|december|jan|feb|mar|apr|jun|jul|aug|sep|sept|oct|nov|dec)\\" \
"b(?:,?\\s+(\\d{4}))|" "\\b(january|february|march|april|may|june|july|august|september|octobe" \
"r|november|december|jan|feb|mar|apr|jun|jul|aug|sep|sept|oct|nov|dec)\\" \
"b\\s+(\\d{1,2})(?:,?\\s+(\\d{2,4}))?|" "\\b(january|february|march|april|may|june|july|august|september|octobe" \
"r|november|december|jan|feb|mar|apr|jun|jul|aug|sep|sept|oct|nov|dec)\\" \
"b", G_REGEX_CASELESS, 0, &_inner_error0_);
		_tmp0_ = _tmp1_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp2_ = _tmp0_;
		_tmp0_ = NULL;
		_g_regex_unref0 (self->priv->month_regex);
		self->priv->month_regex = _tmp2_;
		_g_regex_unref0 (_tmp0_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp3_;
		const gchar* _tmp4_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp3_ = e;
		_tmp4_ = _tmp3_->message;
		g_warning ("ENMonthNameParser.vala:41: Error creating month regex: %s", _tmp4_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return NULL;
	}
	return self;
}

ChronoENMonthNameParser*
chrono_en_month_name_parser_new (void)
{
	return chrono_en_month_name_parser_construct (CHRONO_TYPE_EN_MONTH_NAME_PARSER);
}

ChronoParseResult*
chrono_en_month_name_parser_parse (ChronoENMonthNameParser* self,
                                   const gchar* text)
{
	GMatchInfo* match = NULL;
	GRegex* _tmp0_;
	GMatchInfo* _tmp1_ = NULL;
	gboolean _tmp2_;
	gint day = 0;
	gint month = 0;
	gint year = 0;
	GDateTime* now = NULL;
	GDateTime* _tmp3_;
	GDateTime* _tmp4_;
	gchar* day_str = NULL;
	GMatchInfo* _tmp5_;
	gchar* _tmp6_;
	gboolean _tmp7_ = FALSE;
	const gchar* _tmp8_;
	gboolean _tmp81_ = FALSE;
	ChronoParseResult* _result_ = NULL;
	ChronoParseResult* _tmp82_;
	ChronoParseResult* _tmp83_;
	GDateTime* _tmp84_;
	GDateTime* _tmp85_;
	gint start_pos = 0;
	gint end_pos = 0;
	GMatchInfo* _tmp86_;
	gint _tmp87_ = 0;
	gint _tmp88_ = 0;
	ChronoParseResult* _tmp89_;
	ChronoParseResult* _tmp90_;
	ChronoParseResult* _tmp91_;
	GMatchInfo* _tmp92_;
	gchar* _tmp93_;
	gchar* _tmp94_;
	ChronoParseResult* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (text != NULL, NULL);
	_tmp0_ = self->priv->month_regex;
	_tmp2_ = g_regex_match (_tmp0_, text, 0, &_tmp1_);
	_g_match_info_unref0 (match);
	match = _tmp1_;
	if (!_tmp2_) {
		result = NULL;
		_g_match_info_unref0 (match);
		return result;
	}
	_tmp3_ = g_date_time_new_now_local ();
	now = _tmp3_;
	_tmp4_ = now;
	year = g_date_time_get_year (_tmp4_);
	day = 1;
	_tmp5_ = match;
	_tmp6_ = g_match_info_fetch (_tmp5_, 1);
	day_str = _tmp6_;
	_tmp8_ = day_str;
	if (_tmp8_ != NULL) {
		const gchar* _tmp9_;
		gint _tmp10_;
		gint _tmp11_;
		_tmp9_ = day_str;
		_tmp10_ = strlen (_tmp9_);
		_tmp11_ = _tmp10_;
		_tmp7_ = _tmp11_ > 0;
	} else {
		_tmp7_ = FALSE;
	}
	if (_tmp7_) {
		const gchar* _tmp12_;
		gint* month_val = NULL;
		GMatchInfo* _tmp13_;
		gchar* _tmp14_;
		gchar* _tmp15_;
		gint* _tmp16_;
		gint* _tmp17_;
		gint* _tmp18_;
		gint* _tmp19_;
		gchar* year_str = NULL;
		GMatchInfo* _tmp20_;
		gchar* _tmp21_;
		gboolean _tmp22_ = FALSE;
		const gchar* _tmp23_;
		_tmp12_ = day_str;
		day = atoi (_tmp12_);
		_tmp13_ = match;
		_tmp14_ = g_match_info_fetch (_tmp13_, 2);
		_tmp15_ = _tmp14_;
		_tmp16_ = chrono_en_constants_get_month (_tmp15_);
		_tmp17_ = _tmp16_;
		_g_free0 (_tmp15_);
		month_val = _tmp17_;
		_tmp18_ = month_val;
		if (_tmp18_ == NULL) {
			result = NULL;
			_g_free0 (month_val);
			_g_free0 (day_str);
			_g_date_time_unref0 (now);
			_g_match_info_unref0 (match);
			return result;
		}
		_tmp19_ = month_val;
		month = *_tmp19_;
		_tmp20_ = match;
		_tmp21_ = g_match_info_fetch (_tmp20_, 3);
		year_str = _tmp21_;
		_tmp23_ = year_str;
		if (_tmp23_ != NULL) {
			const gchar* _tmp24_;
			gint _tmp25_;
			gint _tmp26_;
			_tmp24_ = year_str;
			_tmp25_ = strlen (_tmp24_);
			_tmp26_ = _tmp25_;
			_tmp22_ = _tmp26_ > 0;
		} else {
			_tmp22_ = FALSE;
		}
		if (_tmp22_) {
			const gchar* _tmp27_;
			_tmp27_ = year_str;
			year = atoi (_tmp27_);
			if (year < 100) {
				year += 2000;
			}
		}
		_g_free0 (year_str);
		_g_free0 (month_val);
	} else {
		gchar* month_str = NULL;
		GMatchInfo* _tmp28_;
		gchar* _tmp29_;
		gboolean _tmp30_ = FALSE;
		const gchar* _tmp31_;
		_tmp28_ = match;
		_tmp29_ = g_match_info_fetch (_tmp28_, 4);
		month_str = _tmp29_;
		_tmp31_ = month_str;
		if (_tmp31_ != NULL) {
			const gchar* _tmp32_;
			gint _tmp33_;
			gint _tmp34_;
			_tmp32_ = month_str;
			_tmp33_ = strlen (_tmp32_);
			_tmp34_ = _tmp33_;
			_tmp30_ = _tmp34_ > 0;
		} else {
			_tmp30_ = FALSE;
		}
		if (_tmp30_) {
			gint* month_val = NULL;
			const gchar* _tmp35_;
			gint* _tmp36_;
			gint* _tmp37_;
			gint* _tmp38_;
			gchar* year_str = NULL;
			GMatchInfo* _tmp39_;
			gchar* _tmp40_;
			gboolean _tmp41_ = FALSE;
			const gchar* _tmp42_;
			_tmp35_ = month_str;
			_tmp36_ = chrono_en_constants_get_month (_tmp35_);
			month_val = _tmp36_;
			_tmp37_ = month_val;
			if (_tmp37_ == NULL) {
				result = NULL;
				_g_free0 (month_val);
				_g_free0 (month_str);
				_g_free0 (day_str);
				_g_date_time_unref0 (now);
				_g_match_info_unref0 (match);
				return result;
			}
			_tmp38_ = month_val;
			month = *_tmp38_;
			_tmp39_ = match;
			_tmp40_ = g_match_info_fetch (_tmp39_, 5);
			year_str = _tmp40_;
			_tmp42_ = year_str;
			if (_tmp42_ != NULL) {
				const gchar* _tmp43_;
				gint _tmp44_;
				gint _tmp45_;
				_tmp43_ = year_str;
				_tmp44_ = strlen (_tmp43_);
				_tmp45_ = _tmp44_;
				_tmp41_ = _tmp45_ > 0;
			} else {
				_tmp41_ = FALSE;
			}
			if (_tmp41_) {
				const gchar* _tmp46_;
				_tmp46_ = year_str;
				year = atoi (_tmp46_);
			}
			_g_free0 (year_str);
			_g_free0 (month_val);
		} else {
			GMatchInfo* _tmp47_;
			gchar* _tmp48_;
			gboolean _tmp49_ = FALSE;
			const gchar* _tmp50_;
			_tmp47_ = match;
			_tmp48_ = g_match_info_fetch (_tmp47_, 6);
			_g_free0 (month_str);
			month_str = _tmp48_;
			_tmp50_ = month_str;
			if (_tmp50_ != NULL) {
				const gchar* _tmp51_;
				gint _tmp52_;
				gint _tmp53_;
				_tmp51_ = month_str;
				_tmp52_ = strlen (_tmp51_);
				_tmp53_ = _tmp52_;
				_tmp49_ = _tmp53_ > 0;
			} else {
				_tmp49_ = FALSE;
			}
			if (_tmp49_) {
				gint* month_val = NULL;
				const gchar* _tmp54_;
				gint* _tmp55_;
				gint* _tmp56_;
				gint* _tmp57_;
				gchar* day_str2 = NULL;
				GMatchInfo* _tmp58_;
				gchar* _tmp59_;
				gboolean _tmp60_ = FALSE;
				const gchar* _tmp61_;
				gchar* year_str = NULL;
				GMatchInfo* _tmp66_;
				gchar* _tmp67_;
				gboolean _tmp68_ = FALSE;
				const gchar* _tmp69_;
				_tmp54_ = month_str;
				_tmp55_ = chrono_en_constants_get_month (_tmp54_);
				month_val = _tmp55_;
				_tmp56_ = month_val;
				if (_tmp56_ == NULL) {
					result = NULL;
					_g_free0 (month_val);
					_g_free0 (month_str);
					_g_free0 (day_str);
					_g_date_time_unref0 (now);
					_g_match_info_unref0 (match);
					return result;
				}
				_tmp57_ = month_val;
				month = *_tmp57_;
				_tmp58_ = match;
				_tmp59_ = g_match_info_fetch (_tmp58_, 7);
				day_str2 = _tmp59_;
				_tmp61_ = day_str2;
				if (_tmp61_ != NULL) {
					const gchar* _tmp62_;
					gint _tmp63_;
					gint _tmp64_;
					_tmp62_ = day_str2;
					_tmp63_ = strlen (_tmp62_);
					_tmp64_ = _tmp63_;
					_tmp60_ = _tmp64_ > 0;
				} else {
					_tmp60_ = FALSE;
				}
				if (_tmp60_) {
					const gchar* _tmp65_;
					_tmp65_ = day_str2;
					day = atoi (_tmp65_);
				}
				_tmp66_ = match;
				_tmp67_ = g_match_info_fetch (_tmp66_, 8);
				year_str = _tmp67_;
				_tmp69_ = year_str;
				if (_tmp69_ != NULL) {
					const gchar* _tmp70_;
					gint _tmp71_;
					gint _tmp72_;
					_tmp70_ = year_str;
					_tmp71_ = strlen (_tmp70_);
					_tmp72_ = _tmp71_;
					_tmp68_ = _tmp72_ > 0;
				} else {
					_tmp68_ = FALSE;
				}
				if (_tmp68_) {
					const gchar* _tmp73_;
					_tmp73_ = year_str;
					year = atoi (_tmp73_);
					if (year < 100) {
						year += 2000;
					}
				}
				_g_free0 (year_str);
				_g_free0 (day_str2);
				_g_free0 (month_val);
			} else {
				gint* month_val = NULL;
				GMatchInfo* _tmp74_;
				gchar* _tmp75_;
				gchar* _tmp76_;
				gint* _tmp77_;
				gint* _tmp78_;
				gint* _tmp79_;
				gint* _tmp80_;
				_tmp74_ = match;
				_tmp75_ = g_match_info_fetch (_tmp74_, 9);
				_tmp76_ = _tmp75_;
				_tmp77_ = chrono_en_constants_get_month (_tmp76_);
				_tmp78_ = _tmp77_;
				_g_free0 (_tmp76_);
				month_val = _tmp78_;
				_tmp79_ = month_val;
				if (_tmp79_ == NULL) {
					result = NULL;
					_g_free0 (month_val);
					_g_free0 (month_str);
					_g_free0 (day_str);
					_g_date_time_unref0 (now);
					_g_match_info_unref0 (match);
					return result;
				}
				_tmp80_ = month_val;
				month = *_tmp80_;
				_g_free0 (month_val);
			}
		}
		_g_free0 (month_str);
	}
	if (year < 1000) {
		_tmp81_ = year >= 100;
	} else {
		_tmp81_ = FALSE;
	}
	if (_tmp81_) {
		result = NULL;
		_g_free0 (day_str);
		_g_date_time_unref0 (now);
		_g_match_info_unref0 (match);
		return result;
	}
	_tmp82_ = chrono_parse_result_new ();
	_result_ = _tmp82_;
	_tmp83_ = _result_;
	_tmp84_ = g_date_time_new_local (year, month, day, 0, 0, (gdouble) 0);
	_tmp85_ = _tmp84_;
	chrono_parse_result_set_date (_tmp83_, _tmp85_);
	_g_date_time_unref0 (_tmp85_);
	_tmp86_ = match;
	g_match_info_fetch_pos (_tmp86_, 0, &_tmp87_, &_tmp88_);
	start_pos = _tmp87_;
	end_pos = _tmp88_;
	_tmp89_ = _result_;
	chrono_parse_result_set_start_index (_tmp89_, start_pos);
	_tmp90_ = _result_;
	chrono_parse_result_set_end_index (_tmp90_, end_pos);
	_tmp91_ = _result_;
	_tmp92_ = match;
	_tmp93_ = g_match_info_fetch (_tmp92_, 0);
	_tmp94_ = _tmp93_;
	chrono_parse_result_set_matched_text (_tmp91_, _tmp94_);
	_g_free0 (_tmp94_);
	result = _result_;
	_g_free0 (day_str);
	_g_date_time_unref0 (now);
	_g_match_info_unref0 (match);
	return result;
}

static void
chrono_en_month_name_parser_class_init (ChronoENMonthNameParserClass * klass,
                                        gpointer klass_data)
{
	chrono_en_month_name_parser_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ChronoENMonthNameParser_private_offset);
	G_OBJECT_CLASS (klass)->finalize = chrono_en_month_name_parser_finalize;
}

static void
chrono_en_month_name_parser_instance_init (ChronoENMonthNameParser * self,
                                           gpointer klass)
{
	self->priv = chrono_en_month_name_parser_get_instance_private (self);
}

static void
chrono_en_month_name_parser_finalize (GObject * obj)
{
	ChronoENMonthNameParser * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CHRONO_TYPE_EN_MONTH_NAME_PARSER, ChronoENMonthNameParser);
	_g_regex_unref0 (self->priv->month_regex);
	G_OBJECT_CLASS (chrono_en_month_name_parser_parent_class)->finalize (obj);
}

/**
     * English month name parser
     * 
     * Supports: 15 March, March 15, 15 Mar 2024, Mar 15 2024, January 2012, January, Jan
     */
 G_GNUC_NO_INLINE static GType
chrono_en_month_name_parser_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ChronoENMonthNameParserClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) chrono_en_month_name_parser_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ChronoENMonthNameParser), 0, (GInstanceInitFunc) chrono_en_month_name_parser_instance_init, NULL };
	GType chrono_en_month_name_parser_type_id;
	chrono_en_month_name_parser_type_id = g_type_register_static (G_TYPE_OBJECT, "ChronoENMonthNameParser", &g_define_type_info, 0);
	ChronoENMonthNameParser_private_offset = g_type_add_instance_private (chrono_en_month_name_parser_type_id, sizeof (ChronoENMonthNameParserPrivate));
	return chrono_en_month_name_parser_type_id;
}

GType
chrono_en_month_name_parser_get_type (void)
{
	static gsize chrono_en_month_name_parser_type_id__once = 0;
	if (g_once_init_enter (&chrono_en_month_name_parser_type_id__once)) {
		GType chrono_en_month_name_parser_type_id;
		chrono_en_month_name_parser_type_id = chrono_en_month_name_parser_get_type_once ();
		g_once_init_leave (&chrono_en_month_name_parser_type_id__once, chrono_en_month_name_parser_type_id);
	}
	return chrono_en_month_name_parser_type_id__once;
}

