"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.updateAllNode = exports.updateNode = exports.findNode = exports.insertNode = exports.delNode = exports.moveNode = void 0;

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

/* eslint-disable */
var moveNode = function moveNode(node, sourcePid, sourceIndex) {
  var item = null;

  function _doMove(node) {
    if (item) return;

    if (!node || !node.childNodes) {
      return;
    }

    if (node.id === sourcePid) {
      item = node.childNodes.splice(sourceIndex, 1)[0];
      item && (item.index = sourceIndex);
      return;
    }

    for (var i = 0; i < node.childNodes.length; i++) {
      _doMove(node.childNodes[i]);
    }
  }

  _doMove(node);

  return item;
};

exports.moveNode = moveNode;

var delNode = function delNode(node, pid, idx) {
  var hasDeleted = false;

  function _doDel(node) {
    if (hasDeleted) return;

    if (!node || !node.childNodes) {
      return;
    }

    if (node.id === pid) {
      node.childNodes.splice(idx, 1);
      hasDeleted = true;
      return;
    }

    for (var i = 0; i < node.childNodes.length; i++) {
      _doDel(node.childNodes[i]);
    }
  }

  _doDel(node);
};

exports.delNode = delNode;

var insertNode = function insertNode(node, item, destPid, destIndex) {
  var hasInserted = false;
  var sourcePid = item.pid;
  var sourceIndex = item.index;

  function _doInsert(node) {
    if (hasInserted) return;
    item.pid = destPid;
    item.index = destIndex;

    if (node.id === destPid) {
      if (!node.childNodes) node.childNodes = [];

      if (sourcePid === destPid) {
        // move node on the same level
        if (sourceIndex >= destIndex) {
          node.childNodes.splice(destIndex, 0, item);
        } else {
          node.childNodes.splice(destIndex - 1, 0, item);
        }
      } else {
        // move node between different levels
        node.childNodes.splice(destIndex, 0, item);
      }

      hasInserted = true;
      return;
    }

    if (node && node.childNodes) {
      for (var i = 0; i < node.childNodes.length; i++) {
        _doInsert(node.childNodes[i]);
      }
    }
  }

  _doInsert(node);
};

exports.insertNode = insertNode;

var findNode = function findNode(node, itemId) {
  var itemData = null;

  function _doFind(node) {
    if (itemData) return;

    if (node.id === itemId) {
      itemData = node;
      return;
    }

    node.childNodes && node.childNodes.map(function (n, index) {
      _doFind(n);
    });
  }

  _doFind(node);

  return itemData;
};

exports.findNode = findNode;

var updateNode = function updateNode(node, itemId, newData) {
  var hasUpdated = false;

  function _doUpdate(node) {
    if (hasUpdated) return;

    if (node.id === itemId) {
      _extends(node, newData);

      hasUpdated = true;
      return;
    }

    node.childNodes && node.childNodes.map(function (n, index) {
      _doUpdate(n);
    });
  }

  _doUpdate(node);
};

exports.updateNode = updateNode;

var updateAllNode = function updateAllNode(node, newData) {
  function _doUpdate(node) {
    _extends(node, newData);

    node.childNodes && node.childNodes.map(function (n, index) {
      _doUpdate(n);
    });
  }

  _doUpdate(node);
};

exports.updateAllNode = updateAllNode;