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

#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib/gstdio.h>

#define FLAG_UTILS_SHARED_LAYOUTS_CFG "/usr/share/wl-kbd-assets/xkeyboardconfig/layouts.cfg"
#define FLAG_UTILS_SHARED_FLAGS_PNG_DIR "/usr/share/wl-kbd-assets/flags-png"
#define FLAG_UTILS_SHARED_FLAGS_SVG_DIR "/usr/share/wl-kbd-assets/flags"
#define FLAG_UTILS_CONFIG_LAYOUTS_CFG "/usr/share/wl-kbd-config/xkeyboardconfig/layouts.cfg"
#define FLAG_UTILS_CONFIG_FLAGS_PNG_DIR "/usr/share/wl-kbd-config/flags-png"
#define FLAG_UTILS_CONFIG_FLAGS_SVG_DIR "/usr/share/wl-kbd-config/flags"
#define FLAG_UTILS_LEGACY_FLAGS_SVG_DIR "/usr/share/labwc-keyboard-indicator/flags"
#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
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define TYPE_FLAG_UTILS (flag_utils_get_type ())
#define FLAG_UTILS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_FLAG_UTILS, FlagUtils))
#define FLAG_UTILS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_FLAG_UTILS, FlagUtilsClass))
#define IS_FLAG_UTILS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_FLAG_UTILS))
#define IS_FLAG_UTILS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_FLAG_UTILS))
#define FLAG_UTILS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_FLAG_UTILS, FlagUtilsClass))

typedef struct _FlagUtils FlagUtils;
typedef struct _FlagUtilsClass FlagUtilsClass;
typedef struct _FlagUtilsPrivate FlagUtilsPrivate;
enum  {
	FLAG_UTILS_0_PROPERTY,
	FLAG_UTILS_NUM_PROPERTIES
};
static GParamSpec* flag_utils_properties[FLAG_UTILS_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_string_free0(var) ((var == NULL) ? NULL : (var = (g_string_free (var, TRUE), NULL)))

struct _FlagUtils {
	GObject parent_instance;
	FlagUtilsPrivate * priv;
};

struct _FlagUtilsClass {
	GObjectClass parent_class;
};

static gpointer flag_utils_parent_class = NULL;

VALA_EXTERN GType flag_utils_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (FlagUtils, g_object_unref)
static gchar* flag_utils_find_existing_path (gchar** candidates,
                                      gint candidates_length1,
                                      GFileTest test);
static gchar* flag_utils_resolve_source_path (const gchar* relative_path,
                                       GFileTest test);
VALA_EXTERN gchar* flag_utils_build_layouts_config_path (void);
VALA_EXTERN gchar* flag_utils_build_flag_dir_path (void);
VALA_EXTERN gchar* flag_utils_build_svg_flag_dir_path (void);
VALA_EXTERN gchar* flag_utils_get_layout_base_code (const gchar* code);
VALA_EXTERN gchar* flag_utils_get_layout_icon_code (const gchar* layout_code);
VALA_EXTERN gchar* flag_utils_sanitize_icon_token (const gchar* value);
VALA_EXTERN gboolean flag_utils_has_flag_asset (const gchar* layout_code);
VALA_EXTERN FlagUtils* flag_utils_new (void);
VALA_EXTERN FlagUtils* flag_utils_construct (GType object_type);
static GType flag_utils_get_type_once (void);
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);

static gchar*
flag_utils_find_existing_path (gchar** candidates,
                               gint candidates_length1,
                               GFileTest test)
{
	gchar* _tmp4_;
	gchar* result;
	{
		gchar** candidate_collection = NULL;
		gint candidate_collection_length1 = 0;
		gint _candidate_collection_size_ = 0;
		gint candidate_it = 0;
		candidate_collection = candidates;
		candidate_collection_length1 = candidates_length1;
		for (candidate_it = 0; candidate_it < candidate_collection_length1; candidate_it = candidate_it + 1) {
			gchar* _tmp0_;
			gchar* candidate = NULL;
			_tmp0_ = g_strdup (candidate_collection[candidate_it]);
			candidate = _tmp0_;
			{
				gboolean _tmp1_ = FALSE;
				const gchar* _tmp2_;
				_tmp2_ = candidate;
				if (g_strcmp0 (_tmp2_, "") != 0) {
					const gchar* _tmp3_;
					_tmp3_ = candidate;
					_tmp1_ = g_file_test (_tmp3_, test);
				} else {
					_tmp1_ = FALSE;
				}
				if (_tmp1_) {
					result = candidate;
					return result;
				}
				_g_free0 (candidate);
			}
		}
	}
	_tmp4_ = g_strdup ("");
	result = _tmp4_;
	return result;
}

static gchar*
flag_utils_resolve_source_path (const gchar* relative_path,
                                GFileTest test)
{
	gchar* cwd = NULL;
	gchar* _tmp0_;
	gchar** candidates = NULL;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar** _tmp3_;
	gint candidates_length1;
	gint _candidates_size_;
	gchar* _tmp4_;
	gchar* result;
	g_return_val_if_fail (relative_path != NULL, NULL);
	_tmp0_ = g_get_current_dir ();
	cwd = _tmp0_;
	_tmp1_ = g_build_filename (cwd, "data", relative_path, NULL);
	_tmp2_ = g_build_filename (cwd, "..", "data", relative_path, NULL);
	_tmp3_ = g_new0 (gchar*, 2 + 1);
	_tmp3_[0] = _tmp1_;
	_tmp3_[1] = _tmp2_;
	candidates = _tmp3_;
	candidates_length1 = 2;
	_candidates_size_ = candidates_length1;
	_tmp4_ = flag_utils_find_existing_path (candidates, (gint) candidates_length1, test);
	result = _tmp4_;
	candidates = (_vala_array_free (candidates, candidates_length1, (GDestroyNotify) g_free), NULL);
	_g_free0 (cwd);
	return result;
}

