/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by 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 utils_renderNode = require('../../utils/render-node.js');
var components_tree_treeItem = require('./tree-item.js');
var components_tree_props = require('./props.js');
var components_tree_utils = require('./utils.js');
var utils_event = require('../../utils/event.js');
require('../../chunks/defineProperty-838e4192.js');
require('../../chunks/get-5e1abd10.js');
require('../../chunks/isArray-cd7357b9.js');
require('../../chunks/isSymbol-a38a5184.js');
require('../../chunks/isObjectLike-c22c7e3b.js');
require('../../chunks/_commonjsHelpers-2e6815bd.js');
require('../../chunks/isFunction-91acade7.js');
require('../../chunks/tree-node.js');
require('../../chunks/_isIndex-52887d1e.js');
require('../../chunks/log.js');
require('vue');
require('lodash/mergeWith');
require('lodash');
require('../config-provider/context.js');
require('../../chunks/zh_CN.js');
require('lodash/isFunction');
require('@ldesign/icons-vue');
require('../checkbox/index.js');
require('../../utils/map-props.js');
require('../../utils/with-install.js');
require('../../config.js');
require('../checkbox/group.js');
require('../../chunks/helper.js');
require('lodash/intersection');
require('../checkbox/checkbox.js');
require('../../utils/helper.js');
require('lodash/camelCase');
require('../checkbox/props.js');
require('../loading/index.js');
require('../loading/loading.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/icon/gradient.js');
require('../../chunks/set-style.js');
require('../../chunks/helper2.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() {
    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() {
      const list = [this.componentName];
      const {
        disabled,
        hover,
        transition,
        checkable,
        expandOnClickNode
      } = this;
      if (disabled) list.push(`${this.classPrefix}-is-disabled`);
      if (hover) list.push(`${this.componentName}--hoverable`);
      if (checkable) list.push(`${this.componentName}--checkable`);
      if (transition) list.push(`${this.componentName}--transition`);
      if (expandOnClickNode) list.push(`${this.componentName}--block-node`);
      return list;
    },
    itemMaxHeight() {
      return 42;
    },
    maxHeight() {
      return 0;
    },
    vScrollEnable() {
      return !!this.maxHeight;
    },
    nested() {
      let nested = !!this.maxHeight;
      nested = false;
      return nested;
    }
  },
  watch: {
    data(list) {
      this.rebuild(list);
    },
    value(nVal) {
      this.store.replaceChecked(nVal);
    },
    expanded(nVal) {
      this.store.replaceExpanded(nVal);
    },
    actived(nVal) {
      this.store.replaceActived(nVal);
    }
  },
  methods: {
    renderItem(node) {
      const h = this.$createElement;
      const {
        nested,
        treeScope,
        $proxyScope,
        expandOnClickNode
      } = this;
      const 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() {
      const {
        store,
        nested
      } = this;
      let nodes = [];
      if (nested) {
        nodes = store.getChildren();
      } else {
        nodes = store.getNodes();
      }
      this.renderTreeNodeViews(nodes);
    },
    renderTreeNodeViews(nodes) {
      const {
        store,
        $cacheMap
      } = this;
      this.treeNodeViews = nodes.map(node => {
        let nodeView = $cacheMap.get(node.value);
        if (!nodeView && node.visible) {
          nodeView = this.renderItem(node);
          $cacheMap.set(node.value, nodeView);
        }
        return nodeView;
      });
      this.$nextTick(() => {
        $cacheMap.forEach((view, value) => {
          if (!store.getNode(value)) $cacheMap.delete(value);
        });
      });
    },
    updateStoreConfig() {
      const {
        store
      } = this;
      if (!store) return;
      const storeProps = pick__default["default"](this, ["keys", "expandAll", "expandLevel", "expandMutex", "expandParent", "activable", "activeMultiple", "disabled", "checkable", "checkStrictly", "load", "lazy", "valueMode", "filter"]);
      store.setConfig(storeProps);
    },
    updateExpanded() {
      const {
        store,
        expanded,
        expandParent
      } = this;
      if (Array.isArray(expanded)) {
        const expandedMap = /* @__PURE__ */new Map();
        expanded.forEach(val => {
          expandedMap.set(val, true);
          if (expandParent) {
            const node = store.getNode(val);
            node.getParents().forEach(tn => {
              expandedMap.set(tn.value, true);
            });
          }
        });
        const expandedArr = Array.from(expandedMap.keys());
        store.setExpanded(expandedArr);
      }
    },
    build() {
      let list = this.data;
      const {
        actived,
        value,
        valueMode,
        filter
      } = this;
      const store = new treeStore.TreeStore({
        valueMode,
        filter,
        onLoad: info => {
          this.handleLoad(info);
        },
        onUpdate: () => {
          this.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(list) {
      this.$cacheMap.clear();
      const {
        store,
        value,
        actived
      } = this;
      store.reload(list);
      if (Array.isArray(value)) store.setChecked(value);
      this.updateExpanded();
      if (Array.isArray(actived)) store.setActived(actived);
      store.refreshState();
    },
    toggleActived(item) {
      const node = components_tree_utils.getNode(this.store, item);
      return this.setActived(node, !node.isActived());
    },
    setActived(item, isActived) {
      const node = components_tree_utils.getNode(this.store, item);
      const actived = node.setActived(isActived);
      const {
        $mouseEvent
      } = this;
      const ctx = {
        node: node.getModel(),
        e: $mouseEvent
      };
      utils_event.emitEvent(this, "active", actived, ctx);
      return actived;
    },
    toggleExpanded(item) {
      const node = components_tree_utils.getNode(this.store, item);
      return this.setExpanded(node, !node.isExpanded());
    },
    setExpanded(item, isExpanded) {
      const node = components_tree_utils.getNode(this.store, item);
      const expanded = node.setExpanded(isExpanded);
      const {
        $mouseEvent
      } = this;
      const ctx = {
        node: node.getModel(),
        e: $mouseEvent
      };
      utils_event.emitEvent(this, "expand", expanded, ctx);
      return expanded;
    },
    toggleChecked(item) {
      const node = components_tree_utils.getNode(this.store, item);
      return this.setChecked(node, !node.isChecked());
    },
    setChecked(item, isChecked) {
      const node = components_tree_utils.getNode(this.store, item);
      const checked = node.setChecked(isChecked);
      const ctx = {
        node: node.getModel()
      };
      utils_event.emitEvent(this, "change", checked, ctx);
      return checked;
    },
    handleLoad(info) {
      const {
        node
      } = info;
      const ctx = {
        node: node.getModel()
      };
      const {
        value,
        expanded,
        actived,
        store
      } = this;
      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(state) {
      const {
        expandOnClickNode
      } = this;
      const {
        mouseEvent,
        event,
        node
      } = state;
      if (!node) return;
      this.$mouseEvent = mouseEvent;
      let shouldExpand = expandOnClickNode;
      let shouldActive = !this.disabled && !node.disabled;
      ["trigger", "ignore"].forEach(markName => {
        const mark = components_tree_utils.getMark(markName, event.target, event.currentTarget);
        const markValue = (mark === null || mark === void 0 ? void 0 : mark.value) || "";
        if (markValue.includes("expand")) {
          if (markName === "trigger") shouldExpand = true;else if (markName === "ignore") shouldExpand = false;
        }
        if (markValue.includes("active")) {
          if (markName === "ignore") shouldActive = false;
        }
      });
      if (shouldExpand) this.toggleExpanded(node);
      const ctx = {
        node: node.getModel(),
        e: mouseEvent
      };
      if (shouldActive) {
        this.toggleActived(node);
        utils_event.emitEvent(this, "click", ctx);
      }
      this.$mouseEvent = null;
    },
    handleChange(state) {
      const {
        disabled
      } = this;
      const {
        node
      } = state;
      if (!node || disabled || node.disabled) return;
      this.toggleChecked(node);
    },
    updateTreeScope() {
      const {
        treeScope
      } = this;
      const scopedProps = pick__default["default"](this, ["checkProps", "disableCheck", "empty", "icon", "label", "line", "operations"]);
      Object.assign(treeScope, scopedProps);
    },
    setItem(value, options) {
      const node = this.store.getNode(value);
      const spec = options;
      const keys = Object.keys(spec);
      if (node && spec) {
        ["expanded", "actived", "checked"].forEach(name => {
          if (keys.includes(name)) {
            this[`set${upperFirst__default["default"](name)}`](node, spec[name]);
            delete spec[name];
          }
        });
        node.set(spec);
      }
    },
    getItem(value) {
      const node = this.store.getNode(value);
      return node === null || node === void 0 ? void 0 : node.getModel();
    },
    getItems(value) {
      const nodes = this.store.getNodes(value);
      return nodes.map(node => node.getModel());
    },
    appendTo(para, item) {
      const {
        store
      } = this;
      let list = [];
      if (Array.isArray(item)) list = item;else list = [item];
      list.forEach(item2 => {
        const val = (item2 === null || item2 === void 0 ? void 0 : item2.value) || "";
        const node = components_tree_utils.getNode(store, val);
        if (node) store.appendNodes(para, node);else store.appendNodes(para, item2);
      });
    },
    insertBefore(value, item) {
      const {
        store
      } = this;
      const val = (item === null || item === void 0 ? void 0 : item.value) || "";
      const node = components_tree_utils.getNode(store, val);
      if (node) store.insertBefore(value, node);else store.insertBefore(value, item);
    },
    insertAfter(value, item) {
      const {
        store
      } = this;
      const val = (item === null || item === void 0 ? void 0 : item.value) || "";
      const node = components_tree_utils.getNode(store, val);
      if (node) store.insertAfter(value, node);else store.insertAfter(value, item);
    },
    remove(value) {
      return this.store.remove(value);
    },
    getIndex(value) {
      return this.store.getNodeIndex(value);
    },
    getParent(value) {
      const node = this.store.getParent(value);
      return node === null || node === void 0 ? void 0 : node.getModel();
    },
    getParents(value) {
      const nodes = this.store.getParents(value);
      return nodes.map(node => node.getModel());
    },
    getPath(value) {
      const node = this.store.getNode(value);
      let pathNodes = [];
      if (node) pathNodes = node.getPath().map(node2 => node2.getModel());
      return pathNodes;
    }
  },
  created() {
    this.$cacheMap = /* @__PURE__ */new Map();
    this.$mouseEvent = null;
    this.$proxyScope = {};
    this.updateTreeScope();
    this.build();
  },
  render() {
    const h = arguments[0];
    const {
      classList,
      $proxyScope,
      treeNodeViews
    } = this;
    this.updateStoreConfig();
    this.updateTreeScope();
    $proxyScope.scopedSlots = this.$scopedSlots;
    let emptyNode = null;
    if (treeNodeViews.length <= 0) {
      const useLocale = !this.empty && !this.$scopedSlots.empty;
      const emptyContent = useLocale ? this.t(this.global.empty) : utils_renderNode.renderLNodeJSX(this, "empty");
      emptyNode = h("div", {
        "class": `${this.componentName}__empty`
      }, [emptyContent]);
    }
    const treeNodeList = h("transition-group", {
      "attrs": {
        "tag": "div",
        "enter-active-class": `${this.componentName}__item--enter-active`,
        "leave-active-class": `${this.componentName}__item--leave-active`
      },
      "class": `${this.componentName}__list`
    }, [treeNodeViews]);
    const treeNode = h("div", {
      "class": classList
    }, [emptyNode || treeNodeList]);
    return treeNode;
  }
});

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