'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _objectAssign = require('object-assign');

var _objectAssign2 = _interopRequireDefault(_objectAssign);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * Creates a new array with a node injected
 * at the specified indexPath
 * @param {Array} nodes
 * @param {Array} indexPath
 * @param {Array} data
 * @return {Array} newData
 */
function injectNodes(nodeToInject, indexPath, data) {
  var newData = void 0;
  var currentIndex = indexPath[0];

  // last item
  if (indexPath.length === 1) {
    return data.map(function (node, index) {
      if (index === currentIndex) {
        return nodeToInject;
      }

      return node;
    });
  }

  newData = data.map(function (node, index) {
    if (index === currentIndex) {
      return (0, _objectAssign2.default)({}, node, {
        nodes: injectNodes(nodeToInject, indexPath.slice(1), node.nodes)
      });
    }

    return node;
  });

  return newData;
} /**
   * Copyright (c) 2015-present, Zippy Technologies
   *
   * This source code is licensed under the MIT license found in the
   * LICENSE file in the root directory of this source tree.
   */

exports.default = injectNodes;