/**
 * 深度合并对象
 * @param  {...any} param 
 * @returns 
 */
function deepAssign(...param) {
  let result = Object.assign({}, ...param);
  for (let item of param) {
    for (let [idx, val] of Object.entries(item)) {
      if (typeof val === 'object') {
        result[idx] = deepAssign(result[idx], val);
      }
    }
  }
  return result;
}


/**
 * 合并数组
 * @param {*} arr1 数组1 
 * @param {*} arr2 数组2
 * @returns 
 */
function mergeArray(arr1, arr2) {
  var arr = [];
  for (var k = 0, len = arr1.length; k < len; k++) {
    arr.push(arr1[k]);
  }
  for (var i = 0, len2 = arr2.length; i < len2; i++) {
    //是否添加该元素
    var flag = true;
    for (var j = 0, len1 = arr.length; j < len1; j++) {
      //如果path相同，则合并children；如果children不存在，则不合并；
      if (arr[j].path == arr2[i].path) {
        flag = false;
        if (arr[j].children instanceof Array && arr2[i].children instanceof Array) {
          arr[j].children = this.mergeArray(arr[j].children, arr2[i].children);
        }
      }
    }
    if (flag) {
      arr.push(arr2[i]);
    }
  }
  //将path为"*"的项移到最后
  var temp;
  var index;
  for (var n = 0, len3 = arr.length; n < len3; n++) {
    if (arr[n].path == '*' || arr[n].path == '/*') {
      index = n;
    }
  }
  if (index != undefined) {
    temp = arr[index];
    arr[index] = arr[len3 - 1];
    arr[len3 - 1] = temp;
  }
  return arr;
}

export default {
  deepAssign,
  mergeArray,
};