/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

'use strict';

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

var upperFirst = require('lodash/upperFirst');
var isFunction = require('lodash/isFunction');
var VueCompositionAPI = require('@vue/composition-api');
var hooks_useConfig = require('../../hooks/useConfig.js');
var utils_renderNode = require('../../utils/render-node.js');
var components_tree_props = require('./props.js');
var components_tree_hooks_useTreeStore = require('./hooks/useTreeStore.js');
var components_tree_hooks_useCache = require('./hooks/useCache.js');
var components_tree_hooks_useTreeAction = require('./hooks/useTreeAction.js');
var components_tree_hooks_useTreeNodes = require('./hooks/useTreeNodes.js');
var components_tree_hooks_useDragHandle = require('./hooks/useDragHandle.js');
var components_tree_utils = require('./utils.js');
var components_configProvider_useConfig = require('../config-provider/useConfig.js');
require('vue');
require('lodash');
require('lodash/pick');
require('../../chunks/tree-store.js');
require('../../chunks/defineProperty-19da3d4c.js');
require('../../chunks/get-fb5b05c8.js');
require('../../chunks/isArray-6b6e7094.js');
require('../../chunks/isSymbol-0ea4cedf.js');
require('../../chunks/isObjectLike-51896b77.js');
require('../../chunks/_commonjsHelpers-fe796db6.js');
require('../../chunks/isFunction-cb08cfdf.js');
require('../../chunks/tree-node.js');
require('../../chunks/log.js');
require('../../chunks/_isIndex-8b06cfbb.js');
require('./tree-item.js');
require('../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../../chunks/zh_CN.js');
require('../../utils/ripple.js');
require('../../chunks/set-style.js');
require('../../config.js');
require('./hooks/useTreeItem.js');
require('./hooks/useItemEvents.js');
require('./hooks/useRenderIcon.js');
require('@ldesign/icons-vue');
require('../../hooks/useGlobalIcon.js');
require('../loading/index.js');
require('../../utils/withInstall.js');
require('lodash/capitalize');
require('../loading/loading.js');
require('../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../utils/easing.js');
require('../../utils/transfer-dom.js');
require('../../utils/mixins.js');
require('../loading/props.js');
require('../loading/icon/gradient.js');
require('../../chunks/helper2.js');
require('../loading/plugin.js');
require('./hooks/useRenderLabel.js');
require('../checkbox/index.js');
require('../../utils/map-props.js');
require('../checkbox/group.js');
require('../../chunks/helper.js');
require('lodash/intersection');
require('../../utils/event.js');
require('../../utils/helper.js');
require('lodash/camelCase');
require('../checkbox/checkbox.js');
require('../checkbox/props.js');
require('./hooks/useRenderLine.js');
require('./hooks/useRenderOperations.js');
require('./hooks/useDraggable.js');
require('lodash/throttle');
require('./hooks/useTreeEvents.js');

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

var upperFirst__default = /*#__PURE__*/_interopDefaultLegacy(upperFirst);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);

var _TdTree = VueCompositionAPI.defineComponent({
  name: "LTree",
  model: {
    prop: "value",
    event: "change"
  },
  props: components_tree_props["default"],
  setup(props2, context) {
    const {
      t,
      global
    } = components_configProvider_useConfig.useConfig("tree");
    const classPrefix = hooks_useConfig.usePrefixClass();
    const componentName = hooks_useConfig.usePrefixClass("tree");
    const refProps = VueCompositionAPI.toRefs(props2);
    const {
      store,
      rebuild,
      updateStoreConfig
    } = components_tree_hooks_useTreeStore["default"](props2, context);
    const {
      cache
    } = components_tree_hooks_useCache["default"](props2);
    const classList = VueCompositionAPI.computed(() => {
      const cname = componentName.value;
      const list = [cname];
      const {
        disabled,
        hover,
        transition,
        checkable,
        draggable,
        expandOnClickNode
      } = props2;
      if (disabled) list.push(`${classPrefix.value}-is-disabled`);
      if (hover) list.push(`${cname}--hoverable`);
      if (checkable) list.push(`${cname}--checkable`);
      if (draggable) list.push(`${cname}--draggable`);
      if (transition) list.push(`${cname}--transition`);
      if (expandOnClickNode) list.push(`${cname}--block-node`);
      return list;
    });
    const state = {
      store,
      cache
    };
    components_tree_hooks_useDragHandle["default"](props2, context, state);
    const {
      setActived,
      setExpanded,
      setChecked
    } = components_tree_hooks_useTreeAction["default"](props2, context, state);
    const {
      renderTreeNodes,
      clearCacheNodes,
      nodesFilterEmpty
    } = components_tree_hooks_useTreeNodes["default"](props2, context, state);
    VueCompositionAPI.watch(refProps.data, list => {
      clearCacheNodes();
      rebuild(list);
    });
    VueCompositionAPI.watch(refProps.keys, keys => {
      store.setConfig({
        keys
      });
    });
    VueCompositionAPI.watch(refProps.value, nVal => {
      store.replaceChecked(nVal);
    });
    VueCompositionAPI.watch(refProps.expanded, nVal => {
      store.replaceExpanded(nVal);
    });
    VueCompositionAPI.watch(refProps.actived, nVal => {
      store.replaceActived(nVal);
    });
    return {
      t,
      global,
      classPrefix,
      componentName,
      store,
      cache,
      classList,
      updateStoreConfig,
      setActived,
      setExpanded,
      setChecked,
      renderTreeNodes,
      nodesFilterEmpty
    };
  },
  methods: {
    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)) {
            const setupMethod = this[`set${upperFirst__default["default"](name)}`];
            if (isFunction__default["default"](setupMethod)) setupMethod(node, spec[name]);
            delete spec[name];
          }
        });
        node.set(spec);
      }
    },
    getItem(value) {
      const node = this.store.getNode(value);
      return 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?.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?.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?.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?.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;
    }
  },
  render(h) {
    const {
      cache,
      classList,
      updateStoreConfig,
      renderTreeNodes,
      nodesFilterEmpty
    } = this;
    updateStoreConfig();
    const {
      scope
    } = cache;
    scope.scopedSlots = this.$scopedSlots;
    const treeNodeViews = renderTreeNodes(h);
    const cname = this.componentName;
    let emptyNode = null;
    if (treeNodeViews.length <= 0 || nodesFilterEmpty) {
      const useLocale = !this.empty && !this.$scopedSlots.empty;
      const emptyContent = useLocale ? this.t(this.global.empty) : utils_renderNode.renderLNodeJSX(this, "empty");
      emptyNode = h("div", {
        "class": `${cname}__empty`
      }, [emptyContent]);
    }
    const treeNodeList = h("transition-group", {
      "attrs": {
        "tag": "div",
        "enter-active-class": `${cname}__item--enter-active`,
        "leave-active-class": `${cname}__item--leave-active`
      },
      "class": `${cname}__list`
    }, [treeNodeViews]);
    const treeNode = h("div", {
      "class": classList
    }, [emptyNode || treeNodeList]);
    return treeNode;
  }
});

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