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

import upperFirst from 'lodash/upperFirst';
import isFunction from 'lodash/isFunction';
import { defineComponent, toRefs, computed, watch } from '@vue/composition-api';
import { usePrefixClass } from '../../hooks/useConfig.js';
import { renderLNodeJSX } from '../../utils/render-node.js';
import props from './props.js';
import useTreeStore from './hooks/useTreeStore.js';
import useCache from './hooks/useCache.js';
import useTreeAction from './hooks/useTreeAction.js';
import useTreeNodes from './hooks/useTreeNodes.js';
import useDragHandle from './hooks/useDragHandle.js';
import { getNode } from './utils.js';
import { useConfig } from '../config-provider/useConfig.js';
import 'vue';
import 'lodash';
import 'lodash/pick';
import '../../chunks/tree-store.js';
import '../../chunks/defineProperty-19da3d4c.js';
import '../../chunks/get-fb5b05c8.js';
import '../../chunks/isArray-6b6e7094.js';
import '../../chunks/isSymbol-0ea4cedf.js';
import '../../chunks/isObjectLike-51896b77.js';
import '../../chunks/_commonjsHelpers-fe796db6.js';
import '../../chunks/isFunction-cb08cfdf.js';
import '../../chunks/tree-node.js';
import '../../chunks/log.js';
import '../../chunks/_isIndex-8b06cfbb.js';
import './tree-item.js';
import '../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import 'lodash/merge';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';
import '../../utils/ripple.js';
import '../../chunks/set-style.js';
import '../../config.js';
import './hooks/useTreeItem.js';
import './hooks/useItemEvents.js';
import './hooks/useRenderIcon.js';
import '@ldesign/icons-vue';
import '../../hooks/useGlobalIcon.js';
import '../loading/index.js';
import '../../utils/withInstall.js';
import 'lodash/capitalize';
import '../loading/loading.js';
import '../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../chunks/index.js';
import '../../utils/easing.js';
import '../../utils/transfer-dom.js';
import '../../utils/mixins.js';
import '../loading/props.js';
import '../loading/icon/gradient.js';
import '../../chunks/helper2.js';
import '../loading/style/css.js';
import '../loading/type.js';
import '../loading/plugin.js';
import './hooks/useRenderLabel.js';
import '../checkbox/index.js';
import '../../utils/map-props.js';
import '../checkbox/group.js';
import '../../chunks/helper.js';
import '../../chunks/_commonjsHelpers.js';
import 'lodash/intersection';
import '../../utils/event.js';
import '../../utils/helper.js';
import 'lodash/camelCase';
import '../checkbox/checkbox.js';
import '../checkbox/props.js';
import '../checkbox/style/css.js';
import '../checkbox/type.js';
import './hooks/useRenderLine.js';
import './hooks/useRenderOperations.js';
import './hooks/useDraggable.js';
import 'lodash/throttle';
import './hooks/useTreeEvents.js';

var _TdTree = defineComponent({
  name: "LTree",
  model: {
    prop: "value",
    event: "change"
  },
  props,
  setup(props2, context) {
    const {
      t,
      global
    } = useConfig("tree");
    const classPrefix = usePrefixClass();
    const componentName = usePrefixClass("tree");
    const refProps = toRefs(props2);
    const {
      store,
      rebuild,
      updateStoreConfig
    } = useTreeStore(props2, context);
    const {
      cache
    } = useCache(props2);
    const classList = 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
    };
    useDragHandle(props2, context, state);
    const {
      setActived,
      setExpanded,
      setChecked
    } = useTreeAction(props2, context, state);
    const {
      renderTreeNodes,
      clearCacheNodes,
      nodesFilterEmpty
    } = useTreeNodes(props2, context, state);
    watch(refProps.data, list => {
      clearCacheNodes();
      rebuild(list);
    });
    watch(refProps.keys, keys => {
      store.setConfig({
        keys
      });
    });
    watch(refProps.value, nVal => {
      store.replaceChecked(nVal);
    });
    watch(refProps.expanded, nVal => {
      store.replaceExpanded(nVal);
    });
    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(name)}`];
            if (isFunction(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 = 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 = 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 = 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) : 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;
  }
});

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