/* ESRelativeDateFormatParser.c generated by valac 0.56.19, the Vala compiler
 * generated from ESRelativeDateFormatParser.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_ES_RELATIVE_DATE_FORMAT_PARSER_0_PROPERTY,
	CHRONO_ES_RELATIVE_DATE_FORMAT_PARSER_NUM_PROPERTIES
};
static GParamSpec* chrono_es_relative_date_format_parser_properties[CHRONO_ES_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 _ChronoESRelativeDateFormatParserPrivate {
	GRegex* relative_regex;
};

static gint ChronoESRelativeDateFormatParser_private_offset;
static gpointer chrono_es_relative_date_format_parser_parent_class = NULL;

static GDateTime* chrono_es_relative_date_format_parser_add_time_unit (ChronoESRelativeDateFormatParser* self,
                                                                GDateTime* date,
                                                                ChronoTimeUnit unit,
                                                                gint amount);
static void chrono_es_relative_date_format_parser_finalize (GObject * obj);
static GType chrono_es_relative_date_format_parser_get_type_once (void);

static inline gpointer
chrono_es_relative_date_format_parser_get_instance_private (ChronoESRelativeDateFormatParser* self)
{
	return G_STRUCT_MEMBER_P (self, ChronoESRelativeDateFormatParser_private_offset);
}

ChronoESRelativeDateFormatParser*
chrono_es_relative_date_format_parser_construct (GType object_type)
{
	ChronoESRelativeDateFormatParser * self = NULL;
	GError* _inner_error0_ = NULL;
	self = (ChronoESRelativeDateFormatParser*) g_object_new (object_type, NULL);
	{
		GRegex* _tmp0_ = NULL;
		GRegex* _tmp1_;
		GRegex* _tmp2_;
		_tmp1_ = g_regex_new ("(?:el|la)?\\s*(?:próximo|proximo|próxima|proxima|siguiente|pasado|pasa" \
"da|que\\s+viene)\\s+(segundo|segundos|minuto|minutos|hora|horas|día|di" \
"a|días|dias|semana|semanas|mes|meses|trimestre|trimestres|año|ano|años" \
"|anos)|" "(?:el|la)?\\s*(segundo|segundos|minuto|minutos|hora|horas|día|dia|días" \
"|dias|semana|semanas|mes|meses|trimestre|trimestres|año|ano|años|anos)" \
"\\s+(?:próximo|proximo|próxima|proxima|siguiente|pasado|pasada|que\\s+" \
"viene)|" "(?:este|esta)\\s+(segundo|segundos|minuto|minutos|hora|horas|día|dia|d" \
"ías|dias|semana|semanas|mes|meses|trimestre|trimestres|año|ano|años|an" \
"os)", 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 ("ESRelativeDateFormatParser.vala:40: 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;
}

ChronoESRelativeDateFormatParser*
chrono_es_relative_date_format_parser_new (void)
{
	return chrono_es_relative_date_format_parser_construct (CHRONO_TYPE_ES_RELATIVE_DATE_FORMAT_PARSER);
}

static gboolean
string_contains (const gchar* self,
                 const gchar* needle)
{
	gchar* _tmp0_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (needle != NULL, FALSE);
	_tmp0_ = strstr ((gchar*) self, (gchar*) needle);
	result = _tmp0_ != NULL;
	return result;
}

ChronoParseResult*
chrono_es_relative_date_format_parser_parse (ChronoESRelativeDateFormatParser* self,
                                             const gchar* text)
{
	GMatchInfo* match = NULL;
	GRegex* _tmp0_;
	GMatchInfo* _tmp1_ = NULL;
	gboolean _tmp2_;
	gchar* full_match = NULL;
	GMatchInfo* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* _tmp7_;
	gchar* unit_str = NULL;
	gint amount = 0;
	const gchar* _tmp19_;
	ChronoTimeUnit* time_unit = NULL;
	const gchar* _tmp20_;
	ChronoTimeUnit* _tmp21_;
	ChronoTimeUnit* _tmp22_;
	gboolean _tmp23_ = FALSE;
	gboolean _tmp24_ = FALSE;
	gboolean _tmp25_ = FALSE;
	gboolean _tmp26_ = FALSE;
	gboolean _tmp27_ = FALSE;
	const gchar* _tmp28_;
	GDateTime* now = NULL;
	GDateTime* _tmp40_;
	GDateTime* date = NULL;
	GDateTime* _tmp41_;
	ChronoTimeUnit* _tmp42_;
	GDateTime* _tmp43_;
	ChronoParseResult* _result_ = NULL;
	ChronoParseResult* _tmp44_;
	ChronoParseResult* _tmp45_;
	GDateTime* _tmp46_;
	gint start_pos = 0;
	gint end_pos = 0;
	GMatchInfo* _tmp47_;
	gint _tmp48_ = 0;
	gint _tmp49_ = 0;
	ChronoParseResult* _tmp50_;
	ChronoParseResult* _tmp51_;
	ChronoParseResult* _tmp52_;
	GMatchInfo* _tmp53_;
	gchar* _tmp54_;
	gchar* _tmp55_;
	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_, 0);
	_tmp5_ = _tmp4_;
	_tmp6_ = g_utf8_strdown (_tmp5_, (gssize) -1);
	_tmp7_ = _tmp6_;
	_g_free0 (_tmp5_);
	full_match = _tmp7_;
	unit_str = NULL;
	amount = 0;
	{
		gint i = 0;
		i = 1;
		{
			gboolean _tmp8_ = FALSE;
			_tmp8_ = TRUE;
			while (TRUE) {
				gchar* temp = NULL;
				GMatchInfo* _tmp10_;
				gchar* _tmp11_;
				gboolean _tmp12_ = FALSE;
				const gchar* _tmp13_;
				if (!_tmp8_) {
					gint _tmp9_;
					_tmp9_ = i;
					i = _tmp9_ + 1;
				}
				_tmp8_ = FALSE;
				if (!(i <= 3)) {
					break;
				}
				_tmp10_ = match;
				_tmp11_ = g_match_info_fetch (_tmp10_, i);
				temp = _tmp11_;
				_tmp13_ = temp;
				if (_tmp13_ != NULL) {
					const gchar* _tmp14_;
					gint _tmp15_;
					gint _tmp16_;
					_tmp14_ = temp;
					_tmp15_ = strlen (_tmp14_);
					_tmp16_ = _tmp15_;
					_tmp12_ = _tmp16_ > 0;
				} else {
					_tmp12_ = FALSE;
				}
				if (_tmp12_) {
					const gchar* _tmp17_;
					gchar* _tmp18_;
					_tmp17_ = temp;
					_tmp18_ = g_strdup (_tmp17_);
					_g_free0 (unit_str);
					unit_str = _tmp18_;
					_g_free0 (temp);
					break;
				}
				_g_free0 (temp);
			}
		}
	}
	_tmp19_ = unit_str;
	if (_tmp19_ == NULL) {
		result = NULL;
		_g_free0 (unit_str);
		_g_free0 (full_match);
		_g_match_info_unref0 (match);
		return result;
	}
	_tmp20_ = unit_str;
	_tmp21_ = chrono_es_constants_get_time_unit (_tmp20_);
	time_unit = _tmp21_;
	_tmp22_ = time_unit;
	if (_tmp22_ == NULL) {
		result = NULL;
		_g_free0 (time_unit);
		_g_free0 (unit_str);
		_g_free0 (full_match);
		_g_match_info_unref0 (match);
		return result;
	}
	_tmp28_ = full_match;
	if (string_contains (_tmp28_, "próximo")) {
		_tmp27_ = TRUE;
	} else {
		const gchar* _tmp29_;
		_tmp29_ = full_match;
		_tmp27_ = string_contains (_tmp29_, "proximo");
	}
	if (_tmp27_) {
		_tmp26_ = TRUE;
	} else {
		const gchar* _tmp30_;
		_tmp30_ = full_match;
		_tmp26_ = string_contains (_tmp30_, "próxima");
	}
	if (_tmp26_) {
		_tmp25_ = TRUE;
	} else {
		const gchar* _tmp31_;
		_tmp31_ = full_match;
		_tmp25_ = string_contains (_tmp31_, "proxima");
	}
	if (_tmp25_) {
		_tmp24_ = TRUE;
	} else {
		const gchar* _tmp32_;
		_tmp32_ = full_match;
		_tmp24_ = string_contains (_tmp32_, "siguiente");
	}
	if (_tmp24_) {
		_tmp23_ = TRUE;
	} else {
		const gchar* _tmp33_;
		_tmp33_ = full_match;
		_tmp23_ = string_contains (_tmp33_, "que viene");
	}
	if (_tmp23_) {
		amount = 1;
	} else {
		gboolean _tmp34_ = FALSE;
		const gchar* _tmp35_;
		_tmp35_ = full_match;
		if (string_contains (_tmp35_, "pasado")) {
			_tmp34_ = TRUE;
		} else {
			const gchar* _tmp36_;
			_tmp36_ = full_match;
			_tmp34_ = string_contains (_tmp36_, "pasada");
		}
		if (_tmp34_) {
			amount = -1;
		} else {
			gboolean _tmp37_ = FALSE;
			const gchar* _tmp38_;
			_tmp38_ = full_match;
			if (string_contains (_tmp38_, "este")) {
				_tmp37_ = TRUE;
			} else {
				const gchar* _tmp39_;
				_tmp39_ = full_match;
				_tmp37_ = string_contains (_tmp39_, "esta");
			}
			if (_tmp37_) {
				amount = 0;
			}
		}
	}
	_tmp40_ = g_date_time_new_now_local ();
	now = _tmp40_;
	_tmp41_ = now;
	_tmp42_ = time_unit;
	_tmp43_ = chrono_es_relative_date_format_parser_add_time_unit (self, _tmp41_, *_tmp42_, amount);
	date = _tmp43_;
	_tmp44_ = chrono_parse_result_new ();
	_result_ = _tmp44_;
	_tmp45_ = _result_;
	_tmp46_ = date;
	chrono_parse_result_set_date (_tmp45_, _tmp46_);
	_tmp47_ = match;
	g_match_info_fetch_pos (_tmp47_, 0, &_tmp48_, &_tmp49_);
	start_pos = _tmp48_;
	end_pos = _tmp49_;
	_tmp50_ = _result_;
	chrono_parse_result_set_start_index (_tmp50_, start_pos);
	_tmp51_ = _result_;
	chrono_parse_result_set_end_index (_tmp51_, end_pos);
	_tmp52_ = _result_;
	_tmp53_ = match;
	_tmp54_ = g_match_info_fetch (_tmp53_, 0);
	_tmp55_ = _tmp54_;
	chrono_parse_result_set_matched_text (_tmp52_, _tmp55_);
	_g_free0 (_tmp55_);
	result = _result_;
	_g_date_time_unref0 (date);
	_g_date_time_unref0 (now);
	_g_free0 (time_unit);
	_g_free0 (unit_str);
	_g_free0 (full_match);
	_g_match_info_unref0 (match);
	return result;
}

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

static GDateTime*
chrono_es_relative_date_format_parser_add_time_unit (ChronoESRelativeDateFormatParser* 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_es_relative_date_format_parser_class_init (ChronoESRelativeDateFormatParserClass * klass,
                                                  gpointer klass_data)
{
	chrono_es_relative_date_format_parser_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ChronoESRelativeDateFormatParser_private_offset);
	G_OBJECT_CLASS (klass)->finalize = chrono_es_relative_date_format_parser_finalize;
}

static void
chrono_es_relative_date_format_parser_instance_init (ChronoESRelativeDateFormatParser * self,
                                                     gpointer klass)
{
	self->priv = chrono_es_relative_date_format_parser_get_instance_private (self);
}

static void
chrono_es_relative_date_format_parser_finalize (GObject * obj)
{
	ChronoESRelativeDateFormatParser * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CHRONO_TYPE_ES_RELATIVE_DATE_FORMAT_PARSER, ChronoESRelativeDateFormatParser);
	_g_regex_unref0 (self->priv->relative_regex);
	G_OBJECT_CLASS (chrono_es_relative_date_format_parser_parent_class)->finalize (obj);
}

/**
     * Spanish relative date format parser
     * 
     * Supports: esta semana, el mes pasado, el próximo año, la semana que viene
     */
 G_GNUC_NO_INLINE static GType
