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

#include "libdino.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gee.h>
#include <glib-object.h>
#include "xmpp-vala.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_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _xmpp_xep_fallback_indication_fallback_location_unref0(var) ((var == NULL) ? NULL : (var = (xmpp_xep_fallback_indication_fallback_location_unref (var), NULL)))
#define _xmpp_xep_fallback_indication_fallback_unref0(var) ((var == NULL) ? NULL : (var = (xmpp_xep_fallback_indication_fallback_unref (var), NULL)))

static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

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

void
dino_send_message (DinoEntitiesConversation* conversation,
                   const gchar* text,
                   gint reply_to_id,
                   DinoEntitiesMessage* correction_to,
                   GeeList* markups)
{
	DinoStreamInteractor* stream_interactor = NULL;
	DinoApplication* _tmp0_;
	DinoStreamInteractor* _tmp1_;
	DinoStreamInteractor* _tmp2_;
	DinoStreamInteractor* _tmp3_;
	DinoEntitiesMessage* out_message = NULL;
	DinoStreamInteractor* _tmp4_;
	DinoModuleIdentity* _tmp5_;
	gpointer _tmp6_;
	DinoMessageProcessor* _tmp7_;
	DinoEntitiesMessage* _tmp8_;
	DinoEntitiesMessage* _tmp9_;
	gboolean _tmp75_;
	gboolean _tmp76_;
	DinoStreamInteractor* _tmp93_;
	DinoModuleIdentity* _tmp94_;
	gpointer _tmp95_;
	DinoContentItemStore* _tmp96_;
	DinoEntitiesMessage* _tmp97_;
	DinoStreamInteractor* _tmp98_;
	DinoModuleIdentity* _tmp99_;
	gpointer _tmp100_;
	DinoMessageProcessor* _tmp101_;
	DinoEntitiesMessage* _tmp102_;
	DinoStreamInteractor* _tmp103_;
	DinoModuleIdentity* _tmp104_;
	gpointer _tmp105_;
	DinoMessageProcessor* _tmp106_;
	DinoEntitiesMessage* _tmp107_;
	g_return_if_fail (conversation != NULL);
	g_return_if_fail (text != NULL);
	g_return_if_fail (markups != NULL);
	_tmp0_ = dino_application_get_default ();
	_tmp1_ = dino_application_get_stream_interactor (_tmp0_);
	_tmp2_ = _tmp1_;
	_tmp3_ = _g_object_ref0 (_tmp2_);
	stream_interactor = _tmp3_;
	_tmp4_ = stream_interactor;
	_tmp5_ = dino_message_processor_IDENTITY;
	_tmp6_ = dino_stream_interactor_get_module (_tmp4_, DINO_TYPE_MESSAGE_PROCESSOR, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, _tmp5_);
	_tmp7_ = (DinoMessageProcessor*) _tmp6_;
	_tmp8_ = dino_message_processor_create_out_message (_tmp7_, text, conversation);
	_tmp9_ = _tmp8_;
	_g_object_unref0 (_tmp7_);
	out_message = _tmp9_;
	if (correction_to != NULL) {
		const gchar* _tmp10_ = NULL;
		const gchar* _tmp11_;
		gchar* correction_to_stanza_id = NULL;
		gchar* _tmp14_;
		DinoEntitiesMessage* _tmp15_;
		const gchar* _tmp16_;
		gchar* _tmp17_;
		DinoStreamInteractor* _tmp18_;
		DinoModuleIdentity* _tmp19_;
		gpointer _tmp20_;
		DinoMessageCorrection* _tmp21_;
		DinoEntitiesMessage* _tmp22_;
		_tmp11_ = correction_to->edit_to;
		_tmp10_ = _tmp11_;
		if (_tmp10_ == NULL) {
			const gchar* _tmp12_;
			const gchar* _tmp13_;
			_tmp12_ = dino_entities_message_get_stanza_id (correction_to);
			_tmp13_ = _tmp12_;
			_tmp10_ = _tmp13_;
		}
		_tmp14_ = g_strdup (_tmp10_);
		correction_to_stanza_id = _tmp14_;
		_tmp15_ = out_message;
		_tmp16_ = correction_to_stanza_id;
		_tmp17_ = g_strdup (_tmp16_);
		_g_free0 (_tmp15_->edit_to);
		_tmp15_->edit_to = _tmp17_;
		_tmp18_ = stream_interactor;
		_tmp19_ = dino_message_correction_IDENTITY;
		_tmp20_ = dino_stream_interactor_get_module (_tmp18_, DINO_TYPE_MESSAGE_CORRECTION, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, _tmp19_);
		_tmp21_ = (DinoMessageCorrection*) _tmp20_;
		_tmp22_ = out_message;
		dino_message_correction_set_correction (_tmp21_, conversation, _tmp22_, correction_to);
		_g_object_unref0 (_tmp21_);
		_g_free0 (correction_to_stanza_id);
	}
	if (reply_to_id != 0) {
		DinoContentItem* reply_to = NULL;
		DinoStreamInteractor* _tmp23_;
		DinoModuleIdentity* _tmp24_;
		gpointer _tmp25_;
		DinoContentItemStore* _tmp26_;
		DinoContentItem* _tmp27_;
		DinoContentItem* _tmp28_;
		DinoEntitiesMessage* _tmp29_;
		DinoContentItem* _tmp30_;
		gint _tmp31_;
		gint _tmp32_;
		gchar* fallback = NULL;
		DinoContentItem* _tmp33_;
		gchar* _tmp34_;
		DinoEntitiesMessage* _tmp35_;
		const gchar* _tmp36_;
		DinoEntitiesMessage* _tmp37_;
		const gchar* _tmp38_;
		const gchar* _tmp39_;
		gchar* _tmp40_;
		gchar* _tmp41_;
		GeeArrayList* fallback_locations = NULL;
		GeeArrayList* _tmp42_;
		GeeArrayList* _tmp43_;
		const gchar* _tmp44_;
		XmppXepFallbackIndicationFallbackLocation* _tmp45_;
		XmppXepFallbackIndicationFallbackLocation* _tmp46_;
		GeeArrayList* fallback_list = NULL;
		GeeArrayList* _tmp47_;
		GeeArrayList* _tmp48_;
		GeeArrayList* _tmp49_;
		XmppXepFallbackIndicationFallback* _tmp50_;
		XmppXepFallbackIndicationFallback* _tmp51_;
		DinoEntitiesMessage* _tmp52_;
		GeeArrayList* _tmp53_;
		_tmp23_ = stream_interactor;
		_tmp24_ = dino_content_item_store_IDENTITY;
		_tmp25_ = dino_stream_interactor_get_module (_tmp23_, DINO_TYPE_CONTENT_ITEM_STORE, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, _tmp24_);
		_tmp26_ = (DinoContentItemStore*) _tmp25_;
		_tmp27_ = dino_content_item_store_get_item_by_id (_tmp26_, conversation, reply_to_id);
		_tmp28_ = _tmp27_;
		_g_object_unref0 (_tmp26_);
		reply_to = _tmp28_;
		_tmp29_ = out_message;
		_tmp30_ = reply_to;
		_tmp31_ = dino_content_item_get_id (_tmp30_);
		_tmp32_ = _tmp31_;
		dino_entities_message_set_quoted_item (_tmp29_, _tmp32_);
		_tmp33_ = reply_to;
		_tmp34_ = dino_fallback_body_get_quoted_fallback_body (_tmp33_);
		fallback = _tmp34_;
		_tmp35_ = out_message;
		_tmp36_ = fallback;
		_tmp37_ = out_message;
		_tmp38_ = dino_entities_message_get_body (_tmp37_);
		_tmp39_ = _tmp38_;
		_tmp40_ = g_strconcat (_tmp36_, _tmp39_, NULL);
		_tmp41_ = _tmp40_;
		dino_entities_message_set_body (_tmp35_, _tmp41_);
		_g_free0 (_tmp41_);
		_tmp42_ = gee_array_list_new (XMPP_XEP_FALLBACK_INDICATION_TYPE_FALLBACK_LOCATION, (GBoxedCopyFunc) xmpp_xep_fallback_indication_fallback_location_ref, (GDestroyNotify) xmpp_xep_fallback_indication_fallback_location_unref, NULL, NULL, NULL);
		fallback_locations = _tmp42_;
		_tmp43_ = fallback_locations;
		_tmp44_ = fallback;
		_tmp45_ = xmpp_xep_fallback_indication_fallback_location_new_partial_body (0, (gint) g_utf8_strlen (_tmp44_, (gssize) -1));
		_tmp46_ = _tmp45_;
		gee_abstract_collection_add ((GeeAbstractCollection*) _tmp43_, _tmp46_);
		_xmpp_xep_fallback_indication_fallback_location_unref0 (_tmp46_);
		_tmp47_ = gee_array_list_new (XMPP_XEP_FALLBACK_INDICATION_TYPE_FALLBACK, (GBoxedCopyFunc) xmpp_xep_fallback_indication_fallback_ref, (GDestroyNotify) xmpp_xep_fallback_indication_fallback_unref, NULL, NULL, NULL);
		fallback_list = _tmp47_;
		_tmp48_ = fallback_list;
		_tmp49_ = fallback_locations;
		_tmp50_ = xmpp_xep_fallback_indication_fallback_new (XMPP_XEP_REPLIES_NS_URI, (GeeList*) _tmp49_);
		_tmp51_ = _tmp50_;
		gee_abstract_collection_add ((GeeAbstractCollection*) _tmp48_, _tmp51_);
		_xmpp_xep_fallback_indication_fallback_unref0 (_tmp51_);
		_tmp52_ = out_message;
		_tmp53_ = fallback_list;
		dino_entities_message_set_fallbacks (_tmp52_, (GeeList*) _tmp53_);
		{
			GeeList* _span_list = NULL;
			gint _span_size = 0;
			GeeList* _tmp54_;
			gint _tmp55_;
			gint _tmp56_;
			gint _span_index = 0;
			_span_list = markups;
			_tmp54_ = _span_list;
			_tmp55_ = gee_collection_get_size ((GeeCollection*) _tmp54_);
			_tmp56_ = _tmp55_;
			_span_size = _tmp56_;
			_span_index = -1;
			while (TRUE) {
				gint _tmp57_;
				gint _tmp58_;
				XmppXepMessageMarkupSpan* span = NULL;
				GeeList* _tmp59_;
				gpointer _tmp60_;
				XmppXepMessageMarkupSpan* _tmp61_;
				XmppXepMessageMarkupSpan* _tmp62_;
				gint _tmp63_;
				gint _tmp64_;
				const gchar* _tmp65_;
				gint _tmp66_;
				gint _tmp67_;
				XmppXepMessageMarkupSpan* _tmp68_;
				XmppXepMessageMarkupSpan* _tmp69_;
				gint _tmp70_;
				gint _tmp71_;
				const gchar* _tmp72_;
				gint _tmp73_;
				gint _tmp74_;
				_span_index = _span_index + 1;
				_tmp57_ = _span_index;
				_tmp58_ = _span_size;
				if (!(_tmp57_ < _tmp58_)) {
					break;
				}
				_tmp59_ = _span_list;
				_tmp60_ = gee_list_get (_tmp59_, _span_index);
				span = (XmppXepMessageMarkupSpan*) _tmp60_;
				_tmp61_ = span;
				_tmp62_ = span;
				_tmp63_ = xmpp_xep_message_markup_span_get_start_char (_tmp62_);
				_tmp64_ = _tmp63_;
				_tmp65_ = fallback;
				_tmp66_ = strlen (_tmp65_);
				_tmp67_ = _tmp66_;
				xmpp_xep_message_markup_span_set_start_char (_tmp62_, _tmp64_ + _tmp67_);
				_tmp68_ = span;
				_tmp69_ = span;
				_tmp70_ = xmpp_xep_message_markup_span_get_end_char (_tmp69_);
				_tmp71_ = _tmp70_;
				_tmp72_ = fallback;
				_tmp73_ = strlen (_tmp72_);
				_tmp74_ = _tmp73_;
				xmpp_xep_message_markup_span_set_end_char (_tmp69_, _tmp71_ + _tmp74_);
				_g_object_unref0 (span);
			}
		}
		_g_object_unref0 (fallback_list);
		_g_object_unref0 (fallback_locations);
		_g_free0 (fallback);
		_g_object_unref0 (reply_to);
	}
	_tmp75_ = gee_collection_get_is_empty ((GeeCollection*) markups);
	_tmp76_ = _tmp75_;
	if (!_tmp76_) {
		DinoEntitiesMessage* _tmp77_;
		DinoEntitiesMessage* _tmp78_;
		gint _tmp79_;
		gint _tmp80_;
		_tmp77_ = out_message;
		_tmp78_ = out_message;
		_tmp79_ = dino_entities_message_get_id (_tmp78_);
		_tmp80_ = _tmp79_;
		dino_entities_message_persist_markups (_tmp77_, markups, _tmp80_);
	}
	if (correction_to != NULL) {
		DinoStreamInteractor* _tmp81_;
		DinoModuleIdentity* _tmp82_;
		gpointer _tmp83_;
		DinoMessageCorrection* _tmp84_;
		DinoEntitiesMessage* _tmp85_;
		gint _tmp86_;
		gint _tmp87_;
		DinoStreamInteractor* _tmp88_;
		DinoModuleIdentity* _tmp89_;
		gpointer _tmp90_;
		DinoMessageProcessor* _tmp91_;
		DinoEntitiesMessage* _tmp92_;
		_tmp81_ = stream_interactor;
		_tmp82_ = dino_message_correction_IDENTITY;
		_tmp83_ = dino_stream_interactor_get_module (_tmp81_, DINO_TYPE_MESSAGE_CORRECTION, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, _tmp82_);
		_tmp84_ = (DinoMessageCorrection*) _tmp83_;
		_tmp85_ = out_message;
		_tmp86_ = dino_entities_message_get_id (_tmp85_);
		_tmp87_ = _tmp86_;
		dino_message_correction_on_received_correction (_tmp84_, conversation, _tmp87_);
		_g_object_unref0 (_tmp84_);
		_tmp88_ = stream_interactor;
		_tmp89_ = dino_message_processor_IDENTITY;
		_tmp90_ = dino_stream_interactor_get_module (_tmp88_, DINO_TYPE_MESSAGE_PROCESSOR, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, _tmp89_);
		_tmp91_ = (DinoMessageProcessor*) _tmp90_;
		_tmp92_ = out_message;
		dino_message_processor_send_xmpp_message (_tmp91_, _tmp92_, conversation, FALSE);
		_g_object_unref0 (_tmp91_);
		_g_object_unref0 (out_message);
		_g_object_unref0 (stream_interactor);
		return;
	}
	_tmp93_ = stream_interactor;
	_tmp94_ = dino_content_item_store_IDENTITY;
	_tmp95_ = dino_stream_interactor_get_module (_tmp93_, DINO_TYPE_CONTENT_ITEM_STORE, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, _tmp94_);
	_tmp96_ = (DinoContentItemStore*) _tmp95_;
	_tmp97_ = out_message;
	dino_content_item_store_insert_message (_tmp96_, _tmp97_, conversation, FALSE);
	_g_object_unref0 (_tmp96_);
	_tmp98_ = stream_interactor;
	_tmp99_ = dino_message_processor_IDENTITY;
	_tmp100_ = dino_stream_interactor_get_module (_tmp98_, DINO_TYPE_MESSAGE_PROCESSOR, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, _tmp99_);
	_tmp101_ = (DinoMessageProcessor*) _tmp100_;
	_tmp102_ = out_message;
	dino_message_processor_send_xmpp_message (_tmp101_, _tmp102_, conversation, FALSE);
	_g_object_unref0 (_tmp101_);
	_tmp103_ = stream_interactor;
	_tmp104_ = dino_message_processor_IDENTITY;
	_tmp105_ = dino_stream_interactor_get_module (_tmp103_, DINO_TYPE_MESSAGE_PROCESSOR, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, _tmp104_);
	_tmp106_ = (DinoMessageProcessor*) _tmp105_;
	_tmp107_ = out_message;
	g_signal_emit_by_name (_tmp106_, "message-sent", _tmp107_, conversation);
	_g_object_unref0 (_tmp106_);
	_g_object_unref0 (out_message);
	_g_object_unref0 (stream_interactor);
}

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

