/* ENTimeExpressionParser.c generated by valac 0.56.19, the Vala compiler
 * generated from ENTimeExpressionParser.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_TIME_EXPRESSION_PARSER_0_PROPERTY,
	CHRONO_EN_TIME_EXPRESSION_PARSER_NUM_PROPERTIES
};
static GParamSpec* chrono_en_time_expression_parser_properties[CHRONO_EN_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 _ChronoENTimeExpressionParserPrivate {
	GRegex* time_expr_regex;
};

static gint ChronoENTimeExpressionParser_private_offset;
static gpointer chrono_en_time_expression_parser_parent_class = NULL;

static GDateTime* chrono_en_time_expression_parser_add_time_unit (ChronoENTimeExpressionParser* self,
                                                           GDateTime* date,
                                                           ChronoTimeUnit unit,
                                                           gint amount);
static void chrono_en_time_expression_parser_finalize (GObject * obj);
static GType chrono_en_time_expression_parser_get_type_once (void);

static inline gpointer
chrono_en_time_expression_parser_get_instance_private (ChronoENTimeExpressionParser* self)
{
	return G_STRUCT_MEMBER_P (self, ChronoENTimeExpressionParser_private_offset);
}

ChronoENTimeExpressionParser*
chrono_en_time_expression_parser_construct (GType object_type)
{
	ChronoENTimeExpressionParser * self = NULL;
	GError* _inner_error0_ = NULL;
	self = (ChronoENTimeExpressionParser*) g_object_new (object_type, NULL);
	{
		GRegex* _tmp0_ = NULL;
		GRegex* _tmp1_;
		GRegex* _tmp2_;
		_tmp1_ = g_regex_new ("(?:in|within)\\s+(\\d+)\\s+(second|seconds|minute|minutes|hour|hours|d" \
"ay|days|week|weeks|month|months|year|years|s|sec|m|min|mins|h|hr|hrs|d" \
"|w|mo|mon|mos|y|yr)|" "(\\d+)\\s+(second|seconds|minute|minutes|hour|hours|day|days|week|week" \
"s|month|months|year|years|s|sec|m|min|mins|h|hr|hrs|d|w|mo|mon|mos|y|y" \
"r)\\s+(?:from\\s+now|later)", 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 ("ENTimeExpressionParser.vala:39: 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;
}

ChronoENTimeExpressionParser*
chrono_en_time_expression_parser_new (void)
{
	return chrono_en_time_expression_parser_construct (CHRONO_TYPE_EN_TIME_EXPRESSION_PARSER);
}

ChronoParseResult*
chrono_en_time_expression_parser_parse (ChronoENTimeExpressionParser* self,
                                        const gchar* text)
{
	GMatchInfo* match = NULL;
	GRegex* _tmp0_;
	GMatchInfo* _tmp1_ = NULL;
	gboolean _tmp2_;
	gint amount = 0;
	gchar* unit_str = NULL;
	gchar* amount_str = NULL;
	GMatchInfo* _tmp3_;
	gchar* _tmp4_;
	gboolean _tmp5_ = FALSE;
	const gchar* _tmp6_;
	ChronoTimeUnit* time_unit = NULL;
	const gchar* _tmp18_;
	ChronoTimeUnit* _tmp19_;
	ChronoTimeUnit* _tmp20_;
	GDateTime* now = NULL;
	GDateTime* _tmp21_;
	GDateTime* date = NULL;
	GDateTime* _tmp22_;
	ChronoTimeUnit* _tmp23_;
	GDateTime* _tmp24_;
	ChronoParseResult* _result_ = NULL;
	ChronoParseResult* _tmp25_;
	ChronoParseResult* _tmp26_;
	GDateTime* _tmp27_;
	gint start_pos = 0;
	gint end_pos = 0;
	GMatchInfo* _tmp28_;
	gint _tmp29_ = 0;
	gint _tmp30_ = 0;
	ChronoParseResult* _tmp31_;
	ChronoParseResult* _tmp32_;
	ChronoParseResult* _tmp33_;
	GMatchInfo* _tmp34_;
	gchar* _tmp35_;
	gchar* _tmp36_;
	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);
	amount_str = _tmp4_;
	_tmp6_ = amount_str;
	if (_tmp6_ != NULL) {
		const gchar* _tmp7_;
		gint _tmp8_;
		gint _tmp9_;
		_tmp7_ = amount_str;
		_tmp8_ = strlen (_tmp7_);
		_tmp9_ = _tmp8_;
		_tmp5_ = _tmp9_ > 0;
	} else {
		_tmp5_ = FALSE;
	}
	if (_tmp5_) {
		const gchar* _tmp10_;
		GMatchInfo* _tmp11_;
		gchar* _tmp12_;
		_tmp10_ = amount_str;
		amount = atoi (_tmp10_);
		_tmp11_ = match;
		_tmp12_ = g_match_info_fetch (_tmp11_, 2);
		_g_free0 (unit_str);
		unit_str = _tmp12_;
	} else {
		GMatchInfo* _tmp13_;
		gchar* _tmp14_;
		gchar* _tmp15_;
		GMatchInfo* _tmp16_;
		gchar* _tmp17_;
		_tmp13_ = match;
		_tmp14_ = g_match_info_fetch (_tmp13_, 3);
		_tmp15_ = _tmp14_;
		amount = atoi (_tmp15_);
		_g_free0 (_tmp15_);
		_tmp16_ = match;
		_tmp17_ = g_match_info_fetch (_tmp16_, 4);
		_g_free0 (unit_str);
		unit_str = _tmp17_;
	}
	_tmp18_ = unit_str;
	_tmp19_ = chrono_en_constants_get_time_unit (_tmp18_);
	time_unit = _tmp19_;
	_tmp20_ = time_unit;
	if (_tmp20_ == NULL) {
		result = NULL;
		_g_free0 (time_unit);
		_g_free0 (amount_str);
		_g_free0 (unit_str);
		_g_match_info_unref0 (match);
		return result;
	}
	_tmp21_ = g_date_time_new_now_local ();
	now = _tmp21_;
	_tmp22_ = now;
	_tmp23_ = time_unit;
	_tmp24_ = chrono_en_time_expression_parser_add_time_unit (self, _tmp22_, *_tmp23_, amount);
	date = _tmp24_;
	_tmp25_ = chrono_parse_result_new ();
	_result_ = _tmp25_;
	_tmp26_ = _result_;
	_tmp27_ = date;
	chrono_parse_result_set_date (_tmp26_, _tmp27_);
	_tmp28_ = match;
	g_match_info_fetch_pos (_tmp28_, 0, &_tmp29_, &_tmp30_);
	start_pos = _tmp29_;
	end_pos = _tmp30_;
	_tmp31_ = _result_;
	chrono_parse_result_set_start_index (_tmp31_, start_pos);
	_tmp32_ = _result_;
	chrono_parse_result_set_end_index (_tmp32_, end_pos);
	_tmp33_ = _result_;
	_tmp34_ = match;
	_tmp35_ = g_match_info_fetch (_tmp34_, 0);
	_tmp36_ = _tmp35_;
	chrono_parse_result_set_matched_text (_tmp33_, _tmp36_);
	_g_free0 (_tmp36_);
	result = _result_;
	_g_date_time_unref0 (date);
	_g_date_time_unref0 (now);
	_g_free0 (time_unit);
	_g_free0 (amount_str);
	_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_en_time_expression_parser_add_time_unit (ChronoENTimeExpressionParser* 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_en_time_expression_parser_class_init (ChronoENTimeExpressionParserClass * klass,
                                             gpointer klass_data)
{
	chrono_en_time_expression_parser_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ChronoENTimeExpressionParser_private_offset);
	G_OBJECT_CLASS (klass)->finalize = chrono_en_time_expression_parser_finalize;
}

static void
chrono_en_time_expression_parser_instance_init (ChronoENTimeExpressionParser * self,
                                                gpointer klass)
{
	self->priv = chrono_en_time_expression_parser_get_instance_private (self);
}

static void
chrono_en_time_expression_parser_finalize (GObject * obj)
{
	ChronoENTimeExpressionParser * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CHRONO_TYPE_EN_TIME_EXPRESSION_PARSER, ChronoENTimeExpressionParser);
	_g_regex_unref0 (self->priv->time_expr_regex);
	G_OBJECT_CLASS (chrono_en_time_expression_parser_parent_class)->finalize (obj);
}

/**
     * English time expression parser
     * 
     * Supports: in 5 minutes, in 2 hours, in 3 days, 5 minutes from now
     */
 G_GNUC_NO_INLINE static GType
