/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import upperFirst from 'lodash/upperFirst';
import pick from 'lodash/pick';
import mixins from '../utils/mixins.js';
import getConfigReceiverMixins from '../config-provider/config-receiver.js';
import { TreeStore } from '../_common/js/tree/tree-store.js';
import TreeItem from './tree-item.js';
import props from './props.js';
import { renderTNodeJSX } from '../utils/render-tnode.js';
import { CLASS_NAMES, FX } from './constants.js';
import { getNode, getMark } from './util.js';
import { emitEvent } from '../utils/event.js';
import 'vue';
import '@babel/runtime/helpers/defineProperty';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../config-provider/type.js';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import 'lodash/difference';
import 'lodash/camelCase';
import 'lodash/isPlainObject';
import '../_common/js/tree/tree-node.js';
import '@babel/runtime/helpers/asyncToGenerator';
import '@babel/runtime/regenerator';
import 'lodash/uniqueId';
import 'lodash/get';
import '../_common/js/tree/tree-node-model.js';
import '../_common/js/log/log.js';
import 'lodash/isFunction';
import 'tdesign-icons-vue';
import '../checkbox/index.js';
import '../checkbox/group.js';
import '../_chunks/dep-1ec74b8b.js';
import '@babel/runtime/helpers/typeof';
import 'lodash/intersection';
import '../config.js';
import '../checkbox/checkbox.js';
import '../utils/classnames.js';
import '../checkbox/props.js';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/isObject';
import '../checkbox/checkbox-group-props.js';
import '../utils/map-props.js';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import './style/css.js';
import '../checkbox/type.js';
import '../loading/index.js';
import '../loading/loading.js';
import '../loading/icon/gradient.js';
import '../_common/js/loading/circle-adapter.js';
import '../_common/js/utils/set-style.js';
import '../_common/js/utils/helper.js';
import '../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../utils/easing.js';
import '../utils/transfer-dom.js';
import '../loading/props.js';
import '../loading/type.js';
import '../loading/plugin.js';
import '../utils/ripple.js';

