/* ESDateTimeComboParser.c generated by valac 0.56.19, the Vala compiler
 * generated from ESDateTimeComboParser.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_DATE_TIME_COMBO_PARSER_0_PROPERTY,
	CHRONO_ES_DATE_TIME_COMBO_PARSER_NUM_PROPERTIES
};
static GParamSpec* chrono_es_date_time_combo_parser_properties[CHRONO_ES_DATE_TIME_COMBO_PARSER_NUM_PROPERTIES];
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_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 _ChronoESDateTimeComboParserPrivate {
	GRegex* combo_regex;
	ChronoESCasualDateParser* casual_parser;
	ChronoTimeParser* time_parser;
};

static gint ChronoESDateTimeComboParser_private_offset;
static gpointer chrono_es_date_time_combo_parser_parent_class = NULL;

static void chrono_es_date_time_combo_parser_finalize (GObject * obj);
static GType chrono_es_date_time_combo_parser_get_type_once (void);

static inline gpointer
chrono_es_date_time_combo_parser_get_instance_private (ChronoESDateTimeComboParser* self)
{
	return G_STRUCT_MEMBER_P (self, ChronoESDateTimeComboParser_private_offset);
}

ChronoESDateTimeComboParser*
chrono_es_date_time_combo_parser_construct (GType object_type)
{
	ChronoESDateTimeComboParser * self = NULL;
	ChronoESCasualDateParser* _tmp0_;
	ChronoTimeParser* _tmp1_;
	GError* _inner_error0_ = NULL;
	self = (ChronoESDateTimeComboParser*) g_object_new (object_type, NULL);
	_tmp0_ = chrono_es_casual_date_parser_new ();
	_g_object_unref0 (self->priv->casual_parser);
	self->priv->casual_parser = _tmp0_;
	_tmp1_ = chrono_time_parser_new ();
	_g_object_unref0 (self->priv->time_parser);
	self->priv->time_parser = _tmp1_;
	{
		GRegex* _tmp2_ = NULL;
		GRegex* _tmp3_;
		GRegex* _tmp4_;
		_tmp3_ = g_regex_new ("(.+?)\\s+a\\s+las\\s+(.+)", G_REGEX_CASELESS, 0, &_inner_error0_);
		_tmp2_ = _tmp3_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp4_ = _tmp2_;
		_tmp2_ = NULL;
		_g_regex_unref0 (self->priv->combo_regex);
		self->priv->combo_regex = _tmp4_;
		_g_regex_unref0 (_tmp2_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp5_;
		const gchar* _tmp6_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp5_ = e;
		_tmp6_ = _tmp5_->message;
		g_warning ("ESDateTimeComboParser.vala:44: Error creating combo regex: %s", _tmp6_);
		_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;
}

ChronoESDateTimeComboParser*
chrono_es_date_time_combo_parser_new (void)
{
	return chrono_es_date_time_combo_parser_construct (CHRONO_TYPE_ES_DATE_TIME_COMBO_PARSER);
}

static gchar*
string_strip (const gchar* self)
{
	gchar* _result_ = NULL;
	gchar* _tmp0_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_strdup (self);
	_result_ = _tmp0_;
	g_strstrip (_result_);
	result = _result_;
	return result;
}

ChronoParseResult*
chrono_es_date_time_combo_parser_parse (ChronoESDateTimeComboParser* self,
                                        const gchar* text)
{
	GMatchInfo* match = NULL;
	GRegex* _tmp0_;
	GMatchInfo* _tmp1_ = NULL;
	gboolean _tmp2_;
	gchar* date_part = NULL;
	GMatchInfo* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* _tmp7_;
	gchar* time_part = NULL;
	GMatchInfo* _tmp8_;
	gchar* _tmp9_;
	gchar* _tmp10_;
	gchar* _tmp11_;
	gchar* _tmp12_;
	ChronoParseResult* date_result = NULL;
	ChronoESCasualDateParser* _tmp13_;
	const gchar* _tmp14_;
	ChronoParseResult* _tmp15_;
	gboolean _tmp16_ = FALSE;
	ChronoParseResult* _tmp17_;
	ChronoParseResult* time_result = NULL;
	ChronoTimeParser* _tmp21_;
	const gchar* _tmp22_;
	ChronoParseResult* _tmp23_;
	gboolean _tmp24_ = FALSE;
	ChronoParseResult* _tmp25_;
	GDateTime* combined_date = NULL;
	ChronoParseResult* _tmp29_;
	GDateTime* _tmp30_;
	GDateTime* _tmp31_;
	ChronoParseResult* _tmp32_;
	GDateTime* _tmp33_;
	GDateTime* _tmp34_;
	ChronoParseResult* _tmp35_;
	GDateTime* _tmp36_;
	GDateTime* _tmp37_;
	ChronoParseResult* _tmp38_;
	GDateTime* _tmp39_;
	GDateTime* _tmp40_;
	ChronoParseResult* _tmp41_;
	GDateTime* _tmp42_;
	GDateTime* _tmp43_;
	ChronoParseResult* _tmp44_;
	GDateTime* _tmp45_;
	GDateTime* _tmp46_;
	GDateTime* _tmp47_;
	ChronoParseResult* _result_ = NULL;
	ChronoParseResult* _tmp48_;
	ChronoParseResult* _tmp49_;
	GDateTime* _tmp50_;
	gint start_pos = 0;
	gint end_pos = 0;
	GMatchInfo* _tmp51_;
	gint _tmp52_ = 0;
	gint _tmp53_ = 0;
	ChronoParseResult* _tmp54_;
	ChronoParseResult* _tmp55_;
	ChronoParseResult* _tmp56_;
	GMatchInfo* _tmp57_;
	gchar* _tmp58_;
	gchar* _tmp59_;
	ChronoParseResult* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (text != NULL, NULL);
	_tmp0_ = self->priv->combo_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_ = string_strip (_tmp5_);
	_tmp7_ = _tmp6_;
	_g_free0 (_tmp5_);
	date_part = _tmp7_;
	_tmp8_ = match;
	_tmp9_ = g_match_info_fetch (_tmp8_, 2);
	_tmp10_ = _tmp9_;
	_tmp11_ = string_strip (_tmp10_);
	_tmp12_ = _tmp11_;
	_g_free0 (_tmp10_);
	time_part = _tmp12_;
	_tmp13_ = self->priv->casual_parser;
	_tmp14_ = date_part;
	_tmp15_ = chrono_es_casual_date_parser_parse (_tmp13_, _tmp14_);
	date_result = _tmp15_;
	_tmp17_ = date_result;
	if (_tmp17_ == NULL) {
		_tmp16_ = TRUE;
	} else {
		ChronoParseResult* _tmp18_;
		GDateTime* _tmp19_;
		GDateTime* _tmp20_;
		_tmp18_ = date_result;
		_tmp19_ = chrono_parse_result_get_date (_tmp18_);
		_tmp20_ = _tmp19_;
		_tmp16_ = _tmp20_ == NULL;
	}
	if (_tmp16_) {
		result = NULL;
		_g_object_unref0 (date_result);
		_g_free0 (time_part);
		_g_free0 (date_part);
		_g_match_info_unref0 (match);
		return result;
	}
	_tmp21_ = self->priv->time_parser;
	_tmp22_ = time_part;
	_tmp23_ = chrono_time_parser_parse (_tmp21_, _tmp22_);
	time_result = _tmp23_;
	_tmp25_ = time_result;
	if (_tmp25_ == NULL) {
		_tmp24_ = TRUE;
	} else {
		ChronoParseResult* _tmp26_;
		GDateTime* _tmp27_;
		GDateTime* _tmp28_;
		_tmp26_ = time_result;
		_tmp27_ = chrono_parse_result_get_date (_tmp26_);
		_tmp28_ = _tmp27_;
		_tmp24_ = _tmp28_ == NULL;
	}
	if (_tmp24_) {
		result = NULL;
		_g_object_unref0 (time_result);
		_g_object_unref0 (date_result);
		_g_free0 (time_part);
		_g_free0 (date_part);
		_g_match_info_unref0 (match);
		return result;
	}
	_tmp29_ = date_result;
	_tmp30_ = chrono_parse_result_get_date (_tmp29_);
	_tmp31_ = _tmp30_;
	_tmp32_ = date_result;
	_tmp33_ = chrono_parse_result_get_date (_tmp32_);
	_tmp34_ = _tmp33_;
	_tmp35_ = date_result;
	_tmp36_ = chrono_parse_result_get_date (_tmp35_);
	_tmp37_ = _tmp36_;
	_tmp38_ = time_result;
	_tmp39_ = chrono_parse_result_get_date (_tmp38_);
	_tmp40_ = _tmp39_;
	_tmp41_ = time_result;
	_tmp42_ = chrono_parse_result_get_date (_tmp41_);
	_tmp43_ = _tmp42_;
	_tmp44_ = time_result;
	_tmp45_ = chrono_parse_result_get_date (_tmp44_);
	_tmp46_ = _tmp45_;
	_tmp47_ = g_date_time_new_local (g_date_time_get_year (_tmp31_), g_date_time_get_month (_tmp34_), g_date_time_get_day_of_month (_tmp37_), g_date_time_get_hour (_tmp40_), g_date_time_get_minute (_tmp43_), (gdouble) g_date_time_get_second (_tmp46_));
	combined_date = _tmp47_;
	_tmp48_ = chrono_parse_result_new ();
	_result_ = _tmp48_;
	_tmp49_ = _result_;
	_tmp50_ = combined_date;
	chrono_parse_result_set_date (_tmp49_, _tmp50_);
	_tmp51_ = match;
	g_match_info_fetch_pos (_tmp51_, 0, &_tmp52_, &_tmp53_);
	start_pos = _tmp52_;
	end_pos = _tmp53_;
	_tmp54_ = _result_;
	chrono_parse_result_set_start_index (_tmp54_, start_pos);
	_tmp55_ = _result_;
	chrono_parse_result_set_end_index (_tmp55_, end_pos);
	_tmp56_ = _result_;
	_tmp57_ = match;
	_tmp58_ = g_match_info_fetch (_tmp57_, 0);
	_tmp59_ = _tmp58_;
	chrono_parse_result_set_matched_text (_tmp56_, _tmp59_);
	_g_free0 (_tmp59_);
	result = _result_;
	_g_date_time_unref0 (combined_date);
	_g_object_unref0 (time_result);
	_g_object_unref0 (date_result);
	_g_free0 (time_part);
	_g_free0 (date_part);
	_g_match_info_unref0 (match);
	return result;
}

static void
chrono_es_date_time_combo_parser_class_init (ChronoESDateTimeComboParserClass * klass,
                                             gpointer klass_data)
{
	chrono_es_date_time_combo_parser_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ChronoESDateTimeComboParser_private_offset);
	G_OBJECT_CLASS (klass)->finalize = chrono_es_date_time_combo_parser_finalize;
}

static void
chrono_es_date_time_combo_parser_instance_init (ChronoESDateTimeComboParser * self,
                                                gpointer klass)
{
	self->priv = chrono_es_date_time_combo_parser_get_instance_private (self);
}

static void
chrono_es_date_time_combo_parser_finalize (GObject * obj)
{
	ChronoESDateTimeComboParser * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CHRONO_TYPE_ES_DATE_TIME_COMBO_PARSER, ChronoESDateTimeComboParser);
	_g_regex_unref0 (self->priv->combo_regex);
	_g_object_unref0 (self->priv->casual_parser);
	_g_object_unref0 (self->priv->time_parser);
	G_OBJECT_CLASS (chrono_es_date_time_combo_parser_parent_class)->finalize (obj);
}

/**
     * Spanish date + time combination parser
     * 
     * Supports: mañana a las 3pm, lunes a las 9am, 15 de marzo a las 14:30
     */
 G_GNUC_NO_INLINE static GType
