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

/*
* Copyright (c) 2018-2026 (https://github.com/phase1geo/Minder)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authored by: Trevor Williams <phase1geo@gmail.com>
*/

#include <glib-object.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <glib/gi18n-lib.h>
#include <float.h>
#include <math.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
#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_UNDO_ITEM (undo_item_get_type ())
#define UNDO_ITEM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_UNDO_ITEM, UndoItem))
#define UNDO_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_UNDO_ITEM, UndoItemClass))
#define IS_UNDO_ITEM(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_UNDO_ITEM))
#define IS_UNDO_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_UNDO_ITEM))
#define UNDO_ITEM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_UNDO_ITEM, UndoItemClass))

typedef struct _UndoItem UndoItem;
typedef struct _UndoItemClass UndoItemClass;
typedef struct _UndoItemPrivate UndoItemPrivate;

#define TYPE_MIND_MAP (mind_map_get_type ())
#define MIND_MAP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MIND_MAP, MindMap))
#define MIND_MAP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MIND_MAP, MindMapClass))
#define IS_MIND_MAP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MIND_MAP))
#define IS_MIND_MAP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MIND_MAP))
#define MIND_MAP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MIND_MAP, MindMapClass))

typedef struct _MindMap MindMap;
typedef struct _MindMapClass MindMapClass;

#define TYPE_UNDO_GROUPS_MERGE (undo_groups_merge_get_type ())
#define UNDO_GROUPS_MERGE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_UNDO_GROUPS_MERGE, UndoGroupsMerge))
#define UNDO_GROUPS_MERGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_UNDO_GROUPS_MERGE, UndoGroupsMergeClass))
#define IS_UNDO_GROUPS_MERGE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_UNDO_GROUPS_MERGE))
#define IS_UNDO_GROUPS_MERGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_UNDO_GROUPS_MERGE))
#define UNDO_GROUPS_MERGE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_UNDO_GROUPS_MERGE, UndoGroupsMergeClass))

typedef struct _UndoGroupsMerge UndoGroupsMerge;
typedef struct _UndoGroupsMergeClass UndoGroupsMergeClass;
typedef struct _UndoGroupsMergePrivate UndoGroupsMergePrivate;

#define TYPE_NODE_GROUP (node_group_get_type ())
#define NODE_GROUP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_NODE_GROUP, NodeGroup))
#define NODE_GROUP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_NODE_GROUP, NodeGroupClass))
#define IS_NODE_GROUP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_NODE_GROUP))
#define IS_NODE_GROUP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_NODE_GROUP))
#define NODE_GROUP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_NODE_GROUP, NodeGroupClass))

typedef struct _NodeGroup NodeGroup;
typedef struct _NodeGroupClass NodeGroupClass;
enum  {
	UNDO_GROUPS_MERGE_0_PROPERTY,
	UNDO_GROUPS_MERGE_NUM_PROPERTIES
};
static GParamSpec* undo_groups_merge_properties[UNDO_GROUPS_MERGE_NUM_PROPERTIES];
#define _g_array_unref0(var) ((var == NULL) ? NULL : (var = (g_array_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

#define TYPE_SELECTION (selection_get_type ())
#define SELECTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_SELECTION, Selection))
#define SELECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_SELECTION, SelectionClass))
#define IS_SELECTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_SELECTION))
#define IS_SELECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_SELECTION))
#define SELECTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_SELECTION, SelectionClass))

typedef struct _Selection Selection;
typedef struct _SelectionClass SelectionClass;

#define TYPE_NODE_GROUPS (node_groups_get_type ())
#define NODE_GROUPS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_NODE_GROUPS, NodeGroups))
#define NODE_GROUPS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_NODE_GROUPS, NodeGroupsClass))
#define IS_NODE_GROUPS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_NODE_GROUPS))
#define IS_NODE_GROUPS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_NODE_GROUPS))
#define NODE_GROUPS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_NODE_GROUPS, NodeGroupsClass))

typedef struct _NodeGroups NodeGroups;
typedef struct _NodeGroupsClass NodeGroupsClass;

struct _UndoItem {
	GObject parent_instance;
	UndoItemPrivate * priv;
};

struct _UndoItemClass {
	GObjectClass parent_class;
	void (*undo) (UndoItem* self, MindMap* map);
	void (*redo) (UndoItem* self, MindMap* map);
	gboolean (*matches) (UndoItem* self, UndoItem* item);
	void (*replace_with_item) (UndoItem* self, UndoItem* item);
	gchar* (*to_string) (UndoItem* self);
};

