"use strict";
var immutable = require("seamless-immutable");

function concatArrayMerger(current, other) {
  if (!(current instanceof Array) || !(other instanceof Array)) return;

  return current.concat(other);
}

function updatingArrayMerger(current, other, config) {
  if (!(current instanceof Array) || !(other instanceof Array)) return;
  if (current.length === 0 || other.length === 0) return;

  var identifier = config.mergerObjectIdentifier;
  if (current[0] === null || !(typeof current[0] === "object") || !current[0][identifier]) return;
  if (other[0] === null || !(typeof other[0] === "object") || !other[0][identifier]) return;

  var currentMap = {};
  for (var i = 0; i < current.length; i++) {
    currentMap[current[i][identifier]] = i;
  }

  var resultList = current.asMutable();
  for (var j = 0; j < other.length; j++) {
    var matchingCurrentIndex = currentMap[other[j][identifier]];

    if (!matchingCurrentIndex) {
      resultList.push(other[j]);
    } else {
      resultList[matchingCurrentIndex] = resultList[matchingCurrentIndex].merge(other[j], config);
    }
  }

  return immutable(resultList);
}

// Export the library
var immutableMergers = {
  concatArrayMerger: concatArrayMerger,
  updatingArrayMerger: updatingArrayMerger
};

Object.freeze(immutableMergers);

module.exports = immutableMergers;

"use strict";
var immutable = require("seamless-immutable");

function concatArrayMerger(current, other) {
  if (!(current instanceof Array) || !(other instanceof Array)) return;

  return current.concat(other);
}

function updatingByIdArrayMerger(current, other, config) {
  if (!(current instanceof Array) || !(other instanceof Array)) return;
  if (current.length === 0 || other.length === 0) return;

  var identifier = config.mergerObjectIdentifier;
  if (current[0] === null || !(typeof current[0] === "object") || !current[0][identifier]) return;
  if (other[0] === null || !(typeof other[0] === "object") || !other[0][identifier]) return;

  var currentMap = {};
  for (var i = 0; i < current.length; i++) {
    currentMap[current[i][identifier]] = i;
  }

  var resultList = current.asMutable();
  for (var j = 0; j < other.length; j++) {
    var matchingCurrentIndex = currentMap[other[j][identifier]];

    if (matchingCurrentIndex === undefined) {
      resultList.push(other[j]);
    } else {
      resultList[matchingCurrentIndex] = resultList[matchingCurrentIndex].merge(other[j], config);
    }
  }

  return immutable(resultList);
}

// Export the library
var immutableMergers = {
  concatArrayMerger: concatArrayMerger,
  updatingByIdArrayMerger: updatingByIdArrayMerger
};

Object.freeze(immutableMergers);

module.exports = immutableMergers;

Fix for when matching index is 0.
Fix for when matching index is 0.
