/* PIKA - Photo and Image Kooker Application
 * a rebranding of The GNU Image Manipulation Program (created with heckimp)
 * A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
 *
 * Original copyright, applying to most contents (license remains unchanged): 
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <cairo.h>
#include <gegl.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#include "libpikacolor/pikacolor.h"
#include "libpikamath/pikamath.h"
#include "libpikabase/pikabase.h"

#include "core-types.h"

#include "gegl/pika-babl-compat.h"
#include "gegl/pika-gegl-apply-operation.h"
#include "gegl/pika-gegl-nodes.h"
#include "gegl/pika-gegl-utils.h"

#include "vectors/pikavectors.h"

#include "pika.h"
#include "pikacontext.h"
#include "pikaerror.h"
#include "pikagrouplayer.h"
#include "pikaimage.h"
#include "pikaimage-merge.h"
#include "pikaimage-undo.h"
#include "pikaitemstack.h"
#include "pikalayer-floating-selection.h"
#include "pikalayer-new.h"
#include "pikalayermask.h"
#include "pikaparasitelist.h"
#include "pikapickable.h"
#include "pikaprogress.h"
#include "pikaprojectable.h"
#include "pikaundostack.h"

#include "pika-intl.h"


static PikaLayer * pika_image_merge_layers (PikaImage     *image,
                                            PikaContainer *container,
                                            GSList        *merge_list,
                                            PikaContext   *context,
                                            PikaMergeType  merge_type,
                                            const gchar   *undo_desc,
                                            PikaProgress  *progress);


/*  public functions  */

GList *
pika_image_merge_visible_layers (PikaImage     *image,
                                 PikaContext   *context,
                                 PikaMergeType  merge_type,
                                 gboolean       merge_active_group,
                                 gboolean       discard_invisible,
                                 PikaProgress  *progress)
{
  const gchar *undo_desc  = C_("undo-type", "Merge Visible Layers");
  GList       *containers = NULL;
  GList       *new_layers = NULL;
  GList       *iter;
  GList       *iter2;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_CONTEXT (context), NULL);
  g_return_val_if_fail (progress == NULL || PIKA_IS_PROGRESS (progress), NULL);

  if (merge_active_group)
    {
      GList *selected_layers = pika_image_get_selected_layers (image);

      /*  if the active layer is the floating selection, get the
       *  underlying drawable, but only if it is a layer
       */
      if (g_list_length (selected_layers) == 1 && pika_layer_is_floating_sel (selected_layers->data))
        {
          PikaDrawable *fs_drawable;

          fs_drawable = pika_layer_get_floating_sel_drawable (selected_layers->data);

          if (PIKA_IS_LAYER (fs_drawable))
            containers = g_list_prepend (containers,
                                         pika_item_get_container (PIKA_ITEM (fs_drawable)));
        }
      else
        {
          for (iter = selected_layers; iter; iter = iter->next)
            if (! pika_item_get_parent (iter->data))
              break;

          /* No need to list selected groups if any selected layer is
           * top-level.
           */
          if (iter == NULL)
            {
              for (iter = selected_layers; iter; iter = iter->next)
                {
                  for (iter2 = selected_layers; iter2; iter2 = iter2->next)
                    {
                      /* Only retain a selected layer's container if no
                       * other selected layers are its parents.
                       */
                      if (iter->data != iter2->data &&
                          pika_viewable_is_ancestor (iter2->data, iter->data))
                        break;
                    }
                  if (iter2 == NULL &&
                      ! g_list_find (containers, pika_item_get_container (PIKA_ITEM (iter->data))))
                    containers = g_list_prepend (containers,
                                                 pika_item_get_container (PIKA_ITEM (iter->data)));
                }
            }
        }
    }
  if (! containers)
    containers = g_list_prepend (NULL, pika_image_get_layers (image));

  pika_set_busy (image->pika);
  pika_image_undo_group_start (image,
                               PIKA_UNDO_GROUP_IMAGE_LAYERS_MERGE,
                               undo_desc);

  for (iter = containers; iter; iter = iter->next)
    {
      PikaContainer *container      = iter->data;
      GSList        *merge_list     = NULL;
      GSList        *invisible_list = NULL;

      for (iter2 = pika_item_stack_get_item_iter (PIKA_ITEM_STACK (container));
           iter2;
           iter2 = g_list_next (iter2))
        {
          PikaLayer *layer = iter2->data;

          if (pika_layer_is_floating_sel (layer))
            continue;

          if (pika_item_get_visible (PIKA_ITEM (layer)))
            {
              merge_list = g_slist_append (merge_list, layer);
            }
          else if (discard_invisible)
            {
              invisible_list = g_slist_append (invisible_list, layer);
            }
        }

      if (merge_list)
        {
          PikaLayer   *layer;
          /* if there's a floating selection, anchor it */
          if (pika_image_get_floating_selection (image))
            floating_sel_anchor (pika_image_get_floating_selection (image));

          layer = pika_image_merge_layers (image,
                                           container,
                                           merge_list, context, merge_type,
                                           undo_desc, progress);
          g_slist_free (merge_list);

          if (invisible_list)
            {
              GSList *list;

              for (list = invisible_list; list; list = g_slist_next (list))
                pika_image_remove_layer (image, list->data, TRUE, NULL);

              g_slist_free (invisible_list);
            }

          new_layers = g_list_prepend (new_layers, layer);
        }
    }

  pika_image_set_selected_layers (image, new_layers);
  pika_image_undo_group_end (image);
  pika_unset_busy (image->pika);

  g_list_free (new_layers);
  g_list_free (containers);

  return pika_image_get_selected_layers (image);
}