struct _UndoGroupsMerge {
	UndoItem parent_instance;
	UndoGroupsMergePrivate * priv;
};

struct _UndoGroupsMergeClass {
	UndoItemClass parent_class;
};

struct _UndoGroupsMergePrivate {
	GArray* _groups;
	NodeGroup* _merged;
};

static gint UndoGroupsMerge_private_offset;
static gpointer undo_groups_merge_parent_class = NULL;

VALA_EXTERN GType undo_item_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (UndoItem, g_object_unref)
VALA_EXTERN gpointer mind_map_ref (gpointer instance);
VALA_EXTERN void mind_map_unref (gpointer instance);
VALA_EXTERN GParamSpec* param_spec_mind_map (const gchar* name,
                                 const gchar* nick,
                                 const gchar* blurb,
                                 GType object_type,
                                 GParamFlags flags);
VALA_EXTERN void value_set_mind_map (GValue* value,
                         gpointer v_object);
VALA_EXTERN void value_take_mind_map (GValue* value,
                          gpointer v_object);
VALA_EXTERN gpointer value_get_mind_map (const GValue* value);
VALA_EXTERN GType mind_map_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (MindMap, mind_map_unref)
VALA_EXTERN GType undo_groups_merge_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (UndoGroupsMerge, g_object_unref)
VALA_EXTERN GType node_group_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (NodeGroup, g_object_unref)
VALA_EXTERN void undo_item_undo (UndoItem* self,
                     MindMap* map);
VALA_EXTERN void undo_item_redo (UndoItem* self,
                     MindMap* map);
VALA_EXTERN UndoGroupsMerge* undo_groups_merge_new (GArray* groups,
                                        NodeGroup* merged);
VALA_EXTERN UndoGroupsMerge* undo_groups_merge_construct (GType object_type,
                                              GArray* groups,
                                              NodeGroup* merged);
VALA_EXTERN UndoItem* undo_item_new (const gchar* name);
VALA_EXTERN UndoItem* undo_item_construct (GType object_type,
                               const gchar* name);
static void _g_object_unref0_ (gpointer var);
static void _vala_NodeGroup_free_function_content_of (gpointer data);
static void undo_groups_merge_real_undo (UndoItem* base,
                                  MindMap* map);
VALA_EXTERN gpointer selection_ref (gpointer instance);
VALA_EXTERN void selection_unref (gpointer instance);
VALA_EXTERN GParamSpec* param_spec_selection (const gchar* name,
                                  const gchar* nick,
                                  const gchar* blurb,
                                  GType object_type,
                                  GParamFlags flags);
VALA_EXTERN void value_set_selection (GValue* value,
                          gpointer v_object);
VALA_EXTERN void value_take_selection (GValue* value,
                           gpointer v_object);
VALA_EXTERN gpointer value_get_selection (const GValue* value);
VALA_EXTERN GType selection_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (Selection, selection_unref)
VALA_EXTERN Selection* mind_map_get_selected (MindMap* self);
VALA_EXTERN gboolean selection_clear (Selection* self,
                          gboolean signal_change,
                          gdouble alpha);
VALA_EXTERN gpointer node_groups_ref (gpointer instance);
VALA_EXTERN void node_groups_unref (gpointer instance);
VALA_EXTERN GParamSpec* param_spec_node_groups (const gchar* name,
                                    const gchar* nick,
                                    const gchar* blurb,
                                    GType object_type,
                                    GParamFlags flags);
VALA_EXTERN void value_set_node_groups (GValue* value,
                            gpointer v_object);
VALA_EXTERN void value_take_node_groups (GValue* value,
                             gpointer v_object);
VALA_EXTERN gpointer value_get_node_groups (const GValue* value);
VALA_EXTERN GType node_groups_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (NodeGroups, node_groups_unref)
VALA_EXTERN NodeGroups* mind_map_get_groups (MindMap* self);
VALA_EXTERN void node_groups_remove_group (NodeGroups* self,
                               NodeGroup* group);
VALA_EXTERN void node_groups_add_group (NodeGroups* self,
                            NodeGroup* group);
VALA_EXTERN gboolean selection_add_group (Selection* self,
                              NodeGroup* group);
VALA_EXTERN void mind_map_queue_draw (MindMap* self);
VALA_EXTERN void mind_map_auto_save (MindMap* self);
static void undo_groups_merge_real_redo (UndoItem* base,
                                  MindMap* map);