gchar*
flag_utils_build_layouts_config_path (void)
{
	gchar* installed = NULL;
	gchar* _tmp0_;
	gchar* resolved = NULL;
	gchar* _tmp1_;
	gchar* _tmp2_;
	const gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* _tmp7_;
	gchar** _tmp8_;
	gchar** _tmp9_;
	gint _tmp9__length1;
	gchar* _tmp10_;
	gchar* _tmp11_;
	const gchar* _tmp12_;
	gchar* result;
	_tmp0_ = g_strdup (FLAG_UTILS_SHARED_LAYOUTS_CFG);
	installed = _tmp0_;
	_tmp1_ = g_strdup (FLAG_UTILS_SHARED_LAYOUTS_CFG);
	_tmp2_ = g_strdup (FLAG_UTILS_CONFIG_LAYOUTS_CFG);
	_tmp3_ = installed;
	_tmp4_ = g_strdup (_tmp3_);
	_tmp5_ = g_build_filename ("xkeyboardconfig", "layouts.cfg", NULL);
	_tmp6_ = _tmp5_;
	_tmp7_ = flag_utils_resolve_source_path (_tmp6_, G_FILE_TEST_EXISTS);
	_tmp8_ = g_new0 (gchar*, 4 + 1);
	_tmp8_[0] = _tmp1_;
	_tmp8_[1] = _tmp2_;
	_tmp8_[2] = _tmp4_;
	_tmp8_[3] = _tmp7_;
	_tmp9_ = _tmp8_;
	_tmp9__length1 = 4;
	_tmp10_ = flag_utils_find_existing_path (_tmp9_, (gint) 4, G_FILE_TEST_EXISTS);
	_tmp11_ = _tmp10_;
	_tmp9_ = (_vala_array_free (_tmp9_, _tmp9__length1, (GDestroyNotify) g_free), NULL);
	_g_free0 (_tmp6_);
	resolved = _tmp11_;
	_tmp12_ = resolved;
	if (g_strcmp0 (_tmp12_, "") != 0) {
		result = resolved;
		_g_free0 (installed);
		return result;
	}
	result = installed;
	_g_free0 (resolved);
	return result;
}

gchar*
flag_utils_build_flag_dir_path (void)
{
	gchar* installed_png = NULL;
	gchar* _tmp0_;
	gchar* resolved_png = NULL;
	gchar* _tmp1_;
	gchar* _tmp2_;
	const gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar** _tmp6_;
	gchar** _tmp7_;
	gint _tmp7__length1;
	gchar* _tmp8_;
	gchar* _tmp9_;
	const gchar* _tmp10_;
	gchar* installed_svg = NULL;
	gchar* _tmp11_;
	gchar* legacy_installed_svg = NULL;
	gchar* _tmp12_;
	gchar* resolved_svg = NULL;
	gchar* _tmp13_;
	gchar* _tmp14_;
	const gchar* _tmp15_;
	gchar* _tmp16_;
	const gchar* _tmp17_;
	gchar* _tmp18_;
	gchar* _tmp19_;
	gchar** _tmp20_;
	gchar** _tmp21_;
	gint _tmp21__length1;
	gchar* _tmp22_;
	gchar* _tmp23_;
	const gchar* _tmp24_;
	gchar* result;
	_tmp0_ = g_strdup (FLAG_UTILS_SHARED_FLAGS_PNG_DIR);
	installed_png = _tmp0_;
	_tmp1_ = g_strdup (FLAG_UTILS_SHARED_FLAGS_PNG_DIR);
	_tmp2_ = g_strdup (FLAG_UTILS_CONFIG_FLAGS_PNG_DIR);
	_tmp3_ = installed_png;
	_tmp4_ = g_strdup (_tmp3_);
	_tmp5_ = flag_utils_resolve_source_path ("flags-png", G_FILE_TEST_IS_DIR);
	_tmp6_ = g_new0 (gchar*, 4 + 1);
	_tmp6_[0] = _tmp1_;
	_tmp6_[1] = _tmp2_;
	_tmp6_[2] = _tmp4_;
	_tmp6_[3] = _tmp5_;
	_tmp7_ = _tmp6_;
	_tmp7__length1 = 4;
	_tmp8_ = flag_utils_find_existing_path (_tmp7_, (gint) 4, G_FILE_TEST_IS_DIR);
	_tmp9_ = _tmp8_;
	_tmp7_ = (_vala_array_free (_tmp7_, _tmp7__length1, (GDestroyNotify) g_free), NULL);
	resolved_png = _tmp9_;
	_tmp10_ = resolved_png;
	if (g_strcmp0 (_tmp10_, "") != 0) {
		result = resolved_png;
		_g_free0 (installed_png);
		return result;
	}
	_tmp11_ = g_strdup (FLAG_UTILS_SHARED_FLAGS_SVG_DIR);
	installed_svg = _tmp11_;
	_tmp12_ = g_strdup (FLAG_UTILS_LEGACY_FLAGS_SVG_DIR);
	legacy_installed_svg = _tmp12_;
	_tmp13_ = g_strdup (FLAG_UTILS_SHARED_FLAGS_SVG_DIR);
	_tmp14_ = g_strdup (FLAG_UTILS_CONFIG_FLAGS_SVG_DIR);
	_tmp15_ = installed_svg;
	_tmp16_ = g_strdup (_tmp15_);
	_tmp17_ = legacy_installed_svg;
	_tmp18_ = g_strdup (_tmp17_);
	_tmp19_ = flag_utils_resolve_source_path ("flags", G_FILE_TEST_IS_DIR);
	_tmp20_ = g_new0 (gchar*, 5 + 1);
	_tmp20_[0] = _tmp13_;
	_tmp20_[1] = _tmp14_;
	_tmp20_[2] = _tmp16_;
	_tmp20_[3] = _tmp18_;
	_tmp20_[4] = _tmp19_;
	_tmp21_ = _tmp20_;
	_tmp21__length1 = 5;
	_tmp22_ = flag_utils_find_existing_path (_tmp21_, (gint) 5, G_FILE_TEST_IS_DIR);
	_tmp23_ = _tmp22_;
	_tmp21_ = (_vala_array_free (_tmp21_, _tmp21__length1, (GDestroyNotify) g_free), NULL);
	resolved_svg = _tmp23_;
	_tmp24_ = resolved_svg;
	if (g_strcmp0 (_tmp24_, "") != 0) {
		result = resolved_svg;
		_g_free0 (legacy_installed_svg);
		_g_free0 (installed_svg);
		_g_free0 (resolved_png);
		_g_free0 (installed_png);
		return result;
	}
	result = installed_svg;
	_g_free0 (resolved_svg);
	_g_free0 (legacy_installed_svg);
	_g_free0 (resolved_png);
	_g_free0 (installed_png);
	return result;
}

