/* date-utils.c generated by valac 0.56.19, the Vala compiler
 * generated from date-utils.vala, do not modify */

/*
 * Copyright (c) 2025 focus-timer contributors
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "ft_core.h"
#include <glib.h>
#include <string.h>
#include <stdlib.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

#define _g_date_time_unref0(var) ((var == NULL) ? NULL : (var = (g_date_time_unref (var), NULL)))
#define _g_variant_unref0(var) ((var == NULL) ? NULL : (var = (g_variant_unref (var), NULL)))

VALA_EXTERN guint ft_date_utils_get_weekday_number_internal (GDateWeekday weekday);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);

void
ft_date_utils_get_today (GDate* result)
{
	GDateTime* datetime = NULL;
	GDateTime* _tmp0_;
	GDate date = {0};
	_tmp0_ = g_date_time_new_now_local ();
	datetime = _tmp0_;
	memset (&date, 0, sizeof (GDate));
	g_date_set_dmy (&date, (GDateDay) g_date_time_get_day_of_month (datetime), (gint) ((GDateMonth) g_date_time_get_month (datetime)), (GDateYear) g_date_time_get_year (datetime));
	*result = date;
	_g_date_time_unref0 (datetime);
	return;
}

gchar*
ft_date_utils_format_date (GDate* date,
                           const gchar* format)
{
	GDateTime* datetime = NULL;
	GDateTime* _tmp0_;
	gchar* _tmp1_;
	gchar* result;
	g_return_val_if_fail (date != NULL, NULL);
	g_return_val_if_fail (format != NULL, NULL);
	_tmp0_ = g_date_time_new_local ((gint) g_date_get_year (date), (gint) g_date_get_month (date), (gint) g_date_get_day (date), 0, 0, 0.0);
	datetime = _tmp0_;
	_tmp1_ = g_date_time_format (datetime, format);
	result = _tmp1_;
	_g_date_time_unref0 (datetime);
	return result;
}

guint
ft_date_utils_get_weekday_number_internal (GDateWeekday weekday)
{
	guint result;
	switch (weekday) {
		case G_DATE_MONDAY:
		{
			result = 1U;
			return result;
		}
		case G_DATE_TUESDAY:
		{
			result = 2U;
			return result;
		}
		case G_DATE_WEDNESDAY:
		{
			result = 3U;
			return result;
		}
		case G_DATE_THURSDAY:
		{
			result = 4U;
			return result;
		}
		case G_DATE_FRIDAY:
		{
			result = 5U;
			return result;
		}
		case G_DATE_SATURDAY:
		{
			result = 6U;
			return result;
		}
		case G_DATE_SUNDAY:
		{
			result = 7U;
			return result;
		}
		default:
		{
			result = 0U;
			return result;
		}
	}
}

/**
     * Convert GLib.DateWeekday to integer
     *
     * The result is locale dependant. Starts from 1 - first day of a work week.
     */
guint
ft_date_utils_get_weekday_number (GDateWeekday weekday)
{
	gint weekday_number = 0;
	guint result;
	weekday_number = (gint) ft_date_utils_get_weekday_number_internal (weekday);
	if (weekday_number != 0) {
		gint first_day_of_week_number = 0;
		gint _result_ = 0;
		first_day_of_week_number = (gint) ft_date_utils_get_weekday_number_internal (ft_locale_get_first_day_of_week ());
		if (first_day_of_week_number == 0) {
			first_day_of_week_number = 7;
		}
		_result_ = (1 + weekday_number) - first_day_of_week_number;
		if (_result_ < 1) {
			_result_ += 7;
		}
		result = (guint) _result_;
		return result;
	} else {
		result = 0U;
		return result;
	}
}

guint
ft_date_utils_get_month_number (GDateMonth month)
{
	guint result;
	switch (month) {
		case G_DATE_JANUARY:
		{
			result = 1U;
			return result;
		}
		case G_DATE_FEBRUARY:
		{
			result = 2U;
			return result;
		}
		case G_DATE_MARCH:
		{
			result = 3U;
			return result;
		}
		case G_DATE_APRIL:
		{
			result = 4U;
			return result;
		}
		case G_DATE_MAY:
		{
			result = 5U;
			return result;
		}
		case G_DATE_JUNE:
		{
			result = 6U;
			return result;
		}
		case G_DATE_JULY:
		{
			result = 7U;
			return result;
		}
		case G_DATE_AUGUST:
		{
			result = 8U;
			return result;
		}
		case G_DATE_SEPTEMBER:
		{
			result = 9U;
			return result;
		}
		case G_DATE_OCTOBER:
		{
			result = 10U;
			return result;
		}
		case G_DATE_NOVEMBER:
		{
			result = 11U;
			return result;
		}
		case G_DATE_DECEMBER:
		{
			result = 12U;
			return result;
		}
		default:
		{
			result = 0U;
			return result;
		}
	}
}

