"use strict";

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

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/**
 * 解析树数据
 * @param {[object]} param [解析对象]
 * @param {[function]} callBack [回调函数 返回一个对象，对象内包含额外属性]
 * @return {[array]} tree [树数据]
 */
function parseTreeStructure(param, callBack, type) {
  var newParam = param;

  if (typeof param === 'string') {
    newParam = JSON.parse(newParam);
  }

  if (!(Object.prototype.toString.call(newParam) === '[object Object]') || !('items' in newParam) || !Array.isArray(newParam.items)) {
    return [];
  }

  var tree = newParam.items;

  tree = function treeProcessor(data) {
    return data.map(function (item) {
      var nodeItem = {
        key: type === 'role' ? item.attributes.code : item.id,
        id: type === 'staff' ? item.attributes.staffId : item.attributes.id || item.id,
        name: item.nodeType == 'user' ? item.attributes.staffName : item.name,
        nodeType: item.nodeType
      };

      if (typeof callBack === 'function') {
        nodeItem = _objectSpread(_objectSpread({}, nodeItem), callBack(item));
      }

      if (item.children.length !== 0) {
        nodeItem = _objectSpread(_objectSpread({}, nodeItem), {}, {
          children: treeProcessor(item.children)
        });
      }

      return nodeItem;
    });
  }(tree);

  return tree;
}
//# sourceMappingURL=utils.js.map