chrono_en_time_expression_parser_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ChronoENTimeExpressionParserClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) chrono_en_time_expression_parser_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ChronoENTimeExpressionParser), 0, (GInstanceInitFunc) chrono_en_time_expression_parser_instance_init, NULL };
	GType chrono_en_time_expression_parser_type_id;
	chrono_en_time_expression_parser_type_id = g_type_register_static (G_TYPE_OBJECT, "ChronoENTimeExpressionParser", &g_define_type_info, 0);
	ChronoENTimeExpressionParser_private_offset = g_type_add_instance_private (chrono_en_time_expression_parser_type_id, sizeof (ChronoENTimeExpressionParserPrivate));
	return chrono_en_time_expression_parser_type_id;
}

GType
chrono_en_time_expression_parser_get_type (void)
{
	static gsize chrono_en_time_expression_parser_type_id__once = 0;
	if (g_once_init_enter (&chrono_en_time_expression_parser_type_id__once)) {
		GType chrono_en_time_expression_parser_type_id;
		chrono_en_time_expression_parser_type_id = chrono_en_time_expression_parser_get_type_once ();
		g_once_init_leave (&chrono_en_time_expression_parser_type_id__once, chrono_en_time_expression_parser_type_id);
	}
	return chrono_en_time_expression_parser_type_id__once;
}

