/* Parser.c generated by valac 0.56.19, the Vala compiler
 * generated from Parser.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 <gee.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.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_PARSER_0_PROPERTY,
	CHRONO_PARSER_NUM_PROPERTIES
};
static GParamSpec* chrono_parser_properties[CHRONO_PARSER_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _ChronoParserPrivate {
	GeeHashMap* language_parsers;
};

static gint ChronoParser_private_offset;
static gpointer chrono_parser_parent_class = NULL;

static void chrono_parser_finalize (GObject * obj);
static GType chrono_parser_get_type_once (void);

static inline gpointer
chrono_parser_get_instance_private (ChronoParser* self)
{
	return G_STRUCT_MEMBER_P (self, ChronoParser_private_offset);
}

ChronoParser*
chrono_parser_construct (GType object_type)
{
	ChronoParser * self = NULL;
	GeeHashMap* _tmp0_;
	ChronoEnglishParser* _tmp1_;
	ChronoEnglishParser* _tmp2_;
	ChronoSpanishParser* _tmp3_;
	ChronoSpanishParser* _tmp4_;
	self = (ChronoParser*) g_object_new (object_type, NULL);
	_tmp0_ = gee_hash_map_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, CHRONO_TYPE_LANGUAGE_PARSER, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
	_g_object_unref0 (self->priv->language_parsers);
	self->priv->language_parsers = _tmp0_;
	_tmp1_ = chrono_english_parser_new ();
	_tmp2_ = _tmp1_;
	chrono_parser_register_language (self, "en", (ChronoLanguageParser*) _tmp2_);
	_g_object_unref0 (_tmp2_);
	_tmp3_ = chrono_spanish_parser_new ();
	_tmp4_ = _tmp3_;
	chrono_parser_register_language (self, "es", (ChronoLanguageParser*) _tmp4_);
	_g_object_unref0 (_tmp4_);
	return self;
}

ChronoParser*
chrono_parser_new (void)
{
	return chrono_parser_construct (CHRONO_TYPE_PARSER);
}

void
chrono_parser_register_language (ChronoParser* self,
                                 const gchar* code,
                                 ChronoLanguageParser* parser)
{
	GeeHashMap* _tmp0_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (code != NULL);
	g_return_if_fail (parser != NULL);
	_tmp0_ = self->priv->language_parsers;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp0_, code, parser);
}

ChronoParseResult*
chrono_parser_parse (ChronoParser* self,
                     const gchar* text,
                     const gchar* language,
                     gboolean parse_recurrence)
{
	GeeHashMap* _tmp0_;
	GeeHashMap* _tmp1_;
	gpointer _tmp2_;
	ChronoLanguageParser* _tmp3_;
	ChronoParseResult* _tmp4_;
	ChronoParseResult* _tmp5_;
	ChronoParseResult* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (text != NULL, NULL);
	g_return_val_if_fail (language != NULL, NULL);
	_tmp0_ = self->priv->language_parsers;
	if (!gee_abstract_map_has_key ((GeeAbstractMap*) _tmp0_, language)) {
		result = NULL;
		return result;
	}
	_tmp1_ = self->priv->language_parsers;
	_tmp2_ = gee_abstract_map_get ((GeeAbstractMap*) _tmp1_, language);
	_tmp3_ = (ChronoLanguageParser*) _tmp2_;
	_tmp4_ = chrono_language_parser_parse (_tmp3_, text, parse_recurrence);
	_tmp5_ = _tmp4_;
	_g_object_unref0 (_tmp3_);
	result = _tmp5_;
	return result;
}

static void
chrono_parser_class_init (ChronoParserClass * klass,
                          gpointer klass_data)
{
	chrono_parser_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ChronoParser_private_offset);
	G_OBJECT_CLASS (klass)->finalize = chrono_parser_finalize;
}

static void
chrono_parser_instance_init (ChronoParser * self,
                             gpointer klass)
{
	self->priv = chrono_parser_get_instance_private (self);
}

static void
chrono_parser_finalize (GObject * obj)
{
	ChronoParser * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CHRONO_TYPE_PARSER, ChronoParser);
	_g_object_unref0 (self->priv->language_parsers);
	G_OBJECT_CLASS (chrono_parser_parent_class)->finalize (obj);
}

 G_GNUC_NO_INLINE static GType
chrono_parser_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ChronoParserClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) chrono_parser_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ChronoParser), 0, (GInstanceInitFunc) chrono_parser_instance_init, NULL };
	GType chrono_parser_type_id;
	chrono_parser_type_id = g_type_register_static (G_TYPE_OBJECT, "ChronoParser", &g_define_type_info, 0);
	ChronoParser_private_offset = g_type_add_instance_private (chrono_parser_type_id, sizeof (ChronoParserPrivate));
	return chrono_parser_type_id;
}

GType
chrono_parser_get_type (void)
{
	static gsize chrono_parser_type_id__once = 0;
	if (g_once_init_enter (&chrono_parser_type_id__once)) {
		GType chrono_parser_type_id;
		chrono_parser_type_id = chrono_parser_get_type_once ();
		g_once_init_leave (&chrono_parser_type_id__once, chrono_parser_type_id);
	}
	return chrono_parser_type_id__once;
}