chrono_es_date_time_combo_parser_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ChronoESDateTimeComboParserClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) chrono_es_date_time_combo_parser_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ChronoESDateTimeComboParser), 0, (GInstanceInitFunc) chrono_es_date_time_combo_parser_instance_init, NULL };
	GType chrono_es_date_time_combo_parser_type_id;
	chrono_es_date_time_combo_parser_type_id = g_type_register_static (G_TYPE_OBJECT, "ChronoESDateTimeComboParser", &g_define_type_info, 0);
	ChronoESDateTimeComboParser_private_offset = g_type_add_instance_private (chrono_es_date_time_combo_parser_type_id, sizeof (ChronoESDateTimeComboParserPrivate));
	return chrono_es_date_time_combo_parser_type_id;
}

GType
chrono_es_date_time_combo_parser_get_type (void)
{
	static gsize chrono_es_date_time_combo_parser_type_id__once = 0;
	if (g_once_init_enter (&chrono_es_date_time_combo_parser_type_id__once)) {
		GType chrono_es_date_time_combo_parser_type_id;
		chrono_es_date_time_combo_parser_type_id = chrono_es_date_time_combo_parser_get_type_once ();
		g_once_init_leave (&chrono_es_date_time_combo_parser_type_id__once, chrono_es_date_time_combo_parser_type_id);
	}
	return chrono_es_date_time_combo_parser_type_id__once;
}

