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

'use strict';

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

var upperFirst = require('lodash/upperFirst');
var pick = require('lodash/pick');
var treeStore = require('../../chunks/tree-store.js');
var utils_mixins = require('../../utils/mixins.js');
var components_configProvider_configReceiver = require('../config-provider/config-receiver.js');
var components_tree_treeItem = require('./tree-item.js');
var components_tree_props = require('./props.js');
var utils_renderNode = require('../../utils/render-node.js');
var components_tree_utils = require('./utils.js');
var utils_event = require('../../utils/event.js');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');
require('../../chunks/defineProperty-28395935.js');
require('../../chunks/tree-node.js');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/regenerator');
require('@babel/runtime/helpers/typeof');
require('../../chunks/isArray-b0f0e351.js');
require('../../chunks/_isIndex-1bf92538.js');
require('../../chunks/isObjectLike-2443c6ff.js');
require('../../chunks/_commonjsHelpers-ea2bee21.js');
require('../../chunks/log.js');
require('vue');
require('lodash/mergeWith');
require('lodash');
require('../config-provider/context.js');
require('../../chunks/zh_CN.js');
require('@babel/runtime/helpers/defineProperty');
require('lodash/isFunction');
require('@ldesign/icons-vue');
require('../checkbox/index.js');
require('../checkbox/group.js');
require('../../chunks/helper.js');
require('lodash/intersection');
require('../checkbox/checkbox.js');
require('../checkbox/props.js');
require('../../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../utils/map-props.js');
require('../../utils/with-install.js');
require('../../config.js');
require('../loading/index.js');
require('../loading/loading.js');
require('../loading/icon/gradient.js');
require('../../chunks/set-style.js');
require('../../chunks/helper2.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"](components_configProvider_configReceiver["default"]("tree")).extend({
  name: "LTree",
  model: {
    prop: "value",
    event: "change"
  },
  components: {
    LTreeItem: components_tree_treeItem["default"]
  },
  props: components_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 = [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__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 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 = components_tree_utils.getNode(this.store, item);
      return this.setActived(node, !node.isActived());
    },
    setActived: function setActived(item, isActived) {
      var node = components_tree_utils.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 = components_tree_utils.getNode(this.store, item);
      return this.setExpanded(node, !node.isExpanded());
    },
    setExpanded: function setExpanded(item, isExpanded) {
      var node = components_tree_utils.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 = components_tree_utils.getNode(this.store, item);
      return this.setChecked(node, !node.isChecked());
    },
    setChecked: function setChecked(item, isChecked) {
      var node = components_tree_utils.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) {
        return;
      }

      this.$mouseEvent = mouseEvent;
      var shouldExpand = expandOnClickNode;
      var shouldActive = !this.disabled && !node.disabled;
      ["trigger", "ignore"].forEach(function (markName) {
        var mark = components_tree_utils.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);
        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 = components_tree_utils.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 = components_tree_utils.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 = components_tree_utils.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_renderNode.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;
  }
});

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