/* contacts-io.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from contacts-io.vala, do not modify */

/*
 * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "contacts.h"
#include "contactscore.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

#define _g_variant_type_free0(var) ((var == NULL) ? NULL : (var = (g_variant_type_free (var), NULL)))
#define _g_variant_unref0(var) ((var == NULL) ? NULL : (var = (g_variant_unref (var), NULL)))
#define _g_variant_builder_unref0(var) ((var == NULL) ? NULL : (var = (g_variant_builder_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_variant_iter_free0(var) ((var == NULL) ? NULL : (var = (g_variant_iter_free (var), NULL)))
#define _g_ptr_array_unref0(var) ((var == NULL) ? NULL : (var = (g_ptr_array_unref (var), NULL)))
#define _vala_assert(expr, msg) if G_LIKELY (expr) ; else g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);
#define _vala_return_if_fail(expr, msg) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return; }
#define _vala_return_val_if_fail(expr, msg, val) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return val; }
#define _vala_warn_if_fail(expr, msg) if G_LIKELY (expr) ; else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);

static void _g_object_unref0_ (gpointer var);

/**
   * Serializes a list of {@link Contact}s as returned by a
   * {@link Contacts.Io.Parser} into a {@link GLib.Variant} so it can be sent
   * from one process to another.
   */
GVariant*
contacts_io_serialize_to_gvariant (ContactsContact** contacts,
                                   gint contacts_length1)
{
	GVariantBuilder* builder = NULL;
	GVariantType* _tmp0_;
	GVariantType* _tmp1_;
	GVariantBuilder* _tmp2_;
	GVariantBuilder* _tmp3_;
	GVariantBuilder* _tmp9_;
	GVariant* _tmp10_;
	GVariant* result;
	_tmp0_ = g_variant_type_new ("aa{sv}");
	_tmp1_ = _tmp0_;
	_tmp2_ = g_variant_builder_new (_tmp1_);
	_tmp3_ = _tmp2_;
	_g_variant_type_free0 (_tmp1_);
	builder = _tmp3_;
	{
		ContactsContact** contact_collection = NULL;
		gint contact_collection_length1 = 0;
		gint _contact_collection_size_ = 0;
		gint contact_it = 0;
		contact_collection = contacts;
		contact_collection_length1 = contacts_length1;
		for (contact_it = 0; contact_it < contact_collection_length1; contact_it = contact_it + 1) {
			ContactsContact* contact = NULL;
			contact = contact_collection[contact_it];
			{
				GVariant* variant = NULL;
				ContactsContact* _tmp4_;
				GVariant* _tmp5_;
				GVariant* _tmp6_;
				GVariantBuilder* _tmp7_;
				GVariant* _tmp8_;
				_tmp4_ = contact;
				_tmp5_ = contacts_contact_to_gvariant (_tmp4_);
				variant = _tmp5_;
				_tmp6_ = variant;
				if (g_variant_n_children (_tmp6_) == ((gsize) 0)) {
					_g_variant_unref0 (variant);
					continue;
				}
				_tmp7_ = builder;
				_tmp8_ = variant;
				g_variant_builder_add_value (_tmp7_, _tmp8_);
				_g_variant_unref0 (variant);
			}
		}
	}
	_tmp9_ = builder;
	_tmp10_ = g_variant_builder_end (_tmp9_);
	g_variant_ref_sink (_tmp10_);
	result = _tmp10_;
	_g_variant_builder_unref0 (builder);
	return result;
}

/**
   * Deserializes the {@link GLib.Variant} back into a list of
   * {@link Contacts.Contact}s.
   */
static void
_g_object_unref0_ (gpointer var)
{
	(var == NULL) ? NULL : (var = (g_object_unref (var), NULL));
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

ContactsContact**
contacts_io_deserialize_gvariant (GVariant* variant,
                                  gint* result_length1)
{
	const GVariantType* _tmp0_;
	GVariantType* _tmp1_;
	GVariantType* _tmp2_;
	GPtrArray* _result_ = NULL;
	GPtrArray* _tmp3_;
	GPtrArray* _tmp4_;
	GVariantIter* iter = NULL;
	GVariantIter* _tmp5_;
	GVariant* element = NULL;
	GPtrArray* _tmp14_;
	gsize _tmp15_ = 0;
	gpointer* _tmp16_;
	ContactsContact** _tmp17_;
	gint _tmp17__length1;
	ContactsContact** result;
	g_return_val_if_fail (variant != NULL, NULL);
	_tmp0_ = g_variant_get_type (variant);
	_tmp1_ = g_variant_type_new ("aa{sv}");
	_tmp2_ = _tmp1_;
	_vala_return_val_if_fail (g_variant_type_equal (_tmp0_, _tmp2_), "variant.get_type ().equal (new VariantType (\"aa{sv}\"))", NULL);
	_tmp3_ = g_ptr_array_new_full ((guint) 0, _g_object_unref0_);
	_tmp4_ = _tmp3_;
	_g_variant_type_free0 (_tmp2_);
	_result_ = _tmp4_;
	_tmp5_ = g_variant_iter_new (variant);
	iter = _tmp5_;
	while (TRUE) {
		GVariantIter* _tmp6_;
		GVariant* _tmp7_ = NULL;
		gboolean _tmp8_;
		ContactsContact* contact = NULL;
		GVariant* _tmp9_;
		ContactsContact* _tmp10_;
		GPtrArray* _tmp11_;
		ContactsContact* _tmp12_;
		ContactsContact* _tmp13_;
		_tmp6_ = iter;
		_tmp8_ = g_variant_iter_next (_tmp6_, "@a{sv}", &_tmp7_, NULL);
		_g_variant_unref0 (element);
		element = _tmp7_;
		if (!_tmp8_) {
			break;
		}
		_tmp9_ = element;
		_tmp10_ = contacts_contact_new_for_gvariant (_tmp9_);
		contact = _tmp10_;
		_tmp11_ = _result_;
		_tmp12_ = contact;
		_tmp13_ = _g_object_ref0 (_tmp12_);
		g_ptr_array_add (_tmp11_, _tmp13_);
		_g_object_unref0 (contact);
	}
	_tmp14_ = _result_;
	_tmp16_ = g_ptr_array_steal (_tmp14_, &_tmp15_);
	_tmp17_ = _tmp16_;
	_tmp17__length1 = _tmp15_;
	if (result_length1) {
		*result_length1 = _tmp17__length1;
	}
	result = _tmp17_;
	_g_variant_unref0 (element);
	_g_variant_iter_free0 (iter);
	_g_ptr_array_unref0 (_result_);
	return result;
}