PikaLayer *
pika_image_flatten (PikaImage     *image,
                    PikaContext   *context,
                    PikaProgress  *progress,
                    GError       **error)
{
  GList     *list;
  GSList    *merge_list = NULL;
  PikaLayer *layer;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_CONTEXT (context), NULL);
  g_return_val_if_fail (progress == NULL || PIKA_IS_PROGRESS (progress), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  for (list = pika_image_get_layer_iter (image);
       list;
       list = g_list_next (list))
    {
      layer = list->data;

      if (pika_layer_is_floating_sel (layer))
        continue;

      if (pika_item_get_visible (PIKA_ITEM (layer)))
        merge_list = g_slist_append (merge_list, layer);
    }

  if (merge_list)
    {
      const gchar *undo_desc = C_("undo-type", "Flatten Image");

      pika_set_busy (image->pika);

      pika_image_undo_group_start (image,
                                   PIKA_UNDO_GROUP_IMAGE_LAYERS_MERGE,
                                   undo_desc);

      /* if there's a floating selection, anchor it */
      if (pika_image_get_floating_selection (image))
        floating_sel_anchor (pika_image_get_floating_selection (image));

      layer = pika_image_merge_layers (image,
                                       pika_image_get_layers (image),
                                       merge_list, context,
                                       PIKA_FLATTEN_IMAGE,
                                       undo_desc, progress);
      g_slist_free (merge_list);

      pika_image_alpha_changed (image);

      pika_image_undo_group_end (image);

      pika_unset_busy (image->pika);

      return layer;
    }

  g_set_error_literal (error, PIKA_ERROR, PIKA_FAILED,
                       _("Cannot flatten an image without any visible layer."));
  return NULL;
}

