"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _isEmpty = _interopRequireDefault(require("lodash/isEmpty"));

var _isObject = _interopRequireDefault(require("lodash/isObject"));

var _mapKeys = _interopRequireDefault(require("lodash/mapKeys"));

var _cloneDeep = _interopRequireDefault(require("lodash/cloneDeep"));

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

function _extends() { _extends = Object.assign ? Object.assign.bind() : 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); }

/**
 * 处理树相关的工具
 */
var _default = {
  /** 将一维数组转换为树结构 */
  translateDataToTree: function translateDataToTree(jsonData, id, pid, mapProps) {
    var newData = (0, _cloneDeep["default"])(jsonData);
    var result = [];
    var temp = {};

    for (var i = 0; i < newData.length; i += 1) {
      temp[newData[i][id]] = newData[i]; // 以id作为索引存储元素，可以无需遍历直接定位元素
    }

    var _loop_1 = function _loop_1(j) {
      var currentElement = newData[j]; // 添加需要的属性

      if (mapProps && (0, _isObject["default"])(mapProps) && !(0, _isEmpty["default"])(mapProps)) {
        (0, _mapKeys["default"])(mapProps, function (value, key) {
          currentElement[key] = currentElement[value];
        });
      }

      var tempCurrentElementParent = temp[currentElement[pid]]; // 临时变量里面的当前元素的父元素

      if (tempCurrentElementParent) {
        // 如果存在父元素
        if (!tempCurrentElementParent.children) {
          // 如果父元素没有chindren键
          tempCurrentElementParent.children = []; // 设上父元素的children键
        }

        tempCurrentElementParent.children.push(currentElement); // 给父元素加上当前元素作为子元素
      } else {
        // 不存在父元素，意味着当前元素是一级元素
        result.push(currentElement);
      }
    };

    for (var j = 0; j < newData.length; j += 1) {
      _loop_1(j);
    }

    return result;
  },

  /**
   * jsonData 输入数据
   * 需要查找的id
   * pk 主键的名称
   * childKeys返回值
   */
  findChildKeys: function findChildKeys(jsonData, id, pk, childKeys) {
    var target = {};
    this.dsFilter(jsonData, id, pk, target);

    if (target && JSON.stringify(target) !== '{}') {
      childKeys.push(target[pk]);

      if (target.children && target.children.length) {
        this.getChildKey(target.children, pk, childKeys);
      }
    }
  },
  dsFilter: function dsFilter(dealData, id, pk, target) {
    for (var i = 0; i < dealData.length; i += 1) {
      if (dealData[i].children && dealData[i].children.length) {
        this.dsFilter(dealData[i].children, id, pk, target);
      }

      if (dealData[i][pk] === id) {
        _extends(target, dealData[i]);
      }
    }
  },
  getChildKey: function getChildKey(dealData, pk, childKeys) {
    for (var i = 0; i < dealData.length; i += 1) {
      if (dealData[i].children && dealData[i].children.length) {
        this.getChildKey(dealData[i].children, pk, childKeys);
      }

      childKeys.push(dealData[i][pk]);
    }
  }
};
exports["default"] = _default;