static void undo_groups_merge_finalize (GObject * obj);
static GType undo_groups_merge_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
undo_groups_merge_get_instance_private (UndoGroupsMerge* self)
{
	return G_STRUCT_MEMBER_P (self, UndoGroupsMerge_private_offset);
}

static void
_g_object_unref0_ (gpointer var)
{
	(var == NULL) ? NULL : (var = (g_object_unref (var), NULL));
}

static void
_vala_NodeGroup_free_function_content_of (gpointer data)
{
	NodeGroup* self;
	self = *((NodeGroup**) data);
	_g_object_unref0_ (self);
}

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

UndoGroupsMerge*
undo_groups_merge_construct (GType object_type,
                             GArray* groups,
                             NodeGroup* merged)
{
	UndoGroupsMerge * self = NULL;
	GArray* _tmp0_;
	NodeGroup* _tmp1_;
	g_return_val_if_fail (groups != NULL, NULL);
	g_return_val_if_fail (merged != NULL, NULL);
	self = (UndoGroupsMerge*) undo_item_construct (object_type, _ ("group merge"));
	_tmp0_ = g_array_new (TRUE, TRUE, sizeof (NodeGroup*));
	g_array_set_clear_func (_tmp0_, (GDestroyNotify) _vala_NodeGroup_free_function_content_of);
	_g_array_unref0 (self->priv->_groups);
	self->priv->_groups = _tmp0_;
	_tmp1_ = _g_object_ref0 (merged);
	_g_object_unref0 (self->priv->_merged);
	self->priv->_merged = _tmp1_;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp2_ = FALSE;
			_tmp2_ = TRUE;
			while (TRUE) {
				GArray* _tmp4_;
				NodeGroup* _tmp5_;
				NodeGroup* _tmp6_;
				if (!_tmp2_) {
					gint _tmp3_;
					_tmp3_ = i;
					i = _tmp3_ + 1;
				}
				_tmp2_ = FALSE;
				if (!(((guint) i) < groups->len)) {
					break;
				}
				_tmp4_ = self->priv->_groups;
				_tmp5_ = g_array_index (groups, NodeGroup*, (guint) i);
				_tmp6_ = _g_object_ref0 (_tmp5_);
				g_array_append_val (_tmp4_, _tmp6_);
			}
		}
	}
	return self;
}

UndoGroupsMerge*
undo_groups_merge_new (GArray* groups,
                       NodeGroup* merged)
{
	return undo_groups_merge_construct (TYPE_UNDO_GROUPS_MERGE, groups, merged);
}

static void
undo_groups_merge_real_undo (UndoItem* base,
                             MindMap* map)
{
	UndoGroupsMerge * self;
	Selection* _tmp0_;
	Selection* _tmp1_;
	NodeGroups* _tmp2_;
	NodeGroups* _tmp3_;
	NodeGroup* _tmp4_;
	self = (UndoGroupsMerge*) base;
	g_return_if_fail (map != NULL);
	_tmp0_ = mind_map_get_selected (map);
	_tmp1_ = _tmp0_;
	selection_clear (_tmp1_, TRUE, 1.0);
	_tmp2_ = mind_map_get_groups (map);
	_tmp3_ = _tmp2_;
	_tmp4_ = self->priv->_merged;
	node_groups_remove_group (_tmp3_, _tmp4_);
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp5_ = FALSE;
			_tmp5_ = TRUE;
			while (TRUE) {
				GArray* _tmp7_;
				NodeGroups* _tmp8_;
				NodeGroups* _tmp9_;
				GArray* _tmp10_;
				NodeGroup* _tmp11_;
				Selection* _tmp12_;
				Selection* _tmp13_;
				GArray* _tmp14_;
				NodeGroup* _tmp15_;
				if (!_tmp5_) {
					gint _tmp6_;
					_tmp6_ = i;
					i = _tmp6_ + 1;
				}
				_tmp5_ = FALSE;
				_tmp7_ = self->priv->_groups;
				if (!(((guint) i) < _tmp7_->len)) {
					break;
				}
				_tmp8_ = mind_map_get_groups (map);
				_tmp9_ = _tmp8_;
				_tmp10_ = self->priv->_groups;
				_tmp11_ = g_array_index (_tmp10_, NodeGroup*, (guint) i);
				node_groups_add_group (_tmp9_, _tmp11_);
				_tmp12_ = mind_map_get_selected (map);
				_tmp13_ = _tmp12_;
				_tmp14_ = self->priv->_groups;
				_tmp15_ = g_array_index (_tmp14_, NodeGroup*, (guint) i);
				selection_add_group (_tmp13_, _tmp15_);
			}
		}
	}
	mind_map_queue_draw (map);
	mind_map_auto_save (map);
}

