/**
 * tdesign v0.41.7
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var upperFirst = require('lodash/upperFirst');
var pick = require('lodash/pick');
var utils_mixins = require('../utils/mixins.js');
var configProvider_configReceiver = require('../config-provider/config-receiver.js');
var _common_js_tree_treeStore = require('../_common/js/tree/tree-store.js');
var tree_treeItem = require('./tree-item.js');
var tree_props = require('./props.js');
var utils_renderTnode = require('../utils/render-tnode.js');
var tree_constants = require('./constants.js');
var tree_util = require('./util.js');
var utils_event = require('../utils/event.js');
require('vue');
require('@babel/runtime/helpers/defineProperty');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');
require('lodash/difference');
require('lodash/camelCase');
require('lodash/isPlainObject');
require('../_common/js/tree/tree-node.js');
require('@babel/runtime/helpers/asyncToGenerator');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/regenerator');
require('lodash/uniqueId');
require('lodash/get');
require('../_common/js/tree/tree-node-model.js');
require('../_common/js/log/log.js');
require('lodash/isFunction');
require('tdesign-icons-vue');
require('../checkbox/index.js');
require('../checkbox/group.js');
require('../_chunks/dep-99705d51.js');
require('@babel/runtime/helpers/typeof');
require('lodash/intersection');
require('../config.js');
require('../checkbox/checkbox.js');
require('../utils/classnames.js');
require('../checkbox/props.js');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('../checkbox/checkbox-group-props.js');
require('../utils/map-props.js');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('../loading/index.js');
require('../loading/loading.js');
require('../loading/icon/gradient.js');
require('../_common/js/loading/circle-adapter.js');
require('../_common/js/utils/set-style.js');
require('../_common/js/utils/helper.js');
require('../utils/dom.js');
require('raf');
require('lodash/isString');
require('../utils/easing.js');
require('../utils/transfer-dom.js');
require('../loading/props.js');
require('../loading/plugin.js');
require('../utils/ripple.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var upperFirst__default = /*#__PURE__*/_interopDefaultLegacy(upperFirst);
var pick__default = /*#__PURE__*/_interopDefaultLegacy(pick);

var _TdTree = utils_mixins["default"](configProvider_configReceiver["default"]("tree")).extend({
  name: "TTree",
  model: {
    prop: "value",
    event: "change"
  },
  props: tree_props["default"],
  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 = [tree_constants.CLASS_NAMES.tree];
      var disabled = this.disabled,
          hover = this.hover,
          transition = this.transition,
          checkable = this.checkable,
          expandOnClickNode = this.expandOnClickNode;

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

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

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

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

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

      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;
      var treeItem = h(tree_treeItem["default"], {
        "key": node.value,
        "attrs": {
          "node": node,
          "nested": nested,
          "treeScope": treeScope,
          "proxyScope": $proxyScope
        },
        "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__default["default"](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 _common_js_tree_treeStore.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 = tree_util.getNode(this.store, item);
      return this.setActived(node, !node.isActived());
    },
    setActived: function setActived(item, isActived) {
      var node = tree_util.getNode(this.store, item);
      var actived = node.setActived(isActived);
      var $mouseEvent = this.$mouseEvent;
      var ctx = {
        node: node.getModel(),
        e: $mouseEvent
      };
      utils_event.emitEvent(this, "active", actived, ctx);
      return actived;
    },
    toggleExpanded: function toggleExpanded(item) {
      var node = tree_util.getNode(this.store, item);
      return this.setExpanded(node, !node.isExpanded());
    },
    setExpanded: function setExpanded(item, isExpanded) {
      var node = tree_util.getNode(this.store, item);
      var expanded = node.setExpanded(isExpanded);
      var $mouseEvent = this.$mouseEvent;
      var ctx = {
        node: node.getModel(),
        e: $mouseEvent
      };
      utils_event.emitEvent(this, "expand", expanded, ctx);
      return expanded;
    },
    toggleChecked: function toggleChecked(item) {
      var node = tree_util.getNode(this.store, item);
      return this.setChecked(node, !node.isChecked());
    },
    setChecked: function setChecked(item, isChecked) {
      var node = tree_util.getNode(this.store, item);
      var checked = node.setChecked(isChecked);
      var ctx = {
        node: node.getModel()
      };
      utils_event.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);
      }

      utils_event.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 = tree_util.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
      };
      utils_event.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__default["default"](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__default["default"](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 = tree_util.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 = tree_util.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 = tree_util.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) : utils_renderTnode.renderTNodeJSX(this, "empty");
      emptyNode = h("div", {
        "class": tree_constants.CLASS_NAMES.treeEmpty
      }, [emptyContent]);
    }

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

exports["default"] = _TdTree;
//# sourceMappingURL=td-tree.js.map
