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

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

#include <langinfo.h>
#include <glib.h>
#include "ft_core.h"
#include <stdlib.h>
#include <string.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_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

VALA_EXTERN GDateWeekday ft_locale_first_day_of_week;
GDateWeekday ft_locale_first_day_of_week = G_DATE_BAD_WEEKDAY;

static guint8* _vala_array_dup12 (guint8* self,
                           gssize length);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

const nl_item FT_LOCALE_MONTHS[12] = {MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, MON_7, MON_8, MON_9, MON_10, MON_11, MON_12};

/**
     * Based on gtkcalendar.c and https://sourceware.org/glibc/wiki/Locales
     */
static gchar
string_get (const gchar* self,
            glong index)
{
	gchar _tmp0_;
	gchar result;
	g_return_val_if_fail (self != NULL, '\0');
	_tmp0_ = ((gchar*) self)[index];
	result = _tmp0_;
	return result;
}

GDateWeekday
ft_locale_get_first_day_of_week (void)
{
	GDateWeekday result;
	if (!g_date_valid_weekday (ft_locale_first_day_of_week)) {
		const gchar* week_origin_ptr = NULL;
		const gchar* _tmp0_;
		gint week_1stday = 0;
		const gchar* _tmp1_;
		const gchar* first_weekday_str = NULL;
		const gchar* _tmp3_;
		gint first_weekday = 0;
		gboolean _tmp4_ = FALSE;
		const gchar* _tmp5_;
		GDateWeekday _tmp11_ = 0;
		_tmp0_ = nl_langinfo (_NL_TIME_WEEK_1STDAY);
		week_origin_ptr = _tmp0_;
		week_1stday = 0;
		_tmp1_ = week_origin_ptr;
		if (_tmp1_ != NULL) {
			gsize ptr_value = 0UL;
			const gchar* _tmp2_;
			guint32 week_origin = 0U;
			_tmp2_ = week_origin_ptr;
			ptr_value = (gsize) ((void*) _tmp2_);
			week_origin = (guint32) (ptr_value & 0xFFFFFFFFLL);
			if (week_origin == ((guint32) 19971130)) {
				week_1stday = 0;
			} else {
				if (week_origin == ((guint32) 19971201)) {
					week_1stday = 1;
				} else {
					g_warning ("locale.vala:90: Unknown value of _NL_TIME_WEEK_1STDAY: %u", (guint) week_origin);
				}
			}
		}
		_tmp3_ = nl_langinfo (_NL_TIME_FIRST_WEEKDAY);
		first_weekday_str = _tmp3_;
		first_weekday = 1;
		_tmp5_ = first_weekday_str;
		if (_tmp5_ != NULL) {
			const gchar* _tmp6_;
			gint _tmp7_;
			gint _tmp8_;
			_tmp6_ = first_weekday_str;
			_tmp7_ = strlen (_tmp6_);
			_tmp8_ = _tmp7_;
			_tmp4_ = _tmp8_ > 0;
		} else {
			_tmp4_ = FALSE;
		}
		if (_tmp4_) {
			guint8 weekday_byte = 0U;
			const gchar* _tmp9_;
			gboolean _tmp10_ = FALSE;
			_tmp9_ = first_weekday_str;
			weekday_byte = (guint8) string_get (_tmp9_, (glong) 0);
			if (((gint) weekday_byte) >= 1) {
				_tmp10_ = ((gint) weekday_byte) <= 7;
			} else {
				_tmp10_ = FALSE;
			}
			if (_tmp10_) {
				first_weekday = (gint) weekday_byte;
			} else {
				g_warning ("locale.vala:108: Unexpected _NL_TIME_FIRST_WEEKDAY byte value: %u", (guint) weekday_byte);
			}
		}
		if ((((week_1stday + first_weekday) - 1) % 7) == 0) {
			_tmp11_ = G_DATE_SUNDAY;
		} else {
			_tmp11_ = G_DATE_MONDAY;
		}
		ft_locale_first_day_of_week = _tmp11_;
	}
	result = ft_locale_first_day_of_week;
	return result;
}

static guint8*
string_get_data (const gchar* self,
                 gint* result_length1)
{
	guint8* result;
	guint8* res = NULL;
	gint res_length1;
	gint _res_size_;
	gint _tmp0_;
	gint _tmp1_;
	guint8* _tmp2_;
	gint _tmp2__length1;
	g_return_val_if_fail (self != NULL, NULL);
	res = (guint8*) self;
	res_length1 = -1;
	_res_size_ = res_length1;
	_tmp0_ = strlen (self);
	_tmp1_ = _tmp0_;
	res_length1 = (gint) _tmp1_;
	_tmp2_ = res;
	_tmp2__length1 = res_length1;
	if (result_length1) {
		*result_length1 = _tmp2__length1;
	}
	result = _tmp2_;
	return result;
}