gchar*
flag_utils_build_svg_flag_dir_path (void)
{
	gchar* installed = NULL;
	gchar* _tmp0_;
	gchar* resolved = NULL;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	gchar** _tmp5_;
	gchar** _tmp6_;
	gint _tmp6__length1;
	gchar* _tmp7_;
	gchar* _tmp8_;
	const gchar* _tmp9_;
	gchar* result;
	_tmp0_ = g_strdup (FLAG_UTILS_SHARED_FLAGS_SVG_DIR);
	installed = _tmp0_;
	_tmp1_ = g_strdup (FLAG_UTILS_SHARED_FLAGS_SVG_DIR);
	_tmp2_ = g_strdup (FLAG_UTILS_CONFIG_FLAGS_SVG_DIR);
	_tmp3_ = g_strdup (FLAG_UTILS_LEGACY_FLAGS_SVG_DIR);
	_tmp4_ = flag_utils_resolve_source_path ("flags", G_FILE_TEST_IS_DIR);
	_tmp5_ = g_new0 (gchar*, 4 + 1);
	_tmp5_[0] = _tmp1_;
	_tmp5_[1] = _tmp2_;
	_tmp5_[2] = _tmp3_;
	_tmp5_[3] = _tmp4_;
	_tmp6_ = _tmp5_;
	_tmp6__length1 = 4;
	_tmp7_ = flag_utils_find_existing_path (_tmp6_, (gint) 4, G_FILE_TEST_IS_DIR);
	_tmp8_ = _tmp7_;
	_tmp6_ = (_vala_array_free (_tmp6_, _tmp6__length1, (GDestroyNotify) g_free), NULL);
	resolved = _tmp8_;
	_tmp9_ = resolved;
	if (g_strcmp0 (_tmp9_, "") != 0) {
		result = resolved;
		_g_free0 (installed);
		return result;
	}
	result = installed;
	_g_free0 (resolved);
	return result;
}

static gchar*
string_strip (const gchar* self)
{
	gchar* _result_ = NULL;
	gchar* _tmp0_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_strdup (self);
	_result_ = _tmp0_;
	g_strstrip (_result_);
	result = _result_;
	return result;
}

static gint
string_index_of (const gchar* self,
                 const gchar* needle,
                 gint start_index)
{
	gchar* _result_ = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gint result;
	g_return_val_if_fail (self != NULL, -1);
	g_return_val_if_fail (needle != NULL, -1);
	_tmp0_ = strstr (((gchar*) self) + start_index, (gchar*) needle);
	_result_ = _tmp0_;
	_tmp1_ = _result_;
	if (_tmp1_ != NULL) {
		gchar* _tmp2_;
		_tmp2_ = _result_;
		result = (gint) (_tmp2_ - ((gchar*) self));
		return result;
	} else {
		result = -1;
		return result;
	}
}

static glong
string_strnlen (gchar* str,
                glong maxlen)
{
	gchar* end = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	glong result;
	_tmp0_ = memchr (str, 0, (gsize) maxlen);
	end = _tmp0_;
	_tmp1_ = end;
	if (_tmp1_ == NULL) {
		result = maxlen;
		return result;
	} else {
		gchar* _tmp2_;
		_tmp2_ = end;
		result = (glong) (_tmp2_ - str);
		return result;
	}
}