GList *
pika_image_merge_down (PikaImage      *image,
                       GList          *layers,
                       PikaContext    *context,
                       PikaMergeType   merge_type,
                       PikaProgress   *progress,
                       GError        **error)
{
  GList       *merged_layers = NULL;
  GList       *merge_lists   = NULL;
  GSList      *merge_list    = NULL;
  PikaLayer   *layer;
  GList       *list;
  const gchar *undo_desc;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_CONTEXT (context), NULL);
  g_return_val_if_fail (progress == NULL || PIKA_IS_PROGRESS (progress), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  layers = g_list_copy (layers);
  while ((list = layers))
    {
      GList *list2;

      g_return_val_if_fail (PIKA_IS_LAYER (list->data), NULL);
      g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (list->data)), NULL);

      if (pika_layer_is_floating_sel (list->data))
        {
          g_set_error_literal (error, PIKA_ERROR, PIKA_FAILED,
                               _("Cannot merge down a floating selection."));
          g_list_free (layers);
          g_list_free_full (merge_lists, (GDestroyNotify) g_slist_free);
          return NULL;
        }

      if (! pika_item_get_visible (PIKA_ITEM (list->data)))
        {
          g_set_error_literal (error, PIKA_ERROR, PIKA_FAILED,
                               _("Cannot merge down an invisible layer."));
          g_list_free (layers);
          g_list_free_full (merge_lists, (GDestroyNotify) g_slist_free);
          return NULL;
        }

      for (list2 = pika_item_get_container_iter (PIKA_ITEM (list->data));
           list2;
           list2 = g_list_next (list2))
        {
          if (list2->data == list->data)
            break;
        }

      layer = NULL;
      for (list2 = g_list_next (list2);
           list2;
           list2 = g_list_next (list2))
        {
          layer = list2->data;

          if (pika_item_get_visible (PIKA_ITEM (layer)))
            {
              if (pika_viewable_get_children (PIKA_VIEWABLE (layer)))
                {
                  g_set_error_literal (error, PIKA_ERROR, PIKA_FAILED,
                                       _("Cannot merge down to a layer group."));
                  g_list_free (layers);
                  g_list_free_full (merge_lists, (GDestroyNotify) g_slist_free);
                  return NULL;
                }

              if (pika_item_is_content_locked (PIKA_ITEM (layer), NULL))
                {
                  g_set_error_literal (error, PIKA_ERROR, PIKA_FAILED,
                                       _("The layer to merge down to is locked."));
                  g_list_free (layers);
                  g_list_free_full (merge_lists, (GDestroyNotify) g_slist_free);
                  return NULL;
                }

              break;
            }

          layer = NULL;
        }

      if (! layer)
        {
          g_set_error_literal (error, PIKA_ERROR, PIKA_FAILED,
                               _("There is no visible layer to merge down to."));
          g_list_free (layers);
          g_list_free_full (merge_lists, (GDestroyNotify) g_slist_free);
          return NULL;
        }

      merge_list = g_slist_append (merge_list, list->data);
      layers     = g_list_delete_link (layers, layers);

      if ((list = g_list_find (layers, layer)))
        {
          /* The next item is itself in the merge down list. Continue
           * filling the same merge list instead of starting a new one.
           */
          layers = g_list_delete_link (layers, list);
          layers = g_list_prepend (layers, layer);

          continue;
        }

      merge_list  = g_slist_append (merge_list, layer);
      merge_lists = g_list_prepend (merge_lists, merge_list);
      merge_list  = NULL;
    }

  undo_desc = C_("undo-type", "Merge Down");

  pika_set_busy (image->pika);

  pika_image_undo_group_start (image,
                               PIKA_UNDO_GROUP_IMAGE_LAYERS_MERGE,
                               undo_desc);

  for (list = merge_lists; list; list = list->next)
    {
      merge_list = list->data;
      layer = pika_image_merge_layers (image,
                                       pika_item_get_container (merge_list->data),
                                       merge_list, context, merge_type,
                                       undo_desc, progress);
      merged_layers = g_list_prepend (merged_layers, layer);
    }

  g_list_free_full (merge_lists, (GDestroyNotify) g_slist_free);

  pika_image_undo_group_end (image);

  pika_unset_busy (image->pika);

  return merged_layers;
}