gchar*
ft_date_utils_get_month_name (GDateMonth month)
{
	gchar* _tmp0_;
	gchar* result;
	_tmp0_ = ft_locale_get_month_name (ft_date_utils_get_month_number (month));
	result = _tmp0_;
	return result;
}

static gpointer
_g_variant_ref0 (gpointer self)
{
	return self ? g_variant_ref (self) : NULL;
}

GVariant*
ft_date_utils_date_to_variant (GDate* date)
{
	GVariant* day = NULL;
	GVariant* _tmp0_;
	GVariant* month = NULL;
	GVariant* _tmp1_;
	GVariant* year = NULL;
	GVariant* _tmp2_;
	GVariant* _tmp3_;
	GVariant* _tmp4_;
	GVariant* _tmp5_;
	GVariant** _tmp6_;
	GVariant** _tmp7_;
	gint _tmp7__length1;
	GVariant* _tmp8_;
	GVariant* _tmp9_;
	GVariant* result;
	g_return_val_if_fail (date != NULL, NULL);
	_tmp0_ = g_variant_new_uint16 ((guint16) g_date_get_day (date));
	g_variant_ref_sink (_tmp0_);
	day = _tmp0_;
	_tmp1_ = g_variant_new_uint16 ((guint16) g_date_get_month (date));
	g_variant_ref_sink (_tmp1_);
	month = _tmp1_;
	_tmp2_ = g_variant_new_uint16 ((guint16) g_date_get_year (date));
	g_variant_ref_sink (_tmp2_);
	year = _tmp2_;
	_tmp3_ = _g_variant_ref0 (day);
	_tmp4_ = _g_variant_ref0 (month);
	_tmp5_ = _g_variant_ref0 (year);
	_tmp6_ = g_new0 (GVariant*, 3 + 1);
	_tmp6_[0] = _tmp3_;
	_tmp6_[1] = _tmp4_;
	_tmp6_[2] = _tmp5_;
	_tmp7_ = _tmp6_;
	_tmp7__length1 = 3;
	_tmp8_ = g_variant_new_tuple (_tmp7_, 3);
	g_variant_ref_sink (_tmp8_);
	_tmp9_ = _tmp8_;
	_tmp7_ = (_vala_array_free (_tmp7_, _tmp7__length1, (GDestroyNotify) g_variant_unref), NULL);
	result = _tmp9_;
	_g_variant_unref0 (year);
	_g_variant_unref0 (month);
	_g_variant_unref0 (day);
	return result;
}

void
ft_date_utils_date_from_variant (GVariant* variant,
                                 GDate* result)
{
	GDate date = {0};
	const gchar* _tmp0_;
	g_return_if_fail (variant != NULL);
	memset (&date, 0, sizeof (GDate));
	_tmp0_ = g_variant_get_type_string (variant);
	if (g_strcmp0 (_tmp0_, "(qqq)") == 0) {
		GDateDay day = (GDateDay) '\0';
		GVariant* _tmp1_;
		GVariant* _tmp2_;
		GDateDay _tmp3_;
		GDateMonth month = 0;
		GVariant* _tmp4_;
		GVariant* _tmp5_;
		GDateMonth _tmp6_;
		GDateYear year = (GDateYear) 0U;
		GVariant* _tmp7_;
		GVariant* _tmp8_;
		GDateYear _tmp9_;
		gboolean _tmp10_ = FALSE;
		gboolean _tmp11_ = FALSE;
		_tmp1_ = g_variant_get_child_value (variant, (gsize) 0);
		_tmp2_ = _tmp1_;
		_tmp3_ = (GDateDay) g_variant_get_uint16 (_tmp2_);
		_g_variant_unref0 (_tmp2_);
		day = _tmp3_;
		_tmp4_ = g_variant_get_child_value (variant, (gsize) 1);
		_tmp5_ = _tmp4_;
		_tmp6_ = (GDateMonth) g_variant_get_uint16 (_tmp5_);
		_g_variant_unref0 (_tmp5_);
		month = _tmp6_;
		_tmp7_ = g_variant_get_child_value (variant, (gsize) 2);
		_tmp8_ = _tmp7_;
		_tmp9_ = (GDateYear) g_variant_get_uint16 (_tmp8_);
		_g_variant_unref0 (_tmp8_);
		year = _tmp9_;
		if (day != G_DATE_BAD_DAY) {
			_tmp11_ = month != G_DATE_BAD_MONTH;
		} else {
			_tmp11_ = FALSE;
		}
		if (_tmp11_) {
			_tmp10_ = year != ((GDateYear) G_DATE_BAD_YEAR);
		} else {
			_tmp10_ = FALSE;
		}
		if (_tmp10_) {
			g_date_set_dmy (&date, day, (gint) month, year);
		}
	}
	*result = date;
	return;
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