static guint8*
_vala_array_dup12 (guint8* self,
                   gssize length)
{
	if (length > 0) {
		return _vala_memdup2 (self, length * sizeof (guint8));
	}
	return NULL;
}

gchar*
ft_locale_get_month_name (guint month_number)
{
	gboolean _tmp0_ = FALSE;
	gchar* month_name = NULL;
	nl_item _tmp2_;
	const gchar* _tmp3_;
	gchar* _tmp4_;
	const gchar* _tmp5_;
	GError* _inner_error0_ = NULL;
	gchar* result;
	if (month_number < ((guint) 1)) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = month_number > ((guint) 12);
	}
	if (_tmp0_) {
		gchar* _tmp1_;
		_tmp1_ = g_strdup ("");
		result = _tmp1_;
		return result;
	}
	_tmp2_ = FT_LOCALE_MONTHS[month_number - 1];
	_tmp3_ = nl_langinfo (_tmp2_);
	_tmp4_ = g_strdup (_tmp3_);
	month_name = _tmp4_;
	_tmp5_ = month_name;
	if (!g_utf8_validate (_tmp5_, (gssize) -1, NULL)) {
		{
			gchar* charset = NULL;
			const gchar* _tmp6_ = NULL;
			gchar* _tmp7_;
			guint8* bytes = NULL;
			const gchar* _tmp8_;
			guint8* _tmp9_;
			gint _tmp9__length1;
			gint _tmp10_ = 0;
			guint8* _tmp11_;
			gint _tmp11__length1;
			guint8* _tmp12_;
			gint _tmp12__length1;
			gint bytes_length1;
			gint _bytes_size_;
			gchar* _tmp13_ = NULL;
			guint8* _tmp14_;
			gint _tmp14__length1;
			const gchar* _tmp15_;
			gchar* _tmp16_;
			gchar* _tmp17_;
			g_get_charset (&_tmp6_);
			_g_free0 (charset);
			_tmp7_ = g_strdup (_tmp6_);
			charset = _tmp7_;
			_tmp8_ = month_name;
			_tmp9_ = string_get_data (_tmp8_, &_tmp10_);
			_tmp9__length1 = _tmp10_;
			_tmp11_ = _tmp9_;
			_tmp11__length1 = _tmp9__length1;
			_tmp12_ = (_tmp11_ != NULL) ? _vala_array_dup12 (_tmp11_, _tmp11__length1) : _tmp11_;
			_tmp12__length1 = _tmp11__length1;
			bytes = _tmp12_;
			bytes_length1 = _tmp12__length1;
			_bytes_size_ = bytes_length1;
			_tmp14_ = bytes;
			_tmp14__length1 = bytes_length1;
			_tmp15_ = charset;
			_tmp16_ = g_convert ((const gchar*) _tmp14_, (gssize) -1, "UTF-8", _tmp15_, NULL, NULL, &_inner_error0_);
			_tmp13_ = _tmp16_;
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				bytes = (g_free (bytes), NULL);
				_g_free0 (charset);
				if (_inner_error0_->domain == G_CONVERT_ERROR) {
					goto __catch0_g_convert_error;
				}
				_g_free0 (month_name);
				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;
			}
			_tmp17_ = _tmp13_;
			_tmp13_ = NULL;
			_g_free0 (month_name);
			month_name = _tmp17_;
			_g_free0 (_tmp13_);
			bytes = (g_free (bytes), NULL);
			_g_free0 (charset);
		}
		goto __finally0;
		__catch0_g_convert_error:
		{
			GError* _error_ = NULL;
			GError* _tmp18_;
			const gchar* _tmp19_;
			_error_ = _inner_error0_;
			_inner_error0_ = NULL;
			_tmp18_ = _error_;
			_tmp19_ = _tmp18_->message;
			g_warning ("locale.vala:141: Failed to convert month name to UTF-8: %s", _tmp19_);
			_g_error_free0 (_error_);
		}
		__finally0:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_free0 (month_name);
			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;
		}
	}
	result = month_name;
	return result;
}

/**
     * Return whether to prefer 12h (AM/PM) format.
     */
gboolean
ft_locale_use_12h_format (void)
{
	const gchar* t_fmt_ptr = NULL;
	const gchar* _tmp0_;
	gboolean _tmp1_ = FALSE;
	const gchar* _tmp2_;
	gboolean result;
	_tmp0_ = nl_langinfo (T_FMT);
	t_fmt_ptr = _tmp0_;
	_tmp2_ = t_fmt_ptr;
	if (_tmp2_ != NULL) {
		const gchar* _tmp3_;
		_tmp3_ = t_fmt_ptr;
		_tmp1_ = g_ascii_strcasecmp (_tmp3_, "%I") == 0;
	} else {
		_tmp1_ = FALSE;
	}
	result = _tmp1_;
	return result;
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

