"use strict";
class ArrayUtils {
  childNode = "children";
  /**
   * 查找节点获得数据
   * @param key
   * @param value
   * @returns
   */
  findNode(data, key, value) {
    return this.findNodeLogic(data, key, value);
  }
  clear(data) {
    data.splice(0, data.length);
    return data.length;
  }
  // 根据索引删除指定对象
  removeByIndex(data, index) {
    data.splice(index, 1);
    return data.length;
  }
  // 删除指定对象
  removeByItem(data, key, value) {
    for (let i = data.length - 1; i >= 0; i--) {
      if (data[i][key] === value) {
        data.splice(i, 1);
      }
    }
    return data.length;
  }
  // 查找相同的对象
  findSame(data, key) {
    const result = [];
    const map = /* @__PURE__ */ new Map();
    for (const obj of data) {
      const val = this.getObjectPropertyValue(obj, key);
      if (map.has(val)) {
        const sameArr = map.get(val);
        sameArr.push(obj);
      } else {
        const newArr = [obj];
        map.set(val, newArr);
        result.push(newArr);
      }
    }
    return result;
  }
  // eval
  getObjectPropertyValue(obj, propertyPath) {
    const getPropertyValue = new Function("obj", `return obj.${propertyPath};`);
    return getPropertyValue(obj);
  }
  //递归查找
  findNodeLogic(data, key, value) {
    for (let i = 0; i < data.length; i++) {
      let item = data[i];
      if (this.getKeyPathValue(item, key) == value) {
        return item;
      } else {
        if (item.hasOwnProperty(this.childNode) && item[this.childNode].length > 0) {
          let target = this.findNodeLogic(item[this.childNode], key, value);
          if (target != null) {
            return target;
          }
        }
      }
    }
    return null;
  }
  /**
   * 支持多级的key路径
   * id
   * meta.path
   * @param item
   * @param key
   * @returns
   */
  getKeyPathValue(item, key) {
    const keys = key.split(".");
    let target = item;
    for (let i = 0; i < keys.length; i++) {
      if (target.hasOwnProperty(keys[i])) {
        target = target[keys[i]];
      }
    }
    return target;
  }
}
const ArrayUtil = new ArrayUtils();

export { ArrayUtil as default };
//# sourceMappingURL=array.mjs.map