static void
undo_groups_merge_real_redo (UndoItem* base,
                             MindMap* map)
{
	UndoGroupsMerge * self;
	Selection* _tmp0_;
	Selection* _tmp1_;
	NodeGroups* _tmp9_;
	NodeGroups* _tmp10_;
	NodeGroup* _tmp11_;
	Selection* _tmp12_;
	Selection* _tmp13_;
	NodeGroup* _tmp14_;
	self = (UndoGroupsMerge*) base;
	g_return_if_fail (map != NULL);
	_tmp0_ = mind_map_get_selected (map);
	_tmp1_ = _tmp0_;
	selection_clear (_tmp1_, TRUE, 1.0);
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp2_ = FALSE;
			_tmp2_ = TRUE;
			while (TRUE) {
				GArray* _tmp4_;
				NodeGroups* _tmp5_;
				NodeGroups* _tmp6_;
				GArray* _tmp7_;
				NodeGroup* _tmp8_;
				if (!_tmp2_) {
					gint _tmp3_;
					_tmp3_ = i;
					i = _tmp3_ + 1;
				}
				_tmp2_ = FALSE;
				_tmp4_ = self->priv->_groups;
				if (!(((guint) i) < _tmp4_->len)) {
					break;
				}
				_tmp5_ = mind_map_get_groups (map);
				_tmp6_ = _tmp5_;
				_tmp7_ = self->priv->_groups;
				_tmp8_ = g_array_index (_tmp7_, NodeGroup*, (guint) i);
				node_groups_remove_group (_tmp6_, _tmp8_);
			}
		}
	}
	_tmp9_ = mind_map_get_groups (map);
	_tmp10_ = _tmp9_;
	_tmp11_ = self->priv->_merged;
	node_groups_add_group (_tmp10_, _tmp11_);
	_tmp12_ = mind_map_get_selected (map);
	_tmp13_ = _tmp12_;
	_tmp14_ = self->priv->_merged;
	selection_add_group (_tmp13_, _tmp14_);
	mind_map_queue_draw (map);
	mind_map_auto_save (map);
}

static void
undo_groups_merge_class_init (UndoGroupsMergeClass * klass,
                              gpointer klass_data)
{
	undo_groups_merge_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &UndoGroupsMerge_private_offset);
	((UndoItemClass *) klass)->undo = (void (*) (UndoItem*, MindMap*)) undo_groups_merge_real_undo;
	((UndoItemClass *) klass)->redo = (void (*) (UndoItem*, MindMap*)) undo_groups_merge_real_redo;
	G_OBJECT_CLASS (klass)->finalize = undo_groups_merge_finalize;
}

static void
undo_groups_merge_instance_init (UndoGroupsMerge * self,
                                 gpointer klass)
{
	self->priv = undo_groups_merge_get_instance_private (self);
}

static void
undo_groups_merge_finalize (GObject * obj)
{
	UndoGroupsMerge * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_UNDO_GROUPS_MERGE, UndoGroupsMerge);
	_g_array_unref0 (self->priv->_groups);
	_g_object_unref0 (self->priv->_merged);
	G_OBJECT_CLASS (undo_groups_merge_parent_class)->finalize (obj);
}

static GType
undo_groups_merge_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (UndoGroupsMergeClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) undo_groups_merge_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (UndoGroupsMerge), 0, (GInstanceInitFunc) undo_groups_merge_instance_init, NULL };
	GType undo_groups_merge_type_id;
	undo_groups_merge_type_id = g_type_register_static (TYPE_UNDO_ITEM, "UndoGroupsMerge", &g_define_type_info, 0);
	UndoGroupsMerge_private_offset = g_type_add_instance_private (undo_groups_merge_type_id, sizeof (UndoGroupsMergePrivate));
	return undo_groups_merge_type_id;
}

GType
undo_groups_merge_get_type (void)
{
	static volatile gsize undo_groups_merge_type_id__once = 0;
	if (g_once_init_enter (&undo_groups_merge_type_id__once)) {
		GType undo_groups_merge_type_id;
		undo_groups_merge_type_id = undo_groups_merge_get_type_once ();
		g_once_init_leave (&undo_groups_merge_type_id__once, undo_groups_merge_type_id);
	}
	return undo_groups_merge_type_id__once;
}

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

