/* ESCasualTimeParser.c generated by valac 0.56.19, the Vala compiler
 * generated from ESCasualTimeParser.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_CASUAL_TIME_PARSER_0_PROPERTY,
	CHRONO_ES_CASUAL_TIME_PARSER_NUM_PROPERTIES
};
static GParamSpec* chrono_es_casual_time_parser_properties[CHRONO_ES_CASUAL_TIME_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 _ChronoESCasualTimeParserPrivate {
	GRegex* casual_time_regex;
};

static gint ChronoESCasualTimeParser_private_offset;
static gpointer chrono_es_casual_time_parser_parent_class = NULL;

static void chrono_es_casual_time_parser_finalize (GObject * obj);
static GType chrono_es_casual_time_parser_get_type_once (void);

static inline gpointer
chrono_es_casual_time_parser_get_instance_private (ChronoESCasualTimeParser* self)
{
	return G_STRUCT_MEMBER_P (self, ChronoESCasualTimeParser_private_offset);
}

ChronoESCasualTimeParser*
chrono_es_casual_time_parser_construct (GType object_type)
{
	ChronoESCasualTimeParser * self = NULL;
	GError* _inner_error0_ = NULL;
	self = (ChronoESCasualTimeParser*) g_object_new (object_type, NULL);
	{
		GRegex* _tmp0_ = NULL;
		GRegex* _tmp1_;
		GRegex* _tmp2_;
		_tmp1_ = g_regex_new ("(?:esta)?\\s{0,3}(mañana|manana|tarde|noche|medianoche|mediodía|mediod" \
"ia)(?=\\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->casual_time_regex);
		self->priv->casual_time_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 ("ESCasualTimeParser.vala:39: Error creating casual time 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;
}

ChronoESCasualTimeParser*
chrono_es_casual_time_parser_new (void)
{
	return chrono_es_casual_time_parser_construct (CHRONO_TYPE_ES_CASUAL_TIME_PARSER);
}

ChronoParseResult*
chrono_es_casual_time_parser_parse (ChronoESCasualTimeParser* self,
                                    const gchar* text)
{
	GMatchInfo* match = NULL;
	GRegex* _tmp0_;
	GMatchInfo* _tmp1_ = NULL;
	gboolean _tmp2_;
	gchar* keyword = NULL;
	GMatchInfo* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* _tmp7_;
	GDateTime* now = NULL;
	GDateTime* _tmp8_;
	gint hour = 0;
	const gchar* _tmp9_;
	const gchar* _tmp10_;
	GQuark _tmp12_ = 0U;
	static GQuark _tmp11_label0 = 0;
	static GQuark _tmp11_label1 = 0;
	static GQuark _tmp11_label2 = 0;
	static GQuark _tmp11_label3 = 0;
	static GQuark _tmp11_label4 = 0;
	static GQuark _tmp11_label5 = 0;
	static GQuark _tmp11_label6 = 0;
	ChronoParseResult* _result_ = NULL;
	ChronoParseResult* _tmp13_;
	ChronoParseResult* _tmp14_;
	GDateTime* _tmp15_;
	GDateTime* _tmp16_;
	GDateTime* _tmp17_;
	GDateTime* _tmp18_;
	GDateTime* _tmp19_;
	gint start_pos = 0;
	gint end_pos = 0;
	GMatchInfo* _tmp20_;
	gint _tmp21_ = 0;
	gint _tmp22_ = 0;
	ChronoParseResult* _tmp23_;
	ChronoParseResult* _tmp24_;
	ChronoParseResult* _tmp25_;
	GMatchInfo* _tmp26_;
	gchar* _tmp27_;
	gchar* _tmp28_;
	ChronoParseResult* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (text != NULL, NULL);
	_tmp0_ = self->priv->casual_time_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_;
	_g_free0 (_tmp5_);
	keyword = _tmp7_;
	_tmp8_ = g_date_time_new_now_local ();
	now = _tmp8_;
	_tmp9_ = keyword;
	_tmp10_ = _tmp9_;
	_tmp12_ = (NULL == _tmp10_) ? 0 : g_quark_from_string (_tmp10_);
	if ((_tmp12_ == ((0 != _tmp11_label0) ? _tmp11_label0 : (_tmp11_label0 = g_quark_from_static_string ("mañana")))) || (_tmp12_ == ((0 != _tmp11_label1) ? _tmp11_label1 : (_tmp11_label1 = g_quark_from_static_string ("manana"))))) {
		switch (0) {
			default:
			{
				hour = 9;
				break;
			}
		}
	} else if (_tmp12_ == ((0 != _tmp11_label2) ? _tmp11_label2 : (_tmp11_label2 = g_quark_from_static_string ("tarde")))) {
		switch (0) {
			default:
			{
				hour = 14;
				break;
			}
		}
	} else if (_tmp12_ == ((0 != _tmp11_label3) ? _tmp11_label3 : (_tmp11_label3 = g_quark_from_static_string ("noche")))) {
		switch (0) {
			default:
			{
				hour = 20;
				break;
			}
		}
	} else if (_tmp12_ == ((0 != _tmp11_label4) ? _tmp11_label4 : (_tmp11_label4 = g_quark_from_static_string ("medianoche")))) {
		switch (0) {
			default:
			{
				hour = 0;
				break;
			}
		}
	} else if ((_tmp12_ == ((0 != _tmp11_label5) ? _tmp11_label5 : (_tmp11_label5 = g_quark_from_static_string ("mediodía")))) || (_tmp12_ == ((0 != _tmp11_label6) ? _tmp11_label6 : (_tmp11_label6 = g_quark_from_static_string ("mediodia"))))) {
		switch (0) {
			default:
			{
				hour = 12;
				break;
			}
		}
	} else {
		switch (0) {
			default:
			{
				result = NULL;
				_g_date_time_unref0 (now);
				_g_free0 (keyword);
				_g_match_info_unref0 (match);
				return result;
			}
		}
	}
	_tmp13_ = chrono_parse_result_new ();
	_result_ = _tmp13_;
	_tmp14_ = _result_;
	_tmp15_ = now;
	_tmp16_ = now;
	_tmp17_ = now;
	_tmp18_ = g_date_time_new_local (g_date_time_get_year (_tmp15_), g_date_time_get_month (_tmp16_), g_date_time_get_day_of_month (_tmp17_), hour, 0, (gdouble) 0);
	_tmp19_ = _tmp18_;
	chrono_parse_result_set_date (_tmp14_, _tmp19_);
	_g_date_time_unref0 (_tmp19_);
	_tmp20_ = match;
	g_match_info_fetch_pos (_tmp20_, 0, &_tmp21_, &_tmp22_);
	start_pos = _tmp21_;
	end_pos = _tmp22_;
	_tmp23_ = _result_;
	chrono_parse_result_set_start_index (_tmp23_, start_pos);
	_tmp24_ = _result_;
	chrono_parse_result_set_end_index (_tmp24_, end_pos);
	_tmp25_ = _result_;
	_tmp26_ = match;
	_tmp27_ = g_match_info_fetch (_tmp26_, 0);
	_tmp28_ = _tmp27_;
	chrono_parse_result_set_matched_text (_tmp25_, _tmp28_);
	_g_free0 (_tmp28_);
	result = _result_;
	_g_date_time_unref0 (now);
	_g_free0 (keyword);
	_g_match_info_unref0 (match);
	return result;
}

static void
chrono_es_casual_time_parser_class_init (ChronoESCasualTimeParserClass * klass,
                                         gpointer klass_data)
{
	chrono_es_casual_time_parser_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ChronoESCasualTimeParser_private_offset);
	G_OBJECT_CLASS (klass)->finalize = chrono_es_casual_time_parser_finalize;
}

static void
chrono_es_casual_time_parser_instance_init (ChronoESCasualTimeParser * self,
                                            gpointer klass)
{
	self->priv = chrono_es_casual_time_parser_get_instance_private (self);
}

static void
chrono_es_casual_time_parser_finalize (GObject * obj)
{
	ChronoESCasualTimeParser * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CHRONO_TYPE_ES_CASUAL_TIME_PARSER, ChronoESCasualTimeParser);
	_g_regex_unref0 (self->priv->casual_time_regex);
	G_OBJECT_CLASS (chrono_es_casual_time_parser_parent_class)->finalize (obj);
}

/**
     * Spanish casual time expressions parser
     * 
     * Supports: mañana, tarde, noche, medianoche, mediodía
     * With optional "esta" prefix
     */
 G_GNUC_NO_INLINE static GType
