/* ENCasualDateParser.c generated by valac 0.56.19, the Vala compiler
 * generated from ENCasualDateParser.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_CASUAL_DATE_PARSER_0_PROPERTY,
	CHRONO_EN_CASUAL_DATE_PARSER_NUM_PROPERTIES
};
static GParamSpec* chrono_en_casual_date_parser_properties[CHRONO_EN_CASUAL_DATE_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 _ChronoENCasualDateParserPrivate {
	GRegex* casual_regex;
};

static gint ChronoENCasualDateParser_private_offset;
static gpointer chrono_en_casual_date_parser_parent_class = NULL;

static void chrono_en_casual_date_parser_finalize (GObject * obj);
static GType chrono_en_casual_date_parser_get_type_once (void);

static inline gpointer
chrono_en_casual_date_parser_get_instance_private (ChronoENCasualDateParser* self)
{
	return G_STRUCT_MEMBER_P (self, ChronoENCasualDateParser_private_offset);
}

ChronoENCasualDateParser*
chrono_en_casual_date_parser_construct (GType object_type)
{
	ChronoENCasualDateParser * self = NULL;
	GError* _inner_error0_ = NULL;
	self = (ChronoENCasualDateParser*) g_object_new (object_type, NULL);
	{
		GRegex* _tmp0_ = NULL;
		GRegex* _tmp1_;
		GRegex* _tmp2_;
		_tmp1_ = g_regex_new ("\\b(now|today|tonight|tomorrow|overmorrow|tmr|tmrw|yesterday|last\\s*n" \
"ight)\\b", 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_regex);
		self->priv->casual_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 ("ENCasualDateParser.vala:38: Error creating casual 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;
}

ChronoENCasualDateParser*
chrono_en_casual_date_parser_new (void)
{
	return chrono_en_casual_date_parser_construct (CHRONO_TYPE_EN_CASUAL_DATE_PARSER);
}

static gchar*
string_replace (const gchar* self,
                const gchar* old,
                const gchar* replacement)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (old != NULL, NULL);
	g_return_val_if_fail (replacement != NULL, NULL);
	if ((*((gchar*) self)) == '\0') {
		_tmp1_ = TRUE;
	} else {
		_tmp1_ = (*((gchar*) old)) == '\0';
	}
	if (_tmp1_) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = g_strcmp0 (old, replacement) == 0;
	}
	if (_tmp0_) {
		gchar* _tmp2_;
		_tmp2_ = g_strdup (self);
		result = _tmp2_;
		return result;
	}
	{
		GRegex* regex = NULL;
		gchar* _tmp3_;
		gchar* _tmp4_;
		GRegex* _tmp5_;
		GRegex* _tmp6_;
		gchar* _tmp7_ = NULL;
		GRegex* _tmp8_;
		gchar* _tmp9_;
		gchar* _tmp10_;
		_tmp3_ = g_regex_escape_string (old, -1);
		_tmp4_ = _tmp3_;
		_tmp5_ = g_regex_new (_tmp4_, 0, 0, &_inner_error0_);
		_tmp6_ = _tmp5_;
		_g_free0 (_tmp4_);
		regex = _tmp6_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected 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;
		}
		_tmp8_ = regex;
		_tmp9_ = g_regex_replace_literal (_tmp8_, self, (gssize) -1, 0, replacement, 0, &_inner_error0_);
		_tmp7_ = _tmp9_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_regex_unref0 (regex);
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected 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;
		}
		_tmp10_ = _tmp7_;
		_tmp7_ = NULL;
		result = _tmp10_;
		_g_free0 (_tmp7_);
		_g_regex_unref0 (regex);
		return result;
	}
	goto __finally0;
	__catch0_g_regex_error:
	{
		g_clear_error (&_inner_error0_);
		g_assert_not_reached ();
	}
	__finally0:
	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;
}

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

ChronoParseResult*
chrono_en_casual_date_parser_parse (ChronoENCasualDateParser* 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_;
	gchar* _tmp8_;
	gchar* _tmp9_;
	GDateTime* now = NULL;
	GDateTime* _tmp10_;
	GDateTime* date = NULL;
	const gchar* _tmp11_;
	const gchar* _tmp12_;
	GQuark _tmp14_ = 0U;
	static GQuark _tmp13_label0 = 0;
	static GQuark _tmp13_label1 = 0;
	static GQuark _tmp13_label2 = 0;
	static GQuark _tmp13_label3 = 0;
	static GQuark _tmp13_label4 = 0;
	static GQuark _tmp13_label5 = 0;
	static GQuark _tmp13_label6 = 0;
	static GQuark _tmp13_label7 = 0;
	static GQuark _tmp13_label8 = 0;
	ChronoParseResult* _result_ = NULL;
	ChronoParseResult* _tmp49_;
	ChronoParseResult* _tmp50_;
	GDateTime* _tmp51_;
	gint start_pos = 0;
	gint end_pos = 0;
	GMatchInfo* _tmp52_;
	gint _tmp53_ = 0;
	gint _tmp54_ = 0;
	ChronoParseResult* _tmp55_;
	ChronoParseResult* _tmp56_;
	ChronoParseResult* _tmp57_;
	GMatchInfo* _tmp58_;
	gchar* _tmp59_;
	gchar* _tmp60_;
	ChronoParseResult* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (text != NULL, NULL);
	_tmp0_ = self->priv->casual_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_;
	_tmp8_ = string_replace (_tmp7_, " ", "");
	_tmp9_ = _tmp8_;
	_g_free0 (_tmp7_);
	_g_free0 (_tmp5_);
	keyword = _tmp9_;
	_tmp10_ = g_date_time_new_now_local ();
	now = _tmp10_;
	_tmp11_ = keyword;
	_tmp12_ = _tmp11_;
	_tmp14_ = (NULL == _tmp12_) ? 0 : g_quark_from_string (_tmp12_);
	if (_tmp14_ == ((0 != _tmp13_label0) ? _tmp13_label0 : (_tmp13_label0 = g_quark_from_static_string ("now")))) {
		switch (0) {
			default:
			{
				GDateTime* _tmp15_;
				GDateTime* _tmp16_;
				_tmp15_ = now;
				_tmp16_ = _g_date_time_ref0 (_tmp15_);
				_g_date_time_unref0 (date);
				date = _tmp16_;
				break;
			}
		}
	} else if (_tmp14_ == ((0 != _tmp13_label1) ? _tmp13_label1 : (_tmp13_label1 = g_quark_from_static_string ("today")))) {
		switch (0) {
			default:
			{
				GDateTime* _tmp17_;
				GDateTime* _tmp18_;
				GDateTime* _tmp19_;
				GDateTime* _tmp20_;
				_tmp17_ = now;
				_tmp18_ = now;
				_tmp19_ = now;
				_tmp20_ = g_date_time_new_local (g_date_time_get_year (_tmp17_), g_date_time_get_month (_tmp18_), g_date_time_get_day_of_month (_tmp19_), 0, 0, (gdouble) 0);
				_g_date_time_unref0 (date);
				date = _tmp20_;
				break;
			}
		}
	} else if (_tmp14_ == ((0 != _tmp13_label2) ? _tmp13_label2 : (_tmp13_label2 = g_quark_from_static_string ("tonight")))) {
		switch (0) {
			default:
			{
				GDateTime* _tmp21_;
				GDateTime* _tmp22_;
				GDateTime* _tmp23_;
				GDateTime* _tmp24_;
				_tmp21_ = now;
				_tmp22_ = now;
				_tmp23_ = now;
				_tmp24_ = g_date_time_new_local (g_date_time_get_year (_tmp21_), g_date_time_get_month (_tmp22_), g_date_time_get_day_of_month (_tmp23_), 20, 0, (gdouble) 0);
				_g_date_time_unref0 (date);
				date = _tmp24_;
				break;
			}
		}
	} else if (((_tmp14_ == ((0 != _tmp13_label3) ? _tmp13_label3 : (_tmp13_label3 = g_quark_from_static_string ("tomorrow")))) || (_tmp14_ == ((0 != _tmp13_label4) ? _tmp13_label4 : (_tmp13_label4 = g_quark_from_static_string ("tmr"))))) || (_tmp14_ == ((0 != _tmp13_label5) ? _tmp13_label5 : (_tmp13_label5 = g_quark_from_static_string ("tmrw"))))) {
		switch (0) {
			default:
			{
				GDateTime* tomorrow = NULL;
				GDateTime* _tmp25_;
				GDateTime* _tmp26_;
				GDateTime* _tmp27_;
				GDateTime* _tmp28_;
				GDateTime* _tmp29_;
				GDateTime* _tmp30_;
				_tmp25_ = now;
				_tmp26_ = g_date_time_add_days (_tmp25_, 1);
				tomorrow = _tmp26_;
				_tmp27_ = tomorrow;
				_tmp28_ = tomorrow;
				_tmp29_ = tomorrow;
				_tmp30_ = g_date_time_new_local (g_date_time_get_year (_tmp27_), g_date_time_get_month (_tmp28_), g_date_time_get_day_of_month (_tmp29_), 0, 0, (gdouble) 0);
				_g_date_time_unref0 (date);
				date = _tmp30_;
				_g_date_time_unref0 (tomorrow);
				break;
			}
		}
	} else if (_tmp14_ == ((0 != _tmp13_label6) ? _tmp13_label6 : (_tmp13_label6 = g_quark_from_static_string ("overmorrow")))) {
		switch (0) {
			default:
			{
				GDateTime* overmorrow = NULL;
				GDateTime* _tmp31_;
				GDateTime* _tmp32_;
				GDateTime* _tmp33_;
				GDateTime* _tmp34_;
				GDateTime* _tmp35_;
				GDateTime* _tmp36_;
				_tmp31_ = now;
				_tmp32_ = g_date_time_add_days (_tmp31_, 2);
				overmorrow = _tmp32_;
				_tmp33_ = overmorrow;
				_tmp34_ = overmorrow;
				_tmp35_ = overmorrow;
				_tmp36_ = g_date_time_new_local (g_date_time_get_year (_tmp33_), g_date_time_get_month (_tmp34_), g_date_time_get_day_of_month (_tmp35_), 0, 0, (gdouble) 0);
				_g_date_time_unref0 (date);
				date = _tmp36_;
				_g_date_time_unref0 (overmorrow);
				break;
			}
		}
	} else if (_tmp14_ == ((0 != _tmp13_label7) ? _tmp13_label7 : (_tmp13_label7 = g_quark_from_static_string ("yesterday")))) {
		switch (0) {
			default:
			{
				GDateTime* yesterday = NULL;
				GDateTime* _tmp37_;
				GDateTime* _tmp38_;
				GDateTime* _tmp39_;
				GDateTime* _tmp40_;
				GDateTime* _tmp41_;
				GDateTime* _tmp42_;
				_tmp37_ = now;
				_tmp38_ = g_date_time_add_days (_tmp37_, -1);
				yesterday = _tmp38_;
				_tmp39_ = yesterday;
				_tmp40_ = yesterday;
				_tmp41_ = yesterday;
				_tmp42_ = g_date_time_new_local (g_date_time_get_year (_tmp39_), g_date_time_get_month (_tmp40_), g_date_time_get_day_of_month (_tmp41_), 0, 0, (gdouble) 0);
				_g_date_time_unref0 (date);
				date = _tmp42_;
				_g_date_time_unref0 (yesterday);
				break;
			}
		}
	} else if (_tmp14_ == ((0 != _tmp13_label8) ? _tmp13_label8 : (_tmp13_label8 = g_quark_from_static_string ("lastnight")))) {
		switch (0) {
			default:
			{
				GDateTime* _tmp43_;
				GDateTime* _tmp44_;
				GDateTime* _tmp45_;
				GDateTime* _tmp46_;
				GDateTime* _tmp47_;
				GDateTime* _tmp48_;
				_tmp43_ = now;
				_tmp44_ = now;
				_tmp45_ = now;
				_tmp46_ = g_date_time_new_local (g_date_time_get_year (_tmp43_), g_date_time_get_month (_tmp44_), g_date_time_get_day_of_month (_tmp45_), 20, 0, (gdouble) 0);
				_tmp47_ = _tmp46_;
				_tmp48_ = g_date_time_add_days (_tmp47_, -1);
				_g_date_time_unref0 (date);
				date = _tmp48_;
				_g_date_time_unref0 (_tmp47_);
				break;
			}
		}
	} else {
		switch (0) {
			default:
			{
				result = NULL;
				_g_date_time_unref0 (date);
				_g_date_time_unref0 (now);
				_g_free0 (keyword);
				_g_match_info_unref0 (match);
				return result;
			}
		}
	}
	_tmp49_ = chrono_parse_result_new ();
	_result_ = _tmp49_;
	_tmp50_ = _result_;
	_tmp51_ = date;
	chrono_parse_result_set_date (_tmp50_, _tmp51_);
	_tmp52_ = match;
	g_match_info_fetch_pos (_tmp52_, 0, &_tmp53_, &_tmp54_);
	start_pos = _tmp53_;
	end_pos = _tmp54_;
	_tmp55_ = _result_;
	chrono_parse_result_set_start_index (_tmp55_, start_pos);
	_tmp56_ = _result_;
	chrono_parse_result_set_end_index (_tmp56_, end_pos);
	_tmp57_ = _result_;
	_tmp58_ = match;
	_tmp59_ = g_match_info_fetch (_tmp58_, 0);
	_tmp60_ = _tmp59_;
	chrono_parse_result_set_matched_text (_tmp57_, _tmp60_);
	_g_free0 (_tmp60_);
	result = _result_;
	_g_date_time_unref0 (date);
	_g_date_time_unref0 (now);
	_g_free0 (keyword);
	_g_match_info_unref0 (match);
	return result;
}

static void
chrono_en_casual_date_parser_class_init (ChronoENCasualDateParserClass * klass,
                                         gpointer klass_data)
{
	chrono_en_casual_date_parser_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ChronoENCasualDateParser_private_offset);
	G_OBJECT_CLASS (klass)->finalize = chrono_en_casual_date_parser_finalize;
}

static void
chrono_en_casual_date_parser_instance_init (ChronoENCasualDateParser * self,
                                            gpointer klass)
{
	self->priv = chrono_en_casual_date_parser_get_instance_private (self);
}

static void
chrono_en_casual_date_parser_finalize (GObject * obj)
{
	ChronoENCasualDateParser * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CHRONO_TYPE_EN_CASUAL_DATE_PARSER, ChronoENCasualDateParser);
	_g_regex_unref0 (self->priv->casual_regex);
	G_OBJECT_CLASS (chrono_en_casual_date_parser_parent_class)->finalize (obj);
}

/**
     * English casual date expressions parser
     * 
     * Supports: now, today, tonight, tomorrow, overmorrow, tmr, tmrw, yesterday, last night
     */
 G_GNUC_NO_INLINE static GType