PikaLayer *
pika_image_merge_group_layer (PikaImage      *image,
                              PikaGroupLayer *group)
{
  PikaLayer *parent;
  PikaLayer *layer;
  gint       index;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_GROUP_LAYER (group), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (group)), NULL);
  g_return_val_if_fail (pika_item_get_image (PIKA_ITEM (group)) == image, NULL);

  pika_image_undo_group_start (image, PIKA_UNDO_GROUP_IMAGE_LAYERS_MERGE,
                               C_("undo-type", "Merge Layer Group"));

  parent = pika_layer_get_parent (PIKA_LAYER (group));
  index  = pika_item_get_index (PIKA_ITEM (group));

  /* if this is a pass-through group, change its mode to NORMAL *before*
   * duplicating it, since PASS_THROUGH mode is invalid for regular layers.
   * see bug #793714.
   */
  if (pika_layer_get_mode (PIKA_LAYER (group)) == PIKA_LAYER_MODE_PASS_THROUGH)
    {
      PikaLayerColorSpace    blend_space;
      PikaLayerColorSpace    composite_space;
      PikaLayerCompositeMode composite_mode;

      /* keep the group's current blend space, composite space, and composite
       * mode.
       */
      blend_space     = pika_layer_get_blend_space     (PIKA_LAYER (group));
      composite_space = pika_layer_get_composite_space (PIKA_LAYER (group));
      composite_mode  = pika_layer_get_composite_mode  (PIKA_LAYER (group));

      pika_layer_set_mode            (PIKA_LAYER (group), PIKA_LAYER_MODE_NORMAL, TRUE);
      pika_layer_set_blend_space     (PIKA_LAYER (group), blend_space,            TRUE);
      pika_layer_set_composite_space (PIKA_LAYER (group), composite_space,        TRUE);
      pika_layer_set_composite_mode  (PIKA_LAYER (group), composite_mode,         TRUE);
    }

  layer = PIKA_LAYER (pika_item_duplicate (PIKA_ITEM (group),
                                           PIKA_TYPE_LAYER));

  pika_object_set_name (PIKA_OBJECT (layer), pika_object_get_name (group));

  pika_image_remove_layer (image, PIKA_LAYER (group), TRUE, NULL);
  pika_image_add_layer (image, layer, parent, index, TRUE);

  pika_image_undo_group_end (image);

  return layer;
}


/* merging vectors */

PikaVectors *
pika_image_merge_visible_vectors (PikaImage  *image,
                                  GError    **error)
{
  GList       *list;
  GList       *merge_list = NULL;
  PikaVectors *vectors;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  for (list = pika_image_get_vectors_iter (image);
       list;
       list = g_list_next (list))
    {
      vectors = list->data;

      if (pika_item_get_visible (PIKA_ITEM (vectors)))
        merge_list = g_list_prepend (merge_list, vectors);
    }

  merge_list = g_list_reverse (merge_list);

  if (merge_list && merge_list->next)
    {
      PikaVectors *target_vectors;
      gchar       *name;
      gint         pos;

      pika_set_busy (image->pika);

      pika_image_undo_group_start (image, PIKA_UNDO_GROUP_IMAGE_VECTORS_MERGE,
                                   C_("undo-type", "Merge Visible Paths"));

      vectors = PIKA_VECTORS (merge_list->data);

      name = g_strdup (pika_object_get_name (vectors));
      pos = pika_item_get_index (PIKA_ITEM (vectors));

      target_vectors = PIKA_VECTORS (pika_item_duplicate (PIKA_ITEM (vectors),
                                                          PIKA_TYPE_VECTORS));
      pika_image_remove_vectors (image, vectors, TRUE, NULL);

      for (list = g_list_next (merge_list);
           list;
           list = g_list_next (list))
        {
          vectors = list->data;

          pika_vectors_add_strokes (vectors, target_vectors);
          pika_image_remove_vectors (image, vectors, TRUE, NULL);
        }

      pika_object_take_name (PIKA_OBJECT (target_vectors), name);

      g_list_free (merge_list);

      /* FIXME tree */
      pika_image_add_vectors (image, target_vectors, NULL, pos, TRUE);
      pika_unset_busy (image->pika);

      pika_image_undo_group_end (image);

      return target_vectors;
    }
  else
    {
      g_set_error_literal (error, PIKA_ERROR, PIKA_FAILED,
                           _("Not enough visible paths for a merge. "
                             "There must be at least two."));
      return NULL;
    }
}


/*  private functions  */