var _TdTree = mixins(getConfigReceiverMixins("tree")).extend({
  name: "TTree",
  model: {
    prop: "value",
    event: "change"
  },
  props: props,
  data: function data() {
    var checkProps = this.checkProps,
        empty = this.empty,
        icon = this.icon,
        label = this.label,
        line = this.line,
        operations = this.operations;
    return {
      store: null,
      nodesMap: null,
      mouseEvent: null,
      treeNodes: [],
      treeScope: {
        checkProps: checkProps,
        empty: empty,
        icon: icon,
        label: label,
        line: line,
        operations: operations,
        scopedSlots: null
      },
      transitionCD: null
    };
  },
  computed: {
    classList: function classList() {
      var list = [CLASS_NAMES.tree];
      var disabled = this.disabled,
          hover = this.hover,
          transition = this.transition,
          checkable = this.checkable,
          expandOnClickNode = this.expandOnClickNode;

      if (disabled) {
        list.push(CLASS_NAMES.disabled);
      }

      if (hover) {
        list.push(CLASS_NAMES.treeHoverable);
      }

      if (checkable) {
        list.push(CLASS_NAMES.treeCheckable);
      }

      if (transition) {
        list.push(CLASS_NAMES.treeTransition);
      }

      if (expandOnClickNode) {
        list.push(CLASS_NAMES.treeBlockNode);
      }

      return list;
    }
  },
  watch: {
    data: function data(list) {
      this.rebuild(list);
    },
    value: function value(nVal) {
      this.store.replaceChecked(nVal);
    },
    expanded: function expanded(nVal) {
      this.store.replaceExpanded(nVal);
    },
    actived: function actived(nVal) {
      this.store.replaceActived(nVal);
    }
  },
  methods: {
    renderItem: function renderItem(node) {
      var h = this.$createElement;
      var treeScope = this.treeScope;
      var treeItem = h(TreeItem, {
        "key": node.value,
        "attrs": {
          "node": node,
          "treeScope": treeScope
        },
        "on": {
          "click": this.handleClick,
          "change": this.handleChange
        }
      });
      return treeItem;
    },
    getNodesMap: function getNodesMap() {
      var nodesMap = this.nodesMap;

      if (!nodesMap) {
        nodesMap = /* @__PURE__ */new Map();
        this.nodesMap = nodesMap;
      }

      return nodesMap;
    },
    refresh: function refresh() {
      var _this = this;

      var store = this.store;
      var nodesMap = this.getNodesMap();
      var allNodes = store.getNodes();
      var curNodesMap = /* @__PURE__ */new Map();
      this.treeNodes = allNodes.map(function (node) {
        curNodesMap.set(node.value, 1);
        var nodeView = nodesMap.get(node.value);

        if (!nodeView && node.visible) {
          nodeView = _this.renderItem(node);
          nodesMap.set(node.value, nodeView);
        }

        return nodeView;
      });
      this.$nextTick(function () {
        var keys = _toConsumableArray(nodesMap.keys());

        keys.forEach(function (value) {
          if (!curNodesMap.get(value)) {
            nodesMap["delete"](value);
          }
        });
        curNodesMap.clear();
      });
    },
    updateStoreConfig: function updateStoreConfig() {
      var store = this.store;
      if (!store) return;
      var storeProps = pick(this, ["keys", "expandAll", "expandLevel", "expandMutex", "expandParent", "activable", "activeMultiple", "disabled", "checkable", "checkStrictly", "load", "lazy", "valueMode", "filter"]);
      store.setConfig(storeProps);
    },
    updateExpanded: function updateExpanded() {
      var store = this.store,
          expanded = this.expanded,
          expandParent = this.expandParent;

      if (Array.isArray(expanded)) {
        var expandedMap = /* @__PURE__ */new Map();
        expanded.forEach(function (val) {
          expandedMap.set(val, true);

          if (expandParent) {
            var node = store.getNode(val);
            node.getParents().forEach(function (tn) {
              expandedMap.set(tn.value, true);
            });
          }
        });
        var expandedArr = Array.from(expandedMap.keys());
        store.setExpanded(expandedArr);
      }
    },
    build: function build() {
      var _this2 = this;

      var list = this.data;
      var actived = this.actived,
          value = this.value,
          valueMode = this.valueMode,
          filter = this.filter;
      var store = new TreeStore({
        valueMode: valueMode,
        filter: filter,
        onLoad: function onLoad(info) {
          _this2.handleLoad(info);
        },
        onUpdate: function onUpdate() {
          _this2.refresh();
        }
      });
      this.store = store;
      this.updateStoreConfig();

      if (!Array.isArray(list)) {
        list = [];
      }

      store.append(list);
      store.refreshNodes();

      if (Array.isArray(value)) {
        store.setChecked(value);
      }

      this.updateExpanded();

      if (Array.isArray(actived)) {
        store.setActived(actived);
      }

      this.refresh();
    },
    rebuild: function rebuild(list) {
      this.getNodesMap().clear();
      this.treeNodes.length = 0;
      var store = this.store,
          value = this.value,
          actived = this.actived;
      store.reload(list);

      if (Array.isArray(value)) {
        store.setChecked(value);
      }

      this.updateExpanded();

      if (Array.isArray(actived)) {
        store.setActived(actived);
      }

      store.refreshState();
    },
    toggleActived: function toggleActived(item) {
      var node = getNode(this.store, item);
      return this.setActived(node, !node.isActived());
    },
    setActived: function setActived(item, isActived) {
      var node = getNode(this.store, item);
      var actived = node.setActived(isActived);
      var mouseEvent = this.mouseEvent;
      var ctx = {
        node: node.getModel(),
        e: mouseEvent
      };
      emitEvent(this, "active", actived, ctx);
      return actived;
    },
    toggleExpanded: function toggleExpanded(item) {
      var node = getNode(this.store, item);
      return this.setExpanded(node, !node.isExpanded());
    },
    setExpanded: function setExpanded(item, isExpanded) {
      var node = getNode(this.store, item);
      var expanded = node.setExpanded(isExpanded);
      var mouseEvent = this.mouseEvent;
      var ctx = {
        node: node.getModel(),
        e: mouseEvent
      };
      emitEvent(this, "expand", expanded, ctx);
      return expanded;
    },
    toggleChecked: function toggleChecked(item) {
      var node = getNode(this.store, item);
      return this.setChecked(node, !node.isChecked());
    },
    setChecked: function setChecked(item, isChecked) {
      var node = getNode(this.store, item);
      var checked = node.setChecked(isChecked);
      var ctx = {
        node: node.getModel()
      };
      emitEvent(this, "change", checked, ctx);
      return checked;
    },
    handleLoad: function handleLoad(info) {
      var node = info.node;
      var ctx = {
        node: node.getModel()
      };
      var value = this.value,
          expanded = this.expanded,
          actived = this.actived,
          store = this.store;

      if (value && value.length > 0) {
        store.replaceChecked(value);
      }

      if (expanded && expanded.length > 0) {
        store.replaceExpanded(expanded);
      }

      if (actived && actived.length > 0) {
        store.replaceActived(actived);
      }

      emitEvent(this, "load", ctx);
    },
    handleClick: function handleClick(state) {
      var expandOnClickNode = this.expandOnClickNode;
      var mouseEvent = state.mouseEvent,
          event = state.event,
          node = state.node;

      if (!node || this.disabled || node.disabled) {
        return;
      }

      this.mouseEvent = mouseEvent;
      var shouldExpand = expandOnClickNode;
      var shouldActive = true;
      ["trigger", "ignore"].forEach(function (markName) {
        var mark = getMark(markName, event.target, event.currentTarget);
        var markValue = (mark === null || mark === void 0 ? void 0 : mark.value) || "";

        if (markValue.indexOf("expand") >= 0) {
          if (markName === "trigger") {
            shouldExpand = true;
          } else if (markName === "ignore") {
            shouldExpand = false;
          }
        }

        if (markValue.indexOf("active") >= 0) {
          if (markName === "ignore") {
            shouldActive = false;
          }
        }
      });

      if (shouldExpand) {
        this.toggleExpanded(node);
      }

      if (shouldActive) {
        this.toggleActived(node);
      }

      var ctx = {
        node: node.getModel(),
        e: mouseEvent
      };
      emitEvent(this, "click", ctx);
      this.mouseEvent = null;
    },
    handleChange: function handleChange(state) {
      var disabled = this.disabled;
      var node = state.node;

      if (!node || disabled || node.disabled) {
        return;
      }

      this.toggleChecked(node);
    },
    setItem: function setItem(value, options) {
      var _this3 = this;

      var node = this.store.getNode(value);
      var spec = options;
      var keys = Object.keys(spec);

      if (node && spec) {
        ["expanded", "actived", "checked"].forEach(function (name) {
          if (keys.includes(name)) {
            _this3["set".concat(upperFirst(name))](node, spec[name]);

            delete spec[name];
          }
        });
        node.set(spec);
      }
    },
    getItem: function getItem(value) {
      var node = this.store.getNode(value);
      return node === null || node === void 0 ? void 0 : node.getModel();
    },
    getItems: function getItems(value) {
      var nodes = this.store.getNodes(value);
      return nodes.map(function (node) {
        return node.getModel();
      });
    },
    appendTo: function appendTo(para, item) {
      var _this4 = this;

      var list = [];

      if (Array.isArray(item)) {
        list = item;
      } else {
        list = [item];
      }

      list.forEach(function (item2) {
        var val = (item2 === null || item2 === void 0 ? void 0 : item2.value) || "";
        var node = getNode(_this4.store, val);

        if (node) {
          _this4.store.appendNodes(para, node);
        } else {
          _this4.store.appendNodes(para, item2);
        }
      });
    },
    insertBefore: function insertBefore(value, item) {
      var val = (item === null || item === void 0 ? void 0 : item.value) || "";
      var node = getNode(this.store, val);

      if (node) {
        this.store.insertBefore(value, node);
      } else {
        this.store.insertBefore(value, item);
      }
    },
    insertAfter: function insertAfter(value, item) {
      var val = (item === null || item === void 0 ? void 0 : item.value) || "";
      var node = getNode(this.store, val);

      if (node) {
        this.store.insertAfter(value, node);
      } else {
        this.store.insertAfter(value, item);
      }
    },
    remove: function remove(value) {
      return this.store.remove(value);
    },
    getIndex: function getIndex(value) {
      return this.store.getNodeIndex(value);
    },
    getParent: function getParent(value) {
      var node = this.store.getParent(value);
      return node === null || node === void 0 ? void 0 : node.getModel();
    },
    getParents: function getParents(value) {
      var nodes = this.store.getParents(value);
      return nodes.map(function (node) {
        return node.getModel();
      });
    },
    getPath: function getPath(value) {
      var node = this.store.getNode(value);
      var pathNodes = [];

      if (node) {
        pathNodes = node.getPath().map(function (node2) {
          return node2.getModel();
        });
      }

      return pathNodes;
    }
  },
  created: function created() {
    this.build();
  },
  render: function render() {
    var h = arguments[0];
    var classList = this.classList,
        treeNodes = this.treeNodes,
        treeScope = this.treeScope,
        scopedSlots = this.$scopedSlots;
    var scopeProps = pick(this, ["checkProps", "disableCheck", "icon", "label", "line", "operations"]);
    this.updateStoreConfig();
    Object.assign(treeScope, scopeProps);
    treeScope.scopedSlots = scopedSlots;
    var emptyNode = null;
    var treeNodeList = null;

    if (treeNodes.length <= 0) {
      var useLocale = !this.empty && !this.$scopedSlots.empty;
      emptyNode = h("div", {
        "class": CLASS_NAMES.treeEmpty
      }, [useLocale ? this.t(this.global.empty) : renderTNodeJSX(this, "empty")]);
    }

    treeNodeList = h("transition-group", {
      "attrs": {
        "name": FX.treeNode,
        "tag": "div",
        "enter-active-class": CLASS_NAMES.treeNodeEnter,
        "leave-active-class": CLASS_NAMES.treeNodeLeave
      },
      "class": CLASS_NAMES.treeList
    }, [treeNodes]);
    return h("div", {
      "class": classList
    }, [emptyNode || treeNodeList]);
  }
});

export { _TdTree as default };
//# sourceMappingURL=td-tree.js.map
