import "core-js/modules/es.array.reduce.js";
const defaultKeyDic = {
  value: 'value',
  label: 'label',
  children: 'children'
};
const defaultCheckFn = (item, value, keyDic = defaultKeyDic) => {
  return String(item[keyDic.value]) === String(value);
};
export function getPathFromTree(data, id, keyDic = defaultKeyDic, checkFn = defaultCheckFn, indexArray = []) {
  if (!data) return [];
  const arr = Array.from(indexArray);
  for (let i = 0, len = data.length; i < len; i++) {
    arr.push(data[i]);
    if (checkFn(data[i], id, keyDic)) {
      return arr;
    }
    const children = data[i][keyDic.children];
    if (children && children.length) {
      const result = getPathFromTree(children, id, keyDic, checkFn, arr);
      if (result && result.length) return result;
    }
    arr.pop();
  }
  return [];
}
export function getItemFromTree(treeData, value, keyDic = defaultKeyDic, checkFn = defaultCheckFn) {
  keyDic = {
    ...defaultKeyDic,
    ...keyDic
  };
  if (value === '' || !(treeData !== null && treeData !== void 0 && treeData.length)) return null;
  let result = null;
  for (let i = 0; i < treeData.length; i++) {
    const item = treeData[i];
    if (checkFn(item, value, keyDic)) {
      return item;
    }
    if (item[keyDic.children]) {
      result = getItemFromTree(item[keyDic.children], value, keyDic, checkFn);
      if (result) {
        return result;
      }
    }
  }
  return result;
}

/**
 * 根据 key 拍平数组
 * @param arr
 */

export function isArray(val) {
  return Array.isArray(val);
}
export function flatten(arr, key, result = []) {
  return arr.reduce((acc, cur) => {
    const {
      [key]: child,
      ...rest
    } = cur;
    if (isArray(child)) {
      acc.push(rest);
      return flatten(child, key, result);
    }
    acc.push(cur);
    return acc;
  }, result);
}