/* ESTimeExpressionParser.c generated by valac 0.56.19, the Vala compiler
 * generated from ESTimeExpressionParser.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_TIME_EXPRESSION_PARSER_0_PROPERTY,
	CHRONO_ES_TIME_EXPRESSION_PARSER_NUM_PROPERTIES
};
static GParamSpec* chrono_es_time_expression_parser_properties[CHRONO_ES_TIME_EXPRESSION_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 _ChronoESTimeExpressionParserPrivate {
	GRegex* time_expr_regex;
};

static gint ChronoESTimeExpressionParser_private_offset;
static gpointer chrono_es_time_expression_parser_parent_class = NULL;

static GDateTime* chrono_es_time_expression_parser_add_time_unit (ChronoESTimeExpressionParser* self,
                                                           GDateTime* date,
                                                           ChronoTimeUnit unit,
                                                           gint amount);
static void chrono_es_time_expression_parser_finalize (GObject * obj);
static GType chrono_es_time_expression_parser_get_type_once (void);

static inline gpointer
chrono_es_time_expression_parser_get_instance_private (ChronoESTimeExpressionParser* self)
{
	return G_STRUCT_MEMBER_P (self, ChronoESTimeExpressionParser_private_offset);
}

ChronoESTimeExpressionParser*
chrono_es_time_expression_parser_construct (GType object_type)
{
	ChronoESTimeExpressionParser * self = NULL;
	GError* _inner_error0_ = NULL;
	self = (ChronoESTimeExpressionParser*) g_object_new (object_type, NULL);
	{
		GRegex* _tmp0_ = NULL;
		GRegex* _tmp1_;
		GRegex* _tmp2_;
		_tmp1_ = g_regex_new ("(?:en|dentro\\s+de)\\s+(\\d+)\\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|seg|min|h|d|sem|trim)", 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->time_expr_regex);
		self->priv->time_expr_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 ("ESTimeExpressionParser.vala:38: Error creating time expression 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;
}

ChronoESTimeExpressionParser*
chrono_es_time_expression_parser_new (void)
{
	return chrono_es_time_expression_parser_construct (CHRONO_TYPE_ES_TIME_EXPRESSION_PARSER);
}

ChronoParseResult*
chrono_es_time_expression_parser_parse (ChronoESTimeExpressionParser* self,
                                        const gchar* text)
{
	GMatchInfo* match = NULL;
	GRegex* _tmp0_;
	GMatchInfo* _tmp1_ = NULL;
	gboolean _tmp2_;
	gint amount = 0;
	GMatchInfo* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gint _tmp6_;
	gchar* unit_str = NULL;
	GMatchInfo* _tmp7_;
	gchar* _tmp8_;
	ChronoTimeUnit* time_unit = NULL;
	const gchar* _tmp9_;
	ChronoTimeUnit* _tmp10_;
	ChronoTimeUnit* _tmp11_;
	GDateTime* now = NULL;
	GDateTime* _tmp12_;
	GDateTime* date = NULL;
	GDateTime* _tmp13_;
	ChronoTimeUnit* _tmp14_;
	GDateTime* _tmp15_;
	ChronoParseResult* _result_ = NULL;
	ChronoParseResult* _tmp16_;
	ChronoParseResult* _tmp17_;
	GDateTime* _tmp18_;
	gint start_pos = 0;
	gint end_pos = 0;
	GMatchInfo* _tmp19_;
	gint _tmp20_ = 0;
	gint _tmp21_ = 0;
	ChronoParseResult* _tmp22_;
	ChronoParseResult* _tmp23_;
	ChronoParseResult* _tmp24_;
	GMatchInfo* _tmp25_;
	gchar* _tmp26_;
	gchar* _tmp27_;
	ChronoParseResult* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (text != NULL, NULL);
	_tmp0_ = self->priv->time_expr_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_ = atoi (_tmp5_);
	_g_free0 (_tmp5_);
	amount = _tmp6_;
	_tmp7_ = match;
	_tmp8_ = g_match_info_fetch (_tmp7_, 2);
	unit_str = _tmp8_;
	_tmp9_ = unit_str;
	_tmp10_ = chrono_es_constants_get_time_unit (_tmp9_);
	time_unit = _tmp10_;
	_tmp11_ = time_unit;
	if (_tmp11_ == NULL) {
		result = NULL;
		_g_free0 (time_unit);
		_g_free0 (unit_str);
		_g_match_info_unref0 (match);
		return result;
	}
	_tmp12_ = g_date_time_new_now_local ();
	now = _tmp12_;
	_tmp13_ = now;
	_tmp14_ = time_unit;
	_tmp15_ = chrono_es_time_expression_parser_add_time_unit (self, _tmp13_, *_tmp14_, amount);
	date = _tmp15_;
	_tmp16_ = chrono_parse_result_new ();
	_result_ = _tmp16_;
	_tmp17_ = _result_;
	_tmp18_ = date;
	chrono_parse_result_set_date (_tmp17_, _tmp18_);
	_tmp19_ = match;
	g_match_info_fetch_pos (_tmp19_, 0, &_tmp20_, &_tmp21_);
	start_pos = _tmp20_;
	end_pos = _tmp21_;
	_tmp22_ = _result_;
	chrono_parse_result_set_start_index (_tmp22_, start_pos);
	_tmp23_ = _result_;
	chrono_parse_result_set_end_index (_tmp23_, end_pos);
	_tmp24_ = _result_;
	_tmp25_ = match;
	_tmp26_ = g_match_info_fetch (_tmp25_, 0);
	_tmp27_ = _tmp26_;
	chrono_parse_result_set_matched_text (_tmp24_, _tmp27_);
	_g_free0 (_tmp27_);
	result = _result_;
	_g_date_time_unref0 (date);
	_g_date_time_unref0 (now);
	_g_free0 (time_unit);
	_g_free0 (unit_str);
	_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_time_expression_parser_add_time_unit (ChronoESTimeExpressionParser* self,
                                                GDateTime* date,
                                                ChronoTimeUnit unit,
                                                gint amount)
{
	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_;
			return result;
		}
		case CHRONO_TIME_UNIT_MINUTE:
		{
			GDateTime* _tmp1_;
			_tmp1_ = g_date_time_add_minutes (date, amount);
			result = _tmp1_;
			return result;
		}
		case CHRONO_TIME_UNIT_HOUR:
		{
			GDateTime* _tmp2_;
			_tmp2_ = g_date_time_add_hours (date, amount);
			result = _tmp2_;
			return result;
		}
		case CHRONO_TIME_UNIT_DAY:
		{
			GDateTime* _tmp3_;
			_tmp3_ = g_date_time_add_days (date, amount);
			result = _tmp3_;
			return result;
		}
		case CHRONO_TIME_UNIT_WEEK:
		{
			GDateTime* _tmp4_;
			_tmp4_ = g_date_time_add_weeks (date, amount);
			result = _tmp4_;
			return result;
		}
		case CHRONO_TIME_UNIT_MONTH:
		{
			GDateTime* _tmp5_;
			_tmp5_ = g_date_time_add_months (date, amount);
			result = _tmp5_;
			return result;
		}
		case CHRONO_TIME_UNIT_QUARTER:
		{
			GDateTime* _tmp6_;
			_tmp6_ = g_date_time_add_months (date, amount * 3);
			result = _tmp6_;
			return result;
		}
		case CHRONO_TIME_UNIT_YEAR:
		{
			GDateTime* _tmp7_;
			_tmp7_ = g_date_time_add_years (date, amount);
			result = _tmp7_;
			return result;
		}
		default:
		{
			GDateTime* _tmp8_;
			_tmp8_ = _g_date_time_ref0 (date);
			result = _tmp8_;
			return result;
		}
	}
}

static void
chrono_es_time_expression_parser_class_init (ChronoESTimeExpressionParserClass * klass,
                                             gpointer klass_data)
{
	chrono_es_time_expression_parser_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ChronoESTimeExpressionParser_private_offset);
	G_OBJECT_CLASS (klass)->finalize = chrono_es_time_expression_parser_finalize;
}

static void
chrono_es_time_expression_parser_instance_init (ChronoESTimeExpressionParser * self,
                                                gpointer klass)
{
	self->priv = chrono_es_time_expression_parser_get_instance_private (self);
}

static void
chrono_es_time_expression_parser_finalize (GObject * obj)
{
	ChronoESTimeExpressionParser * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CHRONO_TYPE_ES_TIME_EXPRESSION_PARSER, ChronoESTimeExpressionParser);
	_g_regex_unref0 (self->priv->time_expr_regex);
	G_OBJECT_CLASS (chrono_es_time_expression_parser_parent_class)->finalize (obj);
}

/**
     * Spanish time expression parser
     * 
     * Supports: en 5 minutos, en 2 horas, en 3 días, dentro de 5 minutos
     */
 G_GNUC_NO_INLINE static GType
chrono_es_time_expression_parser_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ChronoESTimeExpressionParserClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) chrono_es_time_expression_parser_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ChronoESTimeExpressionParser), 0, (GInstanceInitFunc) chrono_es_time_expression_parser_instance_init, NULL };
	GType chrono_es_time_expression_parser_type_id;
	chrono_es_time_expression_parser_type_id = g_type_register_static (G_TYPE_OBJECT, "ChronoESTimeExpressionParser", &g_define_type_info, 0);
	ChronoESTimeExpressionParser_private_offset = g_type_add_instance_private (chrono_es_time_expression_parser_type_id, sizeof (ChronoESTimeExpressionParserPrivate));
	return chrono_es_time_expression_parser_type_id;
}

GType
chrono_es_time_expression_parser_get_type (void)
{
	static gsize chrono_es_time_expression_parser_type_id__once = 0;
	if (g_once_init_enter (&chrono_es_time_expression_parser_type_id__once)) {
		GType chrono_es_time_expression_parser_type_id;
		chrono_es_time_expression_parser_type_id = chrono_es_time_expression_parser_get_type_once ();
		g_once_init_leave (&chrono_es_time_expression_parser_type_id__once, chrono_es_time_expression_parser_type_id);
	}
	return chrono_es_time_expression_parser_type_id__once;
}