static gchar*
string_substring (const gchar* self,
                  glong offset,
                  glong len)
{
	glong string_length = 0L;
	gboolean _tmp0_ = FALSE;
	gchar* _tmp3_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	if (offset >= ((glong) 0)) {
		_tmp0_ = len >= ((glong) 0);
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		string_length = string_strnlen ((gchar*) self, offset + len);
	} else {
		gint _tmp1_;
		gint _tmp2_;
		_tmp1_ = strlen (self);
		_tmp2_ = _tmp1_;
		string_length = (glong) _tmp2_;
	}
	if (offset < ((glong) 0)) {
		offset = string_length + offset;
		g_return_val_if_fail (offset >= ((glong) 0), NULL);
	} else {
		g_return_val_if_fail (offset <= string_length, NULL);
	}
	if (len < ((glong) 0)) {
		len = string_length - offset;
	}
	g_return_val_if_fail ((offset + len) <= string_length, NULL);
	_tmp3_ = g_strndup (((gchar*) self) + offset, (gsize) len);
	result = _tmp3_;
	return result;
}

gchar*
flag_utils_get_layout_base_code (const gchar* code)
{
	gchar* base_code = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gint variant_pos = 0;
	const gchar* _tmp4_;
	gint slash_pos = 0;
	const gchar* _tmp7_;
	const gchar* _tmp10_;
	gchar* _tmp11_;
	gchar* result;
	g_return_val_if_fail (code != NULL, NULL);
	_tmp0_ = g_utf8_strdown (code, (gssize) -1);
	_tmp1_ = _tmp0_;
	_tmp2_ = string_strip (_tmp1_);
	_tmp3_ = _tmp2_;
	_g_free0 (_tmp1_);
	base_code = _tmp3_;
	_tmp4_ = base_code;
	variant_pos = string_index_of (_tmp4_, "(", 0);
	if (variant_pos > 0) {
		const gchar* _tmp5_;
		gchar* _tmp6_;
		_tmp5_ = base_code;
		_tmp6_ = string_substring (_tmp5_, (glong) 0, (glong) variant_pos);
		_g_free0 (base_code);
		base_code = _tmp6_;
	}
	_tmp7_ = base_code;
	slash_pos = string_index_of (_tmp7_, "/", 0);
	if (slash_pos > 0) {
		const gchar* _tmp8_;
		gchar* _tmp9_;
		_tmp8_ = base_code;
		_tmp9_ = string_substring (_tmp8_, (glong) 0, (glong) slash_pos);
		_g_free0 (base_code);
		base_code = _tmp9_;
	}
	_tmp10_ = base_code;
	_tmp11_ = string_strip (_tmp10_);
	result = _tmp11_;
	_g_free0 (base_code);
	return result;
}

