/* ISOParser.c generated by valac 0.56.19, the Vala compiler
 * generated from ISOParser.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_ISO_PARSER_0_PROPERTY,
	CHRONO_ISO_PARSER_NUM_PROPERTIES
};
static GParamSpec* chrono_iso_parser_properties[CHRONO_ISO_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 _ChronoISOParserPrivate {
	GRegex* iso_regex;
};

static gint ChronoISOParser_private_offset;
static gpointer chrono_iso_parser_parent_class = NULL;

static void chrono_iso_parser_finalize (GObject * obj);
static GType chrono_iso_parser_get_type_once (void);

static inline gpointer
chrono_iso_parser_get_instance_private (ChronoISOParser* self)
{
	return G_STRUCT_MEMBER_P (self, ChronoISOParser_private_offset);
}

ChronoISOParser*
chrono_iso_parser_construct (GType object_type)
{
	ChronoISOParser * self = NULL;
	GError* _inner_error0_ = NULL;
	self = (ChronoISOParser*) g_object_new (object_type, NULL);
	{
		GRegex* _tmp0_ = NULL;
		GRegex* _tmp1_;
		GRegex* _tmp2_;
		_tmp1_ = g_regex_new ("(\\d{4})-(\\d{2})-(\\d{2})" "(?:T(\\d{2}):(\\d{2})(?::(\\d{2})(?:\\.(\\d+))?)?" "(Z|[+-]\\d{2}:\\d{2})?)?", 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->iso_regex);
		self->priv->iso_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 ("ISOParser.vala:43: Error creating ISO 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;
}

ChronoISOParser*
chrono_iso_parser_new (void)
{
	return chrono_iso_parser_construct (CHRONO_TYPE_ISO_PARSER);
}

ChronoParseResult*
chrono_iso_parser_parse (ChronoISOParser* self,
                         const gchar* text)
{
	GMatchInfo* match = NULL;
	GRegex* _tmp0_;
	GMatchInfo* _tmp1_ = NULL;
	gboolean _tmp2_;
	gint year = 0;
	GMatchInfo* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gint _tmp6_;
	gint month = 0;
	GMatchInfo* _tmp7_;
	gchar* _tmp8_;
	gchar* _tmp9_;
	gint _tmp10_;
	gint day = 0;
	GMatchInfo* _tmp11_;
	gchar* _tmp12_;
	gchar* _tmp13_;
	gint _tmp14_;
	gint hour = 0;
	gint minute = 0;
	gint second = 0;
	gchar* time_part = NULL;
	GMatchInfo* _tmp15_;
	gchar* _tmp16_;
	gboolean _tmp17_ = FALSE;
	const gchar* _tmp18_;
	ChronoParseResult* _result_ = NULL;
	ChronoParseResult* _tmp34_;
	ChronoParseResult* _tmp35_;
	GDateTime* _tmp36_;
	GDateTime* _tmp37_;
	gint start_pos = 0;
	gint end_pos = 0;
	GMatchInfo* _tmp38_;
	gint _tmp39_ = 0;
	gint _tmp40_ = 0;
	ChronoParseResult* _tmp41_;
	ChronoParseResult* _tmp42_;
	ChronoParseResult* _tmp43_;
	GMatchInfo* _tmp44_;
	gchar* _tmp45_;
	gchar* _tmp46_;
	ChronoParseResult* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (text != NULL, NULL);
	_tmp0_ = self->priv->iso_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_);
	year = _tmp6_;
	_tmp7_ = match;
	_tmp8_ = g_match_info_fetch (_tmp7_, 2);
	_tmp9_ = _tmp8_;
	_tmp10_ = atoi (_tmp9_);
	_g_free0 (_tmp9_);
	month = _tmp10_;
	_tmp11_ = match;
	_tmp12_ = g_match_info_fetch (_tmp11_, 3);
	_tmp13_ = _tmp12_;
	_tmp14_ = atoi (_tmp13_);
	_g_free0 (_tmp13_);
	day = _tmp14_;
	hour = 0;
	minute = 0;
	second = 0;
	_tmp15_ = match;
	_tmp16_ = g_match_info_fetch (_tmp15_, 4);
	time_part = _tmp16_;
	_tmp18_ = time_part;
	if (_tmp18_ != NULL) {
		const gchar* _tmp19_;
		gint _tmp20_;
		gint _tmp21_;
		_tmp19_ = time_part;
		_tmp20_ = strlen (_tmp19_);
		_tmp21_ = _tmp20_;
		_tmp17_ = _tmp21_ > 0;
	} else {
		_tmp17_ = FALSE;
	}
	if (_tmp17_) {
		const gchar* _tmp22_;
		GMatchInfo* _tmp23_;
		gchar* _tmp24_;
		gchar* _tmp25_;
		gchar* sec = NULL;
		GMatchInfo* _tmp26_;
		gchar* _tmp27_;
		gboolean _tmp28_ = FALSE;
		const gchar* _tmp29_;
		_tmp22_ = time_part;
		hour = atoi (_tmp22_);
		_tmp23_ = match;
		_tmp24_ = g_match_info_fetch (_tmp23_, 5);
		_tmp25_ = _tmp24_;
		minute = atoi (_tmp25_);
		_g_free0 (_tmp25_);
		_tmp26_ = match;
		_tmp27_ = g_match_info_fetch (_tmp26_, 6);
		sec = _tmp27_;
		_tmp29_ = sec;
		if (_tmp29_ != NULL) {
			const gchar* _tmp30_;
			gint _tmp31_;
			gint _tmp32_;
			_tmp30_ = sec;
			_tmp31_ = strlen (_tmp30_);
			_tmp32_ = _tmp31_;
			_tmp28_ = _tmp32_ > 0;
		} else {
			_tmp28_ = FALSE;
		}
		if (_tmp28_) {
			const gchar* _tmp33_;
			_tmp33_ = sec;
			second = atoi (_tmp33_);
		}
		_g_free0 (sec);
	}
	_tmp34_ = chrono_parse_result_new ();
	_result_ = _tmp34_;
	_tmp35_ = _result_;
	_tmp36_ = g_date_time_new_local (year, month, day, hour, minute, (gdouble) second);
	_tmp37_ = _tmp36_;
	chrono_parse_result_set_date (_tmp35_, _tmp37_);
	_g_date_time_unref0 (_tmp37_);
	_tmp38_ = match;
	g_match_info_fetch_pos (_tmp38_, 0, &_tmp39_, &_tmp40_);
	start_pos = _tmp39_;
	end_pos = _tmp40_;
	_tmp41_ = _result_;
	chrono_parse_result_set_start_index (_tmp41_, start_pos);
	_tmp42_ = _result_;
	chrono_parse_result_set_end_index (_tmp42_, end_pos);
	_tmp43_ = _result_;
	_tmp44_ = match;
	_tmp45_ = g_match_info_fetch (_tmp44_, 0);
	_tmp46_ = _tmp45_;
	chrono_parse_result_set_matched_text (_tmp43_, _tmp46_);
	_g_free0 (_tmp46_);
	result = _result_;
	_g_free0 (time_part);
	_g_match_info_unref0 (match);
	return result;
}

static void
chrono_iso_parser_class_init (ChronoISOParserClass * klass,
                              gpointer klass_data)
{
	chrono_iso_parser_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ChronoISOParser_private_offset);
	G_OBJECT_CLASS (klass)->finalize = chrono_iso_parser_finalize;
}

static void
chrono_iso_parser_instance_init (ChronoISOParser * self,
                                 gpointer klass)
{
	self->priv = chrono_iso_parser_get_instance_private (self);
}

static void
chrono_iso_parser_finalize (GObject * obj)
{
	ChronoISOParser * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CHRONO_TYPE_ISO_PARSER, ChronoISOParser);
	_g_regex_unref0 (self->priv->iso_regex);
	G_OBJECT_CLASS (chrono_iso_parser_parent_class)->finalize (obj);
}

 G_GNUC_NO_INLINE static GType
chrono_iso_parser_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ChronoISOParserClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) chrono_iso_parser_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ChronoISOParser), 0, (GInstanceInitFunc) chrono_iso_parser_instance_init, NULL };
	GType chrono_iso_parser_type_id;
	chrono_iso_parser_type_id = g_type_register_static (G_TYPE_OBJECT, "ChronoISOParser", &g_define_type_info, 0);
	ChronoISOParser_private_offset = g_type_add_instance_private (chrono_iso_parser_type_id, sizeof (ChronoISOParserPrivate));
	return chrono_iso_parser_type_id;
}

GType
chrono_iso_parser_get_type (void)
{
	static gsize chrono_iso_parser_type_id__once = 0;
	if (g_once_init_enter (&chrono_iso_parser_type_id__once)) {
		GType chrono_iso_parser_type_id;
		chrono_iso_parser_type_id = chrono_iso_parser_get_type_once ();
		g_once_init_leave (&chrono_iso_parser_type_id__once, chrono_iso_parser_type_id);
	}
	return chrono_iso_parser_type_id__once;
}

