/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

import upperFirst from 'lodash/upperFirst';
import pick from 'lodash/pick';
import { T as TreeStore } from '../../_chunks/dep-360db93c.js';
import mixins from '../../utils/mixins.js';
import getConfigReceiverMixins from '../config-provider/config-receiver.js';
import TreeItem from './tree-item.js';
import props from './props.js';
import { renderLNodeJSX } from '../../utils/render-node.js';
import { getNode, getMark } from './utils.js';
import { emitEvent } from '../../utils/event.js';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import '../../_chunks/dep-33b15d9d.js';
import '../../_chunks/dep-40125abf.js';
import '@babel/runtime/helpers/toConsumableArray';
import '@babel/runtime/regenerator';
import '@babel/runtime/helpers/typeof';
import '../../_chunks/dep-d204d516.js';
import '../../_chunks/dep-64cbbe92.js';
import '../../_chunks/dep-5c5da9dd.js';
import '../../_chunks/dep-53d4956d.js';
import '../../_chunks/dep-a65fe6e9.js';
import 'vue';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../_chunks/dep-a0f2090c.js';
import '@babel/runtime/helpers/defineProperty';
import 'lodash/isFunction';
import '@ldesign/icons-vue';
import '../checkbox/index.js';
import '../checkbox/group.js';
import '../../_chunks/dep-b13ef886.js';
import 'lodash/intersection';
import '../checkbox/checkbox.js';
import '../checkbox/props.js';
import '../../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../utils/map-props.js';
import '../../utils/with-install.js';
import '../../config.js';
import '../loading/index.js';
import '../loading/loading.js';
import '../loading/icon/gradient.js';
import '../../_chunks/dep-85e8b23b.js';
import '../../_chunks/dep-fc383775.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/plugin.js';
import '../../utils/ripple.js';

var _TdTree = mixins(getConfigReceiverMixins("tree")).extend({
  name: "LTree",
  model: {
    prop: "value",
    event: "change"
  },
  components: {
    LTreeItem: TreeItem
  },
  props: props,
  data: function data() {
    return {
      store: null,
      treeNodeViews: [],
      treeScope: {
        checkProps: null,
        disableCheck: false,
        empty: null,
        icon: null,
        label: null,
        line: null,
        operations: null
      },
      $cacheMap: null,
      $mouseEvent: null,
      $proxyScope: null
    };
  },
  computed: {
    classList: function classList() {
      var list = [this.componentName];
      var disabled = this.disabled,
          hover = this.hover,
          transition = this.transition,
          checkable = this.checkable,
          expandOnClickNode = this.expandOnClickNode;

      if (disabled) {
        list.push("".concat(this.classPrefix, "-is-disabled"));
      }

      if (hover) {
        list.push("".concat(this.componentName, "--hoverable"));
      }

      if (checkable) {
        list.push("".concat(this.componentName, "--checkable"));
      }

      if (transition) {
        list.push("".concat(this.componentName, "--transition"));
      }

      if (expandOnClickNode) {
        list.push("".concat(this.componentName, "--block-node"));
      }

      return list;
    },
    itemMaxHeight: function itemMaxHeight() {
      return 42;
    },
    maxHeight: function maxHeight() {
      return 0;
    },
    vScrollEnable: function vScrollEnable() {
      return !!this.maxHeight;
    },
    nested: function nested() {
      var nested = !!this.maxHeight;
      nested = false;
      return nested;
    }
  },
  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 nested = this.nested,
          treeScope = this.treeScope,
          $proxyScope = this.$proxyScope,
          expandOnClickNode = this.expandOnClickNode;
      var treeItem = h("l-tree-item", {
        "key": node.value,
        "attrs": {
          "node": node,
          "nested": nested,
          "treeScope": treeScope,
          "proxyScope": $proxyScope,
          "expandOnClickNode": expandOnClickNode
        },
        "on": {
          "click": this.handleClick,
          "change": this.handleChange
        }
      });
      return treeItem;
    },
    refresh: function refresh() {
      var store = this.store,
          nested = this.nested;
      var nodes = [];

      if (nested) {
        nodes = store.getChildren();
      } else {
        nodes = store.getNodes();
      }

      this.renderTreeNodeViews(nodes);
    },
    renderTreeNodeViews: function renderTreeNodeViews(nodes) {
      var _this = this;

      var store = this.store,
          $cacheMap = this.$cacheMap;
      this.treeNodeViews = nodes.map(function (node) {
        var nodeView = $cacheMap.get(node.value);

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

        return nodeView;
      });
      this.$nextTick(function () {
        $cacheMap.forEach(function (view, value) {
          if (!store.getNode(value)) {
            $cacheMap["delete"](value);
          }
        });
      });
    },
    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.$cacheMap.clear();
      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) {
        return;
      }

      this.$mouseEvent = mouseEvent;
      var shouldExpand = expandOnClickNode;
      var shouldActive = !this.disabled && !node.disabled;
      ["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);
      }

      var ctx = {
        node: node.getModel(),
        e: mouseEvent
      };

      if (shouldActive) {
        this.toggleActived(node);
        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);
    },
    updateTreeScope: function updateTreeScope() {
      var treeScope = this.treeScope;
      var scopedProps = pick(this, ["checkProps", "disableCheck", "empty", "icon", "label", "line", "operations"]);
      Object.assign(treeScope, scopedProps);
    },
    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 store = this.store;
      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(store, val);

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

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

      if (node) {
        store.insertAfter(value, node);
      } else {
        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.$cacheMap = /* @__PURE__ */new Map();
    this.$mouseEvent = null;
    this.$proxyScope = {};
    this.updateTreeScope();
    this.build();
  },
  render: function render() {
    var h = arguments[0];
    var classList = this.classList,
        $proxyScope = this.$proxyScope,
        treeNodeViews = this.treeNodeViews;
    this.updateStoreConfig();
    this.updateTreeScope();
    $proxyScope.scopedSlots = this.$scopedSlots;
    var emptyNode = null;

    if (treeNodeViews.length <= 0) {
      var useLocale = !this.empty && !this.$scopedSlots.empty;
      var emptyContent = useLocale ? this.t(this.global.empty) : renderLNodeJSX(this, "empty");
      emptyNode = h("div", {
        "class": "".concat(this.componentName, "__empty")
      }, [emptyContent]);
    }

    var treeNodeList = h("transition-group", {
      "attrs": {
        "tag": "div",
        "enter-active-class": "".concat(this.componentName, "__item--enter-active"),
        "leave-active-class": "".concat(this.componentName, "__item--leave-active")
      },
      "class": "".concat(this.componentName, "__list")
    }, [treeNodeViews]);
    var treeNode = h("div", {
      "class": classList
    }, [emptyNode || treeNodeList]);
    return treeNode;
  }
});

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