/* ENRelativeDateFormatParser.c generated by valac 0.56.19, the Vala compiler
 * generated from ENRelativeDateFormatParser.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_RELATIVE_DATE_FORMAT_PARSER_0_PROPERTY,
	CHRONO_EN_RELATIVE_DATE_FORMAT_PARSER_NUM_PROPERTIES
};
static GParamSpec* chrono_en_relative_date_format_parser_properties[CHRONO_EN_RELATIVE_DATE_FORMAT_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 _ChronoENRelativeDateFormatParserPrivate {
	GRegex* relative_regex;
};

static gint ChronoENRelativeDateFormatParser_private_offset;
static gpointer chrono_en_relative_date_format_parser_parent_class = NULL;

static GDateTime* chrono_en_relative_date_format_parser_add_time_unit (ChronoENRelativeDateFormatParser* self,
                                                                GDateTime* date,
                                                                ChronoTimeUnit unit,
                                                                gint amount);
static void chrono_en_relative_date_format_parser_finalize (GObject * obj);
static GType chrono_en_relative_date_format_parser_get_type_once (void);

static inline gpointer
chrono_en_relative_date_format_parser_get_instance_private (ChronoENRelativeDateFormatParser* self)
{
	return G_STRUCT_MEMBER_P (self, ChronoENRelativeDateFormatParser_private_offset);
}

ChronoENRelativeDateFormatParser*
chrono_en_relative_date_format_parser_construct (GType object_type)
{
	ChronoENRelativeDateFormatParser * self = NULL;
	GError* _inner_error0_ = NULL;
	self = (ChronoENRelativeDateFormatParser*) g_object_new (object_type, NULL);
	{
		GRegex* _tmp0_ = NULL;
		GRegex* _tmp1_;
		GRegex* _tmp2_;
		_tmp1_ = g_regex_new ("(this|last|past|next|after\\s+this)\\s+(second|seconds|minute|minutes|" \
"hour|hours|day|days|week|weeks|month|months|quarter|quarters|year|year" \
"s|s|sec|m|min|mins|h|hr|hrs|d|w|mo|mon|mos|qtr|y|yr)(?=\\W|$)", 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->relative_regex);
		self->priv->relative_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 ("ENRelativeDateFormatParser.vala:38: Error creating relative date 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;
}

ChronoENRelativeDateFormatParser*
chrono_en_relative_date_format_parser_new (void)
{
	return chrono_en_relative_date_format_parser_construct (CHRONO_TYPE_EN_RELATIVE_DATE_FORMAT_PARSER);
}

static gchar*
string_replace (const gchar* self,
                const gchar* old,
                const gchar* replacement)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (old != NULL, NULL);
	g_return_val_if_fail (replacement != NULL, NULL);
	if ((*((gchar*) self)) == '\0') {
		_tmp1_ = TRUE;
	} else {
		_tmp1_ = (*((gchar*) old)) == '\0';
	}
	if (_tmp1_) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = g_strcmp0 (old, replacement) == 0;
	}
	if (_tmp0_) {
		gchar* _tmp2_;
		_tmp2_ = g_strdup (self);
		result = _tmp2_;
		return result;
	}
	{
		GRegex* regex = NULL;
		gchar* _tmp3_;
		gchar* _tmp4_;
		GRegex* _tmp5_;
		GRegex* _tmp6_;
		gchar* _tmp7_ = NULL;
		GRegex* _tmp8_;
		gchar* _tmp9_;
		gchar* _tmp10_;
		_tmp3_ = g_regex_escape_string (old, -1);
		_tmp4_ = _tmp3_;
		_tmp5_ = g_regex_new (_tmp4_, 0, 0, &_inner_error0_);
		_tmp6_ = _tmp5_;
		_g_free0 (_tmp4_);
		regex = _tmp6_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected 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;
		}
		_tmp8_ = regex;
		_tmp9_ = g_regex_replace_literal (_tmp8_, self, (gssize) -1, 0, replacement, 0, &_inner_error0_);
		_tmp7_ = _tmp9_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_regex_unref0 (regex);
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected 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;
		}
		_tmp10_ = _tmp7_;
		_tmp7_ = NULL;
		result = _tmp10_;
		_g_free0 (_tmp7_);
		_g_regex_unref0 (regex);
		return result;
	}
	goto __finally0;
	__catch0_g_regex_error:
	{
		g_clear_error (&_inner_error0_);
		g_assert_not_reached ();
	}
	__finally0:
	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;
}

static gpointer
_g_date_time_ref0 (gpointer self)
{
	return self ? g_date_time_ref (self) : NULL;
}

ChronoParseResult*
chrono_en_relative_date_format_parser_parse (ChronoENRelativeDateFormatParser* self,
                                             const gchar* text)
{
	GMatchInfo* match = NULL;
	GRegex* _tmp0_;
	GMatchInfo* _tmp1_ = NULL;
	gboolean _tmp2_;
	gchar* modifier = NULL;
	GMatchInfo* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* _tmp7_;
	gchar* _tmp8_;
	gchar* _tmp9_;
	gchar* unit_str = NULL;
	GMatchInfo* _tmp10_;
	gchar* _tmp11_;
	ChronoTimeUnit* time_unit = NULL;
	const gchar* _tmp12_;
	ChronoTimeUnit* _tmp13_;
	ChronoTimeUnit* _tmp14_;
	GDateTime* now = NULL;
	GDateTime* _tmp15_;
	GDateTime* date = NULL;
	const gchar* _tmp16_;
	const gchar* _tmp17_;
	GQuark _tmp19_ = 0U;
	static GQuark _tmp18_label0 = 0;
	static GQuark _tmp18_label1 = 0;
	static GQuark _tmp18_label2 = 0;
	static GQuark _tmp18_label3 = 0;
	static GQuark _tmp18_label4 = 0;
	ChronoParseResult* _result_ = NULL;
	ChronoParseResult* _tmp28_;
	ChronoParseResult* _tmp29_;
	GDateTime* _tmp30_;
	gint start_pos = 0;
	gint end_pos = 0;
	GMatchInfo* _tmp31_;
	gint _tmp32_ = 0;
	gint _tmp33_ = 0;
	ChronoParseResult* _tmp34_;
	ChronoParseResult* _tmp35_;
	ChronoParseResult* _tmp36_;
	GMatchInfo* _tmp37_;
	gchar* _tmp38_;
	gchar* _tmp39_;
	ChronoParseResult* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (text != NULL, NULL);
	_tmp0_ = self->priv->relative_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_ = match;
	_tmp4_ = g_match_info_fetch (_tmp3_, 1);
	_tmp5_ = _tmp4_;
	_tmp6_ = g_utf8_strdown (_tmp5_, (gssize) -1);
	_tmp7_ = _tmp6_;
	_tmp8_ = string_replace (_tmp7_, " ", "");
	_tmp9_ = _tmp8_;
	_g_free0 (_tmp7_);
	_g_free0 (_tmp5_);
	modifier = _tmp9_;
	_tmp10_ = match;
	_tmp11_ = g_match_info_fetch (_tmp10_, 2);
	unit_str = _tmp11_;
	_tmp12_ = unit_str;
	_tmp13_ = chrono_en_constants_get_time_unit (_tmp12_);
	time_unit = _tmp13_;
	_tmp14_ = time_unit;
	if (_tmp14_ == NULL) {
		result = NULL;
		_g_free0 (time_unit);
		_g_free0 (unit_str);
		_g_free0 (modifier);
		_g_match_info_unref0 (match);
		return result;
	}
	_tmp15_ = g_date_time_new_now_local ();
	now = _tmp15_;
	_tmp16_ = modifier;
	_tmp17_ = _tmp16_;
	_tmp19_ = (NULL == _tmp17_) ? 0 : g_quark_from_string (_tmp17_);
	if ((_tmp19_ == ((0 != _tmp18_label0) ? _tmp18_label0 : (_tmp18_label0 = g_quark_from_static_string ("this")))) || (_tmp19_ == ((0 != _tmp18_label1) ? _tmp18_label1 : (_tmp18_label1 = g_quark_from_static_string ("afterthis"))))) {
		switch (0) {
			default:
			{
				GDateTime* _tmp20_;
				GDateTime* _tmp21_;
				_tmp20_ = now;
				_tmp21_ = _g_date_time_ref0 (_tmp20_);
				_g_date_time_unref0 (date);
				date = _tmp21_;
				break;
			}
		}
	} else if ((_tmp19_ == ((0 != _tmp18_label2) ? _tmp18_label2 : (_tmp18_label2 = g_quark_from_static_string ("last")))) || (_tmp19_ == ((0 != _tmp18_label3) ? _tmp18_label3 : (_tmp18_label3 = g_quark_from_static_string ("past"))))) {
		switch (0) {
			default:
			{
				GDateTime* _tmp22_;
				ChronoTimeUnit* _tmp23_;
				GDateTime* _tmp24_;
				_tmp22_ = now;
				_tmp23_ = time_unit;
				_tmp24_ = chrono_en_relative_date_format_parser_add_time_unit (self, _tmp22_, *_tmp23_, -1);
				_g_date_time_unref0 (date);
				date = _tmp24_;
				break;
			}
		}
	} else if (_tmp19_ == ((0 != _tmp18_label4) ? _tmp18_label4 : (_tmp18_label4 = g_quark_from_static_string ("next")))) {
		switch (0) {
			default:
			{
				GDateTime* _tmp25_;
				ChronoTimeUnit* _tmp26_;
				GDateTime* _tmp27_;
				_tmp25_ = now;
				_tmp26_ = time_unit;
				_tmp27_ = chrono_en_relative_date_format_parser_add_time_unit (self, _tmp25_, *_tmp26_, 1);
				_g_date_time_unref0 (date);
				date = _tmp27_;
				break;
			}
		}
	} else {
		switch (0) {
			default:
			{
				result = NULL;
				_g_date_time_unref0 (date);
				_g_date_time_unref0 (now);
				_g_free0 (time_unit);
				_g_free0 (unit_str);
				_g_free0 (modifier);
				_g_match_info_unref0 (match);
				return result;
			}
		}
	}
	_tmp28_ = chrono_parse_result_new ();
	_result_ = _tmp28_;
	_tmp29_ = _result_;
	_tmp30_ = date;
	chrono_parse_result_set_date (_tmp29_, _tmp30_);
	_tmp31_ = match;
	g_match_info_fetch_pos (_tmp31_, 0, &_tmp32_, &_tmp33_);
	start_pos = _tmp32_;
	end_pos = _tmp33_;
	_tmp34_ = _result_;
	chrono_parse_result_set_start_index (_tmp34_, start_pos);
	_tmp35_ = _result_;
	chrono_parse_result_set_end_index (_tmp35_, end_pos);
	_tmp36_ = _result_;
	_tmp37_ = match;
	_tmp38_ = g_match_info_fetch (_tmp37_, 0);
	_tmp39_ = _tmp38_;
	chrono_parse_result_set_matched_text (_tmp36_, _tmp39_);
	_g_free0 (_tmp39_);
	result = _result_;
	_g_date_time_unref0 (date);
	_g_date_time_unref0 (now);
	_g_free0 (time_unit);
	_g_free0 (unit_str);
	_g_free0 (modifier);
	_g_match_info_unref0 (match);
	return result;
}

static GDateTime*
chrono_en_relative_date_format_parser_add_time_unit (ChronoENRelativeDateFormatParser* self,
                                                     GDateTime* date,
                                                     ChronoTimeUnit unit,
                                                     gint amount)
{
	GDateTime* _result_ = NULL;
	GDateTime* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (date != NULL, NULL);
	switch (unit) {
		case CHRONO_TIME_UNIT_SECOND:
		{
			GDateTime* _tmp0_;
			_tmp0_ = g_date_time_add_seconds (date, (gdouble) amount);
			result = _tmp0_;
			_g_date_time_unref0 (_result_);
			return result;
		}
		case CHRONO_TIME_UNIT_MINUTE:
		{
			GDateTime* _tmp1_;
			_tmp1_ = g_date_time_add_minutes (date, amount);
			result = _tmp1_;
			_g_date_time_unref0 (_result_);
			return result;
		}
		case CHRONO_TIME_UNIT_HOUR:
		{
			GDateTime* _tmp2_;
			_tmp2_ = g_date_time_add_hours (date, amount);
			result = _tmp2_;
			_g_date_time_unref0 (_result_);
			return result;
		}
		case CHRONO_TIME_UNIT_DAY:
		{
			GDateTime* _tmp3_;
			GDateTime* _tmp4_;
			GDateTime* _tmp5_;
			GDateTime* _tmp6_;
			GDateTime* _tmp7_;
			_tmp3_ = g_date_time_add_days (date, amount);
			_g_date_time_unref0 (_result_);
			_result_ = _tmp3_;
			_tmp4_ = _result_;
			_tmp5_ = _result_;
			_tmp6_ = _result_;
			_tmp7_ = g_date_time_new_local (g_date_time_get_year (_tmp4_), g_date_time_get_month (_tmp5_), g_date_time_get_day_of_month (_tmp6_), 0, 0, (gdouble) 0);
			result = _tmp7_;
			_g_date_time_unref0 (_result_);
			return result;
		}
		case CHRONO_TIME_UNIT_WEEK:
		{
			GDateTime* _tmp8_;
			GDateTime* _tmp9_;
			GDateTime* _tmp10_;
			GDateTime* _tmp11_;
			GDateTime* _tmp12_;
			_tmp8_ = g_date_time_add_weeks (date, amount);
			_g_date_time_unref0 (_result_);
			_result_ = _tmp8_;
			_tmp9_ = _result_;
			_tmp10_ = _result_;
			_tmp11_ = _result_;
			_tmp12_ = g_date_time_new_local (g_date_time_get_year (_tmp9_), g_date_time_get_month (_tmp10_), g_date_time_get_day_of_month (_tmp11_), 0, 0, (gdouble) 0);
			result = _tmp12_;
			_g_date_time_unref0 (_result_);
			return result;
		}
		case CHRONO_TIME_UNIT_MONTH:
		{
			GDateTime* _tmp13_;
			GDateTime* _tmp14_;
			GDateTime* _tmp15_;
			GDateTime* _tmp16_;
			GDateTime* _tmp17_;
			_tmp13_ = g_date_time_add_months (date, amount);
			_g_date_time_unref0 (_result_);
			_result_ = _tmp13_;
			_tmp14_ = _result_;
			_tmp15_ = _result_;
			_tmp16_ = _result_;
			_tmp17_ = g_date_time_new_local (g_date_time_get_year (_tmp14_), g_date_time_get_month (_tmp15_), g_date_time_get_day_of_month (_tmp16_), 0, 0, (gdouble) 0);
			result = _tmp17_;
			_g_date_time_unref0 (_result_);
			return result;
		}
		case CHRONO_TIME_UNIT_QUARTER:
		{
			GDateTime* _tmp18_;
			GDateTime* _tmp19_;
			GDateTime* _tmp20_;
			GDateTime* _tmp21_;
			GDateTime* _tmp22_;
			_tmp18_ = g_date_time_add_months (date, amount * 3);
			_g_date_time_unref0 (_result_);
			_result_ = _tmp18_;
			_tmp19_ = _result_;
			_tmp20_ = _result_;
			_tmp21_ = _result_;
			_tmp22_ = g_date_time_new_local (g_date_time_get_year (_tmp19_), g_date_time_get_month (_tmp20_), g_date_time_get_day_of_month (_tmp21_), 0, 0, (gdouble) 0);
			result = _tmp22_;
			_g_date_time_unref0 (_result_);
			return result;
		}
		case CHRONO_TIME_UNIT_YEAR:
		{
			GDateTime* _tmp23_;
			GDateTime* _tmp24_;
			GDateTime* _tmp25_;
			GDateTime* _tmp26_;
			GDateTime* _tmp27_;
			_tmp23_ = g_date_time_add_years (date, amount);
			_g_date_time_unref0 (_result_);
			_result_ = _tmp23_;
			_tmp24_ = _result_;
			_tmp25_ = _result_;
			_tmp26_ = _result_;
			_tmp27_ = g_date_time_new_local (g_date_time_get_year (_tmp24_), g_date_time_get_month (_tmp25_), g_date_time_get_day_of_month (_tmp26_), 0, 0, (gdouble) 0);
			result = _tmp27_;
			_g_date_time_unref0 (_result_);
			return result;
		}
		default:
		{
			GDateTime* _tmp28_;
			_tmp28_ = _g_date_time_ref0 (date);
			result = _tmp28_;
			_g_date_time_unref0 (_result_);
			return result;
		}
	}
}

static void
chrono_en_relative_date_format_parser_class_init (ChronoENRelativeDateFormatParserClass * klass,
                                                  gpointer klass_data)
{
	chrono_en_relative_date_format_parser_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ChronoENRelativeDateFormatParser_private_offset);
	G_OBJECT_CLASS (klass)->finalize = chrono_en_relative_date_format_parser_finalize;
}

static void
chrono_en_relative_date_format_parser_instance_init (ChronoENRelativeDateFormatParser * self,
                                                     gpointer klass)
{
	self->priv = chrono_en_relative_date_format_parser_get_instance_private (self);
}

static void
chrono_en_relative_date_format_parser_finalize (GObject * obj)
{
	ChronoENRelativeDateFormatParser * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CHRONO_TYPE_EN_RELATIVE_DATE_FORMAT_PARSER, ChronoENRelativeDateFormatParser);
	_g_regex_unref0 (self->priv->relative_regex);
	G_OBJECT_CLASS (chrono_en_relative_date_format_parser_parent_class)->finalize (obj);
}

/**
     * English relative date format parser
     * 
     * Supports: this week, last month, next year, past 2 days, etc.
     */
 G_GNUC_NO_INLINE static GType