chrono_en_casual_date_parser_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ChronoENCasualDateParserClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) chrono_en_casual_date_parser_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ChronoENCasualDateParser), 0, (GInstanceInitFunc) chrono_en_casual_date_parser_instance_init, NULL };
	GType chrono_en_casual_date_parser_type_id;
	chrono_en_casual_date_parser_type_id = g_type_register_static (G_TYPE_OBJECT, "ChronoENCasualDateParser", &g_define_type_info, 0);
	ChronoENCasualDateParser_private_offset = g_type_add_instance_private (chrono_en_casual_date_parser_type_id, sizeof (ChronoENCasualDateParserPrivate));
	return chrono_en_casual_date_parser_type_id;
}

GType
chrono_en_casual_date_parser_get_type (void)
{
	static gsize chrono_en_casual_date_parser_type_id__once = 0;
	if (g_once_init_enter (&chrono_en_casual_date_parser_type_id__once)) {
		GType chrono_en_casual_date_parser_type_id;
		chrono_en_casual_date_parser_type_id = chrono_en_casual_date_parser_get_type_once ();
		g_once_init_leave (&chrono_en_casual_date_parser_type_id__once, chrono_en_casual_date_parser_type_id);
	}
	return chrono_en_casual_date_parser_type_id__once;
}