static PikaLayer *
pika_image_merge_layers (PikaImage     *image,
                         PikaContainer *container,
                         GSList        *merge_list,
                         PikaContext   *context,
                         PikaMergeType  merge_type,
                         const gchar   *undo_desc,
                         PikaProgress  *progress)
{
  PikaLayer        *parent;
  gint              x1, y1;
  gint              x2, y2;
  GSList           *layers;
  PikaLayer        *layer;
  PikaLayer        *top_layer;
  PikaLayer        *bottom_layer;
  PikaLayer        *merge_layer;
  gint              position;
  GeglNode         *node;
  GeglNode         *source_node;
  GeglNode         *flatten_node;
  GeglNode         *offset_node;
  GeglNode         *last_node;
  GeglNode         *last_node_source;
  PikaParasiteList *parasites;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_CONTEXT (context), NULL);
  g_return_val_if_fail (progress == NULL || PIKA_IS_PROGRESS (progress), NULL);

  top_layer = merge_list->data;
  parent    = pika_layer_get_parent (top_layer);

  /*  Make sure the image's graph is constructed, so that top-level layers have
   *  a parent node.
   */
  (void) pika_projectable_get_graph (PIKA_PROJECTABLE (image));

  /*  Make sure the parent's graph is constructed, so that the top layer has a
   *  parent node, even if it is the child of a group layer (in particular, of
   *  an invisible group layer, whose graph may not have been constructed as a
   *  result of the above call.  see issue #2095.)
   */
  if (parent)
    (void) pika_filter_get_node (PIKA_FILTER (parent));

  /*  Build our graph inside the top-layer's parent node  */
  source_node = pika_filter_get_node (PIKA_FILTER (top_layer));
  node = gegl_node_get_parent (source_node);

  g_return_val_if_fail (node, NULL);

  /*  Get the layer extents  */
  x1 = y1 = 0;
  x2 = y2 = 0;
  for (layers = merge_list; layers; layers = g_slist_next (layers))
    {
      gint off_x, off_y;

      layer = layers->data;

      pika_item_get_offset (PIKA_ITEM (layer), &off_x, &off_y);

      switch (merge_type)
        {
        case PIKA_EXPAND_AS_NECESSARY:
        case PIKA_CLIP_TO_IMAGE:
          if (layers == merge_list)
            {
              x1 = off_x;
              y1 = off_y;
              x2 = off_x + pika_item_get_width  (PIKA_ITEM (layer));
              y2 = off_y + pika_item_get_height (PIKA_ITEM (layer));
            }
          else
            {
              if (off_x < x1)
                x1 = off_x;
              if (off_y < y1)
                y1 = off_y;
              if ((off_x + pika_item_get_width (PIKA_ITEM (layer))) > x2)
                x2 = (off_x + pika_item_get_width (PIKA_ITEM (layer)));
              if ((off_y + pika_item_get_height (PIKA_ITEM (layer))) > y2)
                y2 = (off_y + pika_item_get_height (PIKA_ITEM (layer)));
            }

          if (merge_type == PIKA_CLIP_TO_IMAGE)
            {
              x1 = CLAMP (x1, 0, pika_image_get_width  (image));
              y1 = CLAMP (y1, 0, pika_image_get_height (image));
              x2 = CLAMP (x2, 0, pika_image_get_width  (image));
              y2 = CLAMP (y2, 0, pika_image_get_height (image));
            }
          break;

        case PIKA_CLIP_TO_BOTTOM_LAYER:
          if (layers->next == NULL)
            {
              x1 = off_x;
              y1 = off_y;
              x2 = off_x + pika_item_get_width  (PIKA_ITEM (layer));
              y2 = off_y + pika_item_get_height (PIKA_ITEM (layer));
            }
          break;

        case PIKA_FLATTEN_IMAGE:
          if (layers->next == NULL)
            {
              x1 = 0;
              y1 = 0;
              x2 = pika_image_get_width  (image);
              y2 = pika_image_get_height (image);
            }
          break;
        }
    }

  if ((x2 - x1) == 0 || (y2 - y1) == 0)
    return NULL;

  bottom_layer = layer;

  flatten_node = NULL;

  if (merge_type == PIKA_FLATTEN_IMAGE ||
      (pika_drawable_is_indexed (PIKA_DRAWABLE (layer)) &&
       ! pika_drawable_has_alpha (PIKA_DRAWABLE (layer))))
    {
      PikaRGB bg;

      merge_layer = pika_layer_new (image, (x2 - x1), (y2 - y1),
                                    pika_image_get_layer_format (image, FALSE),
                                    pika_object_get_name (bottom_layer),
                                    PIKA_OPACITY_OPAQUE,
                                    pika_image_get_default_new_layer_mode (image));

      if (! merge_layer)
        {
          g_warning ("%s: could not allocate merge layer", G_STRFUNC);

          return NULL;
        }

      /*  get the background for compositing  */
      pika_context_get_background (context, &bg);
      pika_pickable_srgb_to_image_color (PIKA_PICKABLE (layer),
                                         &bg, &bg);

      flatten_node = pika_gegl_create_flatten_node
        (&bg,
         pika_drawable_get_space (PIKA_DRAWABLE (layer)),
         pika_layer_get_real_composite_space (bottom_layer));
    }
  else
    {
      /*  The final merged layer inherits the name of the bottom most layer
       *  and the resulting layer has an alpha channel whether or not the
       *  original did. Opacity is set to 100% and the MODE is set to normal.
       */

      merge_layer =
        pika_layer_new (image, (x2 - x1), (y2 - y1),
                        pika_drawable_get_format_with_alpha (PIKA_DRAWABLE (bottom_layer)),
                        pika_object_get_name (bottom_layer),
                        PIKA_OPACITY_OPAQUE,
                        pika_image_get_default_new_layer_mode (image));

      if (! merge_layer)
        {
          g_warning ("%s: could not allocate merge layer", G_STRFUNC);

          return NULL;
        }
    }

  if (merge_type == PIKA_FLATTEN_IMAGE)
    {
      position = 0;
    }
  else
    {
      /*  Find the index in the layer list of the bottom layer--we need this
       *  in order to add the final, merged layer to the layer list correctly
       */
      position =
        pika_container_get_n_children (container) -
        pika_container_get_child_index (container, PIKA_OBJECT (bottom_layer));
    }

  pika_item_set_offset (PIKA_ITEM (merge_layer), x1, y1);

  offset_node = gegl_node_new_child (node,
                                     "operation", "gegl:translate",
                                     "x",         (gdouble) -x1,
                                     "y",         (gdouble) -y1,
                                     NULL);

  if (flatten_node)
    {
      gegl_node_add_child (node, flatten_node);
      g_object_unref (flatten_node);

      gegl_node_link_many (source_node, flatten_node, offset_node, NULL);
    }
  else
    {
      gegl_node_link_many (source_node, offset_node, NULL);
    }

  /*  Disconnect the bottom-layer node's input  */
  last_node        = pika_filter_get_node (PIKA_FILTER (bottom_layer));
  last_node_source = gegl_node_get_producer (last_node, "input", NULL);

  gegl_node_disconnect (last_node, "input");

  /*  Render the graph into the merge layer  */
  pika_gegl_apply_operation (NULL, progress, undo_desc, offset_node,
                             pika_drawable_get_buffer (
                               PIKA_DRAWABLE (merge_layer)),
                             NULL, FALSE);

  /*  Reconnect the bottom-layer node's input  */
  if (last_node_source)
    gegl_node_link (last_node_source, last_node);

  /*  Clean up the graph  */
  gegl_node_remove_child (node, offset_node);

  if (flatten_node)
    gegl_node_remove_child (node, flatten_node);

  /* Copy the tattoo and parasites of the bottom layer to the new layer */
  pika_item_set_tattoo (PIKA_ITEM (merge_layer),
                        pika_item_get_tattoo (PIKA_ITEM (bottom_layer)));

  parasites = pika_item_get_parasites (PIKA_ITEM (bottom_layer));
  parasites = pika_parasite_list_copy (parasites);
  pika_item_set_parasites (PIKA_ITEM (merge_layer), parasites);
  g_object_unref (parasites);

  /*  Remove the merged layers from the image  */
  for (layers = merge_list; layers; layers = g_slist_next (layers))
    pika_image_remove_layer (image, layers->data, TRUE, NULL);

  pika_item_set_visible (PIKA_ITEM (merge_layer), TRUE, FALSE);

  /*  if the type is flatten, remove all the remaining layers  */
  if (merge_type == PIKA_FLATTEN_IMAGE)
    {
      GList *list = pika_image_get_layer_iter (image);

      while (list)
        {
          layer = list->data;

          list = g_list_next (list);
          pika_image_remove_layer (image, layer, TRUE, NULL);
        }

      pika_image_add_layer (image, merge_layer, parent,
                            position, TRUE);
    }
  else
    {
      /*  Add the layer to the image  */
      pika_image_add_layer (image, merge_layer, parent,
                            pika_container_get_n_children (container) -
                            position + 1,
                            TRUE);
    }

  pika_drawable_update (PIKA_DRAWABLE (merge_layer), 0, 0, -1, -1);

  return merge_layer;
}