gchar*
flag_utils_get_layout_icon_code (const gchar* layout_code)
{
	gchar* base_code = NULL;
	gchar* _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp3_;
	const gchar* _tmp4_;
	GQuark _tmp6_ = 0U;
	static GQuark _tmp5_label0 = 0;
	static GQuark _tmp5_label1 = 0;
	static GQuark _tmp5_label2 = 0;
	static GQuark _tmp5_label3 = 0;
	static GQuark _tmp5_label4 = 0;
	static GQuark _tmp5_label5 = 0;
	static GQuark _tmp5_label6 = 0;
	static GQuark _tmp5_label7 = 0;
	static GQuark _tmp5_label8 = 0;
	static GQuark _tmp5_label9 = 0;
	static GQuark _tmp5_label10 = 0;
	static GQuark _tmp5_label11 = 0;
	static GQuark _tmp5_label12 = 0;
	static GQuark _tmp5_label13 = 0;
	static GQuark _tmp5_label14 = 0;
	static GQuark _tmp5_label15 = 0;
	static GQuark _tmp5_label16 = 0;
	static GQuark _tmp5_label17 = 0;
	static GQuark _tmp5_label18 = 0;
	static GQuark _tmp5_label19 = 0;
	static GQuark _tmp5_label20 = 0;
	static GQuark _tmp5_label21 = 0;
	static GQuark _tmp5_label22 = 0;
	static GQuark _tmp5_label23 = 0;
	static GQuark _tmp5_label24 = 0;
	static GQuark _tmp5_label25 = 0;
	static GQuark _tmp5_label26 = 0;
	static GQuark _tmp5_label27 = 0;
	static GQuark _tmp5_label28 = 0;
	static GQuark _tmp5_label29 = 0;
	static GQuark _tmp5_label30 = 0;
	static GQuark _tmp5_label31 = 0;
	static GQuark _tmp5_label32 = 0;
	static GQuark _tmp5_label33 = 0;
	static GQuark _tmp5_label34 = 0;
	static GQuark _tmp5_label35 = 0;
	static GQuark _tmp5_label36 = 0;
	static GQuark _tmp5_label37 = 0;
	static GQuark _tmp5_label38 = 0;
	static GQuark _tmp5_label39 = 0;
	gchar* result;
	g_return_val_if_fail (layout_code != NULL, NULL);
	_tmp0_ = flag_utils_get_layout_base_code (layout_code);
	base_code = _tmp0_;
	_tmp1_ = base_code;
	if (g_strcmp0 (_tmp1_, "") == 0) {
		gchar* _tmp2_;
		_tmp2_ = g_strdup ("us");
		result = _tmp2_;
		_g_free0 (base_code);
		return result;
	}
	_tmp3_ = base_code;
	_tmp4_ = _tmp3_;
	_tmp6_ = (NULL == _tmp4_) ? 0 : g_quark_from_string (_tmp4_);
	if (((_tmp6_ == ((0 != _tmp5_label0) ? _tmp5_label0 : (_tmp5_label0 = g_quark_from_static_string ("en")))) || (_tmp6_ == ((0 != _tmp5_label1) ? _tmp5_label1 : (_tmp5_label1 = g_quark_from_static_string ("english"))))) || (_tmp6_ == ((0 != _tmp5_label2) ? _tmp5_label2 : (_tmp5_label2 = g_quark_from_static_string ("american"))))) {
		switch (0) {
			default:
			{
				gchar* _tmp7_;
				_tmp7_ = g_strdup ("us");
				result = _tmp7_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label3) ? _tmp5_label3 : (_tmp5_label3 = g_quark_from_static_string ("russian")))) {
		switch (0) {
			default:
			{
				gchar* _tmp8_;
				_tmp8_ = g_strdup ("ru");
				result = _tmp8_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label4) ? _tmp5_label4 : (_tmp5_label4 = g_quark_from_static_string ("ukrainian")))) {
		switch (0) {
			default:
			{
				gchar* _tmp9_;
				_tmp9_ = g_strdup ("ua");
				result = _tmp9_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label5) ? _tmp5_label5 : (_tmp5_label5 = g_quark_from_static_string ("belarusian")))) {
		switch (0) {
			default:
			{
				gchar* _tmp10_;
				_tmp10_ = g_strdup ("by");
				result = _tmp10_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if ((_tmp6_ == ((0 != _tmp5_label6) ? _tmp5_label6 : (_tmp5_label6 = g_quark_from_static_string ("ara")))) || (_tmp6_ == ((0 != _tmp5_label7) ? _tmp5_label7 : (_tmp5_label7 = g_quark_from_static_string ("arabic"))))) {
		switch (0) {
			default:
			{
				gchar* _tmp11_;
				_tmp11_ = g_strdup ("ar");
				result = _tmp11_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label8) ? _tmp5_label8 : (_tmp5_label8 = g_quark_from_static_string ("armenian")))) {
		switch (0) {
			default:
			{
				gchar* _tmp12_;
				_tmp12_ = g_strdup ("am");
				result = _tmp12_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if ((_tmp6_ == ((0 != _tmp5_label9) ? _tmp5_label9 : (_tmp5_label9 = g_quark_from_static_string ("bengali")))) || (_tmp6_ == ((0 != _tmp5_label10) ? _tmp5_label10 : (_tmp5_label10 = g_quark_from_static_string ("bangla"))))) {
		switch (0) {
			default:
			{
				gchar* _tmp13_;
				_tmp13_ = g_strdup ("bd");
				result = _tmp13_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if ((_tmp6_ == ((0 != _tmp5_label11) ? _tmp5_label11 : (_tmp5_label11 = g_quark_from_static_string ("brazilian")))) || (_tmp6_ == ((0 != _tmp5_label12) ? _tmp5_label12 : (_tmp5_label12 = g_quark_from_static_string ("brazil"))))) {
		switch (0) {
			default:
			{
				gchar* _tmp14_;
				_tmp14_ = g_strdup ("br");
				result = _tmp14_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label13) ? _tmp5_label13 : (_tmp5_label13 = g_quark_from_static_string ("bulgarian")))) {
		switch (0) {
			default:
			{
				gchar* _tmp15_;
				_tmp15_ = g_strdup ("bg");
				result = _tmp15_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label14) ? _tmp5_label14 : (_tmp5_label14 = g_quark_from_static_string ("canadian")))) {
		switch (0) {
			default:
			{
				gchar* _tmp16_;
				_tmp16_ = g_strdup ("ca");
				result = _tmp16_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label15) ? _tmp5_label15 : (_tmp5_label15 = g_quark_from_static_string ("croatian")))) {
		switch (0) {
			default:
			{
				gchar* _tmp17_;
				_tmp17_ = g_strdup ("hr");
				result = _tmp17_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label16) ? _tmp5_label16 : (_tmp5_label16 = g_quark_from_static_string ("czech")))) {
		switch (0) {
			default:
			{
				gchar* _tmp18_;
				_tmp18_ = g_strdup ("cz");
				result = _tmp18_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label17) ? _tmp5_label17 : (_tmp5_label17 = g_quark_from_static_string ("danish")))) {
		switch (0) {
			default:
			{
				gchar* _tmp19_;
				_tmp19_ = g_strdup ("dk");
				result = _tmp19_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if ((_tmp6_ == ((0 != _tmp5_label18) ? _tmp5_label18 : (_tmp5_label18 = g_quark_from_static_string ("estonian")))) || (_tmp6_ == ((0 != _tmp5_label19) ? _tmp5_label19 : (_tmp5_label19 = g_quark_from_static_string ("ee"))))) {
		switch (0) {
			default:
			{
				gchar* _tmp20_;
				_tmp20_ = g_strdup ("et");
				result = _tmp20_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label20) ? _tmp5_label20 : (_tmp5_label20 = g_quark_from_static_string ("persian")))) {
		switch (0) {
			default:
			{
				gchar* _tmp21_;
				_tmp21_ = g_strdup ("ir");
				result = _tmp21_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label21) ? _tmp5_label21 : (_tmp5_label21 = g_quark_from_static_string ("greek")))) {
		switch (0) {
			default:
			{
				gchar* _tmp22_;
				_tmp22_ = g_strdup ("gr");
				result = _tmp22_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label22) ? _tmp5_label22 : (_tmp5_label22 = g_quark_from_static_string ("hebrew")))) {
		switch (0) {
			default:
			{
				gchar* _tmp23_;
				_tmp23_ = g_strdup ("il");
				result = _tmp23_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label23) ? _tmp5_label23 : (_tmp5_label23 = g_quark_from_static_string ("hindi")))) {
		switch (0) {
			default:
			{
				gchar* _tmp24_;
				_tmp24_ = g_strdup ("in");
				result = _tmp24_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label24) ? _tmp5_label24 : (_tmp5_label24 = g_quark_from_static_string ("japanese")))) {
		switch (0) {
			default:
			{
				gchar* _tmp25_;
				_tmp25_ = g_strdup ("jp");
				result = _tmp25_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label25) ? _tmp5_label25 : (_tmp5_label25 = g_quark_from_static_string ("korean")))) {
		switch (0) {
			default:
			{
				gchar* _tmp26_;
				_tmp26_ = g_strdup ("kr");
				result = _tmp26_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if ((_tmp6_ == ((0 != _tmp5_label26) ? _tmp5_label26 : (_tmp5_label26 = g_quark_from_static_string ("latam")))) || (_tmp6_ == ((0 != _tmp5_label27) ? _tmp5_label27 : (_tmp5_label27 = g_quark_from_static_string ("latinamerican"))))) {
		switch (0) {
			default:
			{
				gchar* _tmp27_;
				_tmp27_ = g_strdup ("es");
				result = _tmp27_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label28) ? _tmp5_label28 : (_tmp5_label28 = g_quark_from_static_string ("polish")))) {
		switch (0) {
			default:
			{
				gchar* _tmp28_;
				_tmp28_ = g_strdup ("pl");
				result = _tmp28_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label29) ? _tmp5_label29 : (_tmp5_label29 = g_quark_from_static_string ("romanian")))) {
		switch (0) {
			default:
			{
				gchar* _tmp29_;
				_tmp29_ = g_strdup ("ro");
				result = _tmp29_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label30) ? _tmp5_label30 : (_tmp5_label30 = g_quark_from_static_string ("serbian")))) {
		switch (0) {
			default:
			{
				gchar* _tmp30_;
				_tmp30_ = g_strdup ("rs");
				result = _tmp30_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label31) ? _tmp5_label31 : (_tmp5_label31 = g_quark_from_static_string ("slovak")))) {
		switch (0) {
			default:
			{
				gchar* _tmp31_;
				_tmp31_ = g_strdup ("sk");
				result = _tmp31_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label32) ? _tmp5_label32 : (_tmp5_label32 = g_quark_from_static_string ("slovenian")))) {
		switch (0) {
			default:
			{
				gchar* _tmp32_;
				_tmp32_ = g_strdup ("si");
				result = _tmp32_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label33) ? _tmp5_label33 : (_tmp5_label33 = g_quark_from_static_string ("swedish")))) {
		switch (0) {
			default:
			{
				gchar* _tmp33_;
				_tmp33_ = g_strdup ("se");
				result = _tmp33_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label34) ? _tmp5_label34 : (_tmp5_label34 = g_quark_from_static_string ("thai")))) {
		switch (0) {
			default:
			{
				gchar* _tmp34_;
				_tmp34_ = g_strdup ("th");
				result = _tmp34_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if (_tmp6_ == ((0 != _tmp5_label35) ? _tmp5_label35 : (_tmp5_label35 = g_quark_from_static_string ("turkish")))) {
		switch (0) {
			default:
			{
				gchar* _tmp35_;
				_tmp35_ = g_strdup ("tr");
				result = _tmp35_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else if ((((_tmp6_ == ((0 != _tmp5_label36) ? _tmp5_label36 : (_tmp5_label36 = g_quark_from_static_string ("jp")))) || (_tmp6_ == ((0 != _tmp5_label37) ? _tmp5_label37 : (_tmp5_label37 = g_quark_from_static_string ("nec-vndr"))))) || (_tmp6_ == ((0 != _tmp5_label38) ? _tmp5_label38 : (_tmp5_label38 = g_quark_from_static_string ("nec_vndr"))))) || (_tmp6_ == ((0 != _tmp5_label39) ? _tmp5_label39 : (_tmp5_label39 = g_quark_from_static_string ("nec_vndr/jp"))))) {
		switch (0) {
			default:
			{
				gchar* _tmp36_;
				_tmp36_ = g_strdup ("jp");
				result = _tmp36_;
				_g_free0 (base_code);
				return result;
			}
		}
	} else {
		switch (0) {
			default:
			{
				const gchar* _tmp37_;
				gchar* _tmp38_;
				_tmp37_ = base_code;
				_tmp38_ = flag_utils_sanitize_icon_token (_tmp37_);
				result = _tmp38_;
				_g_free0 (base_code);
				return result;
			}
		}
	}
}

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;
}

gchar*
flag_utils_sanitize_icon_token (const gchar* value)
{
	GString* builder = NULL;
	GString* _tmp1_;
	gboolean previous_dash = FALSE;
	gchar* _result_ = NULL;
	GString* _tmp21_;
	const gchar* _tmp22_;
	gchar* _tmp23_;
	gchar* result;
	g_return_val_if_fail (value != NULL, NULL);
	if (g_strcmp0 (value, "") == 0) {
		gchar* _tmp0_;
		_tmp0_ = g_strdup ("");
		result = _tmp0_;
		return result;
	}
	_tmp1_ = g_string_new ("");
	builder = _tmp1_;
	previous_dash = FALSE;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp2_ = FALSE;
			_tmp2_ = TRUE;
			while (TRUE) {
				gint _tmp4_;
				gint _tmp5_;
				gchar c = '\0';
				gboolean _tmp6_ = FALSE;
				gboolean _tmp7_ = FALSE;
				gboolean _tmp10_ = FALSE;
				gboolean _tmp11_ = FALSE;
				gboolean _tmp12_ = FALSE;
				gboolean _tmp13_ = FALSE;
				gboolean _tmp14_ = FALSE;
				gboolean _tmp15_ = FALSE;
				gboolean _tmp16_ = FALSE;
				gboolean separator = FALSE;
				gboolean _tmp17_ = FALSE;
				gboolean _tmp18_ = FALSE;
				if (!_tmp2_) {
					gint _tmp3_;
					_tmp3_ = i;
					i = _tmp3_ + 1;
				}
				_tmp2_ = FALSE;
				_tmp4_ = strlen (value);
				_tmp5_ = _tmp4_;
				if (!(i < _tmp5_)) {
					break;
				}
				c = string_get (value, (glong) i);
				if (c >= 'a') {
					_tmp7_ = c <= 'z';
				} else {
					_tmp7_ = FALSE;
				}
				if (_tmp7_) {
					_tmp6_ = TRUE;
				} else {
					gboolean _tmp8_ = FALSE;
					if (c >= '0') {
						_tmp8_ = c <= '9';
					} else {
						_tmp8_ = FALSE;
					}
					_tmp6_ = _tmp8_;
				}
				if (_tmp6_) {
					GString* _tmp9_;
					_tmp9_ = builder;
					g_string_append_c (_tmp9_, c);
					previous_dash = FALSE;
					continue;
				}
				if (c == '/') {
					_tmp16_ = TRUE;
				} else {
					_tmp16_ = c == ' ';
				}
				if (_tmp16_) {
					_tmp15_ = TRUE;
				} else {
					_tmp15_ = c == '(';
				}
				if (_tmp15_) {
					_tmp14_ = TRUE;
				} else {
					_tmp14_ = c == ')';
				}
				if (_tmp14_) {
					_tmp13_ = TRUE;
				} else {
					_tmp13_ = c == '-';
				}
				if (_tmp13_) {
					_tmp12_ = TRUE;
				} else {
					_tmp12_ = c == '_';
				}
				if (_tmp12_) {
					_tmp11_ = TRUE;
				} else {
					_tmp11_ = c == '.';
				}
				if (_tmp11_) {
					_tmp10_ = TRUE;
				} else {
					_tmp10_ = c == ',';
				}
				separator = _tmp10_;
				if (separator) {
					_tmp18_ = !previous_dash;
				} else {
					_tmp18_ = FALSE;
				}
				if (_tmp18_) {
					GString* _tmp19_;
					_tmp19_ = builder;
					_tmp17_ = _tmp19_->len > ((gssize) 0);
				} else {
					_tmp17_ = FALSE;
				}
				if (_tmp17_) {
					GString* _tmp20_;
					_tmp20_ = builder;
					g_string_append_c (_tmp20_, '-');
					previous_dash = TRUE;
				}
			}
		}
	}
	_tmp21_ = builder;
	_tmp22_ = _tmp21_->str;
	_tmp23_ = g_strdup (_tmp22_);
	_result_ = _tmp23_;
	while (TRUE) {
		const gchar* _tmp24_;
		const gchar* _tmp25_;
		const gchar* _tmp26_;
		gint _tmp27_;
		gint _tmp28_;
		gchar* _tmp29_;
		_tmp24_ = _result_;
		if (!g_str_has_suffix (_tmp24_, "-")) {
			break;
		}
		_tmp25_ = _result_;
		_tmp26_ = _result_;
		_tmp27_ = strlen (_tmp26_);
		_tmp28_ = _tmp27_;
		_tmp29_ = string_substring (_tmp25_, (glong) 0, (glong) (_tmp28_ - 1));
		_g_free0 (_result_);
		_result_ = _tmp29_;
	}
	result = _result_;
	_g_string_free0 (builder);
	return result;
}

gboolean
flag_utils_has_flag_asset (const gchar* layout_code)
{
	gchar* icon_code = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar** flag_dirs = NULL;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar** _tmp6_;
	gint flag_dirs_length1;
	gint _flag_dirs_size_;
	gchar** _tmp7_;
	gint _tmp7__length1;
	gboolean result;
	g_return_val_if_fail (layout_code != NULL, FALSE);
	if (g_strcmp0 (layout_code, "") == 0) {
		result = FALSE;
		return result;
	}
	_tmp0_ = g_utf8_strdown (layout_code, (gssize) -1);
	_tmp1_ = _tmp0_;
	_tmp2_ = flag_utils_sanitize_icon_token (_tmp1_);
	_tmp3_ = _tmp2_;
	_g_free0 (_tmp1_);
	icon_code = _tmp3_;
	_tmp4_ = flag_utils_build_flag_dir_path ();
	_tmp5_ = flag_utils_build_svg_flag_dir_path ();
	_tmp6_ = g_new0 (gchar*, 2 + 1);
	_tmp6_[0] = _tmp4_;
	_tmp6_[1] = _tmp5_;
	flag_dirs = _tmp6_;
	flag_dirs_length1 = 2;
	_flag_dirs_size_ = flag_dirs_length1;
	_tmp7_ = flag_dirs;
	_tmp7__length1 = flag_dirs_length1;
	{
		gchar** dir_collection = NULL;
		gint dir_collection_length1 = 0;
		gint _dir_collection_size_ = 0;
		gint dir_it = 0;
		dir_collection = _tmp7_;
		dir_collection_length1 = _tmp7__length1;
		for (dir_it = 0; dir_it < dir_collection_length1; dir_it = dir_it + 1) {
			gchar* _tmp8_;
			gchar* dir = NULL;
			_tmp8_ = g_strdup (dir_collection[dir_it]);
			dir = _tmp8_;
			{
				const gchar* _tmp9_;
				gchar* png_path = NULL;
				const gchar* _tmp10_;
				const gchar* _tmp11_;
				gchar* _tmp12_;
				gchar* _tmp13_;
				gchar* _tmp14_;
				gchar* _tmp15_;
				const gchar* _tmp16_;
				gchar* svg_path = NULL;
				const gchar* _tmp17_;
				const gchar* _tmp18_;
				gchar* _tmp19_;
				gchar* _tmp20_;
				gchar* _tmp21_;
				gchar* _tmp22_;
				const gchar* _tmp23_;
				_tmp9_ = dir;
				if (g_strcmp0 (_tmp9_, "") == 0) {
					_g_free0 (dir);
					continue;
				}
				_tmp10_ = dir;
				_tmp11_ = icon_code;
				_tmp12_ = g_strconcat (_tmp11_, ".png", NULL);
				_tmp13_ = _tmp12_;
				_tmp14_ = g_build_filename (_tmp10_, _tmp13_, NULL);
				_tmp15_ = _tmp14_;
				_g_free0 (_tmp13_);
				png_path = _tmp15_;
				_tmp16_ = png_path;
				if (g_file_test (_tmp16_, G_FILE_TEST_EXISTS)) {
					result = TRUE;
					_g_free0 (png_path);
					_g_free0 (dir);
					flag_dirs = (_vala_array_free (flag_dirs, flag_dirs_length1, (GDestroyNotify) g_free), NULL);
					_g_free0 (icon_code);
					return result;
				}
				_tmp17_ = dir;
				_tmp18_ = icon_code;
				_tmp19_ = g_strconcat (_tmp18_, ".svg", NULL);
				_tmp20_ = _tmp19_;
				_tmp21_ = g_build_filename (_tmp17_, _tmp20_, NULL);
				_tmp22_ = _tmp21_;
				_g_free0 (_tmp20_);
				svg_path = _tmp22_;
				_tmp23_ = svg_path;
				if (g_file_test (_tmp23_, G_FILE_TEST_EXISTS)) {
					result = TRUE;
					_g_free0 (svg_path);
					_g_free0 (png_path);
					_g_free0 (dir);
					flag_dirs = (_vala_array_free (flag_dirs, flag_dirs_length1, (GDestroyNotify) g_free), NULL);
					_g_free0 (icon_code);
					return result;
				}
				_g_free0 (svg_path);
				_g_free0 (png_path);
				_g_free0 (dir);
			}
		}
	}
	result = FALSE;
	flag_dirs = (_vala_array_free (flag_dirs, flag_dirs_length1, (GDestroyNotify) g_free), NULL);
	_g_free0 (icon_code);
	return result;
}

FlagUtils*
flag_utils_construct (GType object_type)
{
	FlagUtils * self = NULL;
	self = (FlagUtils*) g_object_new (object_type, NULL);
	return self;
}

FlagUtils*
flag_utils_new (void)
{
	return flag_utils_construct (TYPE_FLAG_UTILS);
}

static void
flag_utils_class_init (FlagUtilsClass * klass,
                       gpointer klass_data)
{
	flag_utils_parent_class = g_type_class_peek_parent (klass);
}

static void
flag_utils_instance_init (FlagUtils * self,
                          gpointer klass)
{
}

static GType
flag_utils_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (FlagUtilsClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) flag_utils_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (FlagUtils), 0, (GInstanceInitFunc) flag_utils_instance_init, NULL };
	GType flag_utils_type_id;
	flag_utils_type_id = g_type_register_static (G_TYPE_OBJECT, "FlagUtils", &g_define_type_info, 0);
	return flag_utils_type_id;
}

GType
flag_utils_get_type (void)
{
	static volatile gsize flag_utils_type_id__once = 0;
	if (g_once_init_enter (&flag_utils_type_id__once)) {
		GType flag_utils_type_id;
		flag_utils_type_id = flag_utils_get_type_once ();
		g_once_init_leave (&flag_utils_type_id__once, flag_utils_type_id);
	}
	return flag_utils_type_id__once;
}

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);
}

