"use strict";
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
const is = require("../_utils/is.js");
const DFS = (options, parentNode, depth) => {
  for (let i = 0, len = options.length; i < len; i++) {
    const item = options[i];
    let node = {
      value: item.value,
      label: item.label,
      parent: parentNode,
      depth: depth + 1,
      children: [],
      isLeaf: true
    };
    parentNode.children.push(node);
    if (item.children && item.children.length) {
      node.isLeaf = false;
      DFS(item.children, node, depth + 1);
    }
  }
};
class CascaderTree {
  constructor() {
    __publicField(this, "root");
    this.root = {
      value: NaN,
      label: "",
      depth: 0,
      parent: null,
      children: [],
      isLeaf: true
    };
  }
  updateTree(options) {
    this.root = {
      value: NaN,
      label: "",
      depth: 0,
      parent: null,
      children: [],
      isLeaf: true
    };
    DFS(options, this.root, 0);
  }
  getNode(node, val) {
    if (node.value === val) {
      return node;
    }
    const children = node.children;
    for (let i = 0, len = children.length; i < len; i++) {
      const rlt = this.getNode(children[i], val);
      if (rlt) {
        return rlt;
      }
    }
  }
  getChild(node, val) {
    const children = node.children;
    return children.find((item) => item.value === val);
  }
  getPanelInfo(val) {
    let rlt = [];
    if (is.isUndefined(val)) {
      return rlt;
    }
    if (!is.isArray(val)) {
      let node = this.getNode(this.root, val);
      if (is.isUndefined(node) || !node.isLeaf) {
        const columnInfo = this.getNextColumnInfo(this.root);
        if (!is.isUndefined(columnInfo)) {
          rlt = [columnInfo];
        }
      } else {
        while (node.parent) {
          const columnInfo = this.getNextColumnInfo(node.parent, node.value);
          if (!is.isUndefined(columnInfo)) {
            rlt.unshift(columnInfo);
          }
          node = node.parent;
        }
      }
    } else {
      let parent = this.root;
      for (let i = 0, len = val.length; i < len; i++) {
        const child = this.getChild(parent, val[i]);
        if (is.isUndefined(child) || !child.isLeaf && child.depth === len) {
          const columnInfo = this.getNextColumnInfo(this.root);
          if (!is.isUndefined(columnInfo)) {
            rlt = [columnInfo];
          }
          break;
        } else {
          const columnInfo = this.getNextColumnInfo(parent, val[i]);
          rlt.push(columnInfo);
          parent = child;
        }
      }
    }
    return rlt;
  }
  getNextColumnInfo(node, activeVal) {
    return node.children.map((item) => {
      const rlt = {
        value: item.value,
        label: item.label,
        depth: item.depth,
        isActive: false,
        isLeaf: item.children && item.children.length ? false : true
      };
      if (!is.isUndefined(activeVal)) {
        rlt.isActive = item.value === activeVal;
      }
      return rlt;
    });
  }
}
module.exports = CascaderTree;