chrono_es_relative_date_format_parser_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ChronoESRelativeDateFormatParserClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) chrono_es_relative_date_format_parser_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ChronoESRelativeDateFormatParser), 0, (GInstanceInitFunc) chrono_es_relative_date_format_parser_instance_init, NULL };
	GType chrono_es_relative_date_format_parser_type_id;
	chrono_es_relative_date_format_parser_type_id = g_type_register_static (G_TYPE_OBJECT, "ChronoESRelativeDateFormatParser", &g_define_type_info, 0);
	ChronoESRelativeDateFormatParser_private_offset = g_type_add_instance_private (chrono_es_relative_date_format_parser_type_id, sizeof (ChronoESRelativeDateFormatParserPrivate));
	return chrono_es_relative_date_format_parser_type_id;
}

GType
chrono_es_relative_date_format_parser_get_type (void)
{
	static gsize chrono_es_relative_date_format_parser_type_id__once = 0;
	if (g_once_init_enter (&chrono_es_relative_date_format_parser_type_id__once)) {
		GType chrono_es_relative_date_format_parser_type_id;
		chrono_es_relative_date_format_parser_type_id = chrono_es_relative_date_format_parser_get_type_once ();
		g_once_init_leave (&chrono_es_relative_date_format_parser_type_id__once, chrono_es_relative_date_format_parser_type_id);
	}
	return chrono_es_relative_date_format_parser_type_id__once;
}