chrono_es_casual_time_parser_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ChronoESCasualTimeParserClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) chrono_es_casual_time_parser_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ChronoESCasualTimeParser), 0, (GInstanceInitFunc) chrono_es_casual_time_parser_instance_init, NULL };
	GType chrono_es_casual_time_parser_type_id;
	chrono_es_casual_time_parser_type_id = g_type_register_static (G_TYPE_OBJECT, "ChronoESCasualTimeParser", &g_define_type_info, 0);
	ChronoESCasualTimeParser_private_offset = g_type_add_instance_private (chrono_es_casual_time_parser_type_id, sizeof (ChronoESCasualTimeParserPrivate));
	return chrono_es_casual_time_parser_type_id;
}

GType
chrono_es_casual_time_parser_get_type (void)
{
	static gsize chrono_es_casual_time_parser_type_id__once = 0;
	if (g_once_init_enter (&chrono_es_casual_time_parser_type_id__once)) {
		GType chrono_es_casual_time_parser_type_id;
		chrono_es_casual_time_parser_type_id = chrono_es_casual_time_parser_get_type_once ();
		g_once_init_leave (&chrono_es_casual_time_parser_type_id__once, chrono_es_casual_time_parser_type_id);
	}
	return chrono_es_casual_time_parser_type_id__once;
}