chrono_en_relative_date_format_parser_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ChronoENRelativeDateFormatParserClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) chrono_en_relative_date_format_parser_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ChronoENRelativeDateFormatParser), 0, (GInstanceInitFunc) chrono_en_relative_date_format_parser_instance_init, NULL };
	GType chrono_en_relative_date_format_parser_type_id;
	chrono_en_relative_date_format_parser_type_id = g_type_register_static (G_TYPE_OBJECT, "ChronoENRelativeDateFormatParser", &g_define_type_info, 0);
	ChronoENRelativeDateFormatParser_private_offset = g_type_add_instance_private (chrono_en_relative_date_format_parser_type_id, sizeof (ChronoENRelativeDateFormatParserPrivate));
	return chrono_en_relative_date_format_parser_type_id;
}

GType
chrono_en_relative_date_format_parser_get_type (void)
{
	static gsize chrono_en_relative_date_format_parser_type_id__once = 0;
	if (g_once_init_enter (&chrono_en_relative_date_format_parser_type_id__once)) {
		GType chrono_en_relative_date_format_parser_type_id;
		chrono_en_relative_date_format_parser_type_id = chrono_en_relative_date_format_parser_get_type_once ();
		g_once_init_leave (&chrono_en_relative_date_format_parser_type_id__once, chrono_en_relative_date_format_parser_type_id);
	}
	return chrono_en_relative_date_format_parser_type_id__once;
}

