/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import isFunction from 'lodash/isFunction';
import { CaretRightSmallIcon } from '@ldesign/icons-vue';
import mixins from '../../utils/mixins.js';
import getConfigReceiverMixins, { getKeepAnimationMixins, getGlobalIconMixins } from '../config-provider/config-receiver.js';
import { Checkbox } from '../checkbox/index.js';
import { Loading } from '../loading/index.js';
import Ripple from '../../utils/ripple.js';
import { getTNode } from './utils.js';
import 'vue';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';
import '../../utils/map-props.js';
import '../../utils/with-install.js';
import '../../config.js';
import '../checkbox/group.js';
import '../../chunks/helper.js';
import '../../chunks/_commonjsHelpers.js';
import 'lodash/intersection';
import '../../chunks/index.js';
import '../../chunks/log.js';
import '../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../utils/event.js';
import '../../utils/helper.js';
import 'lodash/camelCase';
import '../checkbox/checkbox.js';
import '../../utils/render-node.js';
import '../checkbox/props.js';
import '../checkbox/style/css.js';
import '../checkbox/type.js';
import '../loading/loading.js';
import '../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../utils/easing.js';
import '../../utils/transfer-dom.js';
import '../loading/props.js';
import '../loading/icon/gradient.js';
import '../../chunks/defineProperty-838e4192.js';
import '../../chunks/set-style.js';
import '../../chunks/helper2.js';
import '../loading/style/css.js';
import '../loading/type.js';
import '../loading/plugin.js';

const keepAnimationMixins = getKeepAnimationMixins();
const TreeItemProps = {
  nested: {
    type: Boolean,
    default: false
  },
  node: {
    type: Object
  },
  treeScope: {
    type: Object
  },
  proxyScope: {
    type: Object
  },
  expandOnClickNode: {
    type: Boolean
  }
};
const TreeItem = mixins(getConfigReceiverMixins("tree"), keepAnimationMixins, getGlobalIconMixins()).extend({
  name: "LTreeItem",
  props: TreeItemProps,
  components: {
    LLoading: Loading,
    LCheckbox: Checkbox
  },
  directives: {
    ripple: Ripple
  },
  data() {
    return {
      data: null,
      $clicked: false,
      $nodesMap: null
    };
  },
  methods: {
    getStyles() {
      const {
        level,
        visible
      } = this.node;
      const levelStyle = `--level: ${level};`;
      const hiddenStyle = "display:none;";
      if (visible) return levelStyle;
      return `${hiddenStyle} ${levelStyle}`;
    },
    getClassList() {
      const {
        node,
        nested
      } = this;
      const list = [];
      list.push(`${this.componentName}__item`);
      list.push({
        [`${this.componentName}__item--open`]: node.expanded,
        [`${this.classPrefix}-is-active`]: node.isActivable() ? node.actived : false,
        [`${this.classPrefix}-is-disabled`]: node.isDisabled()
      });
      if (!nested) {
        if (node.visible) list.push(`${this.componentName}__item--visible`);else list.push(`${this.componentName}__item--hidden`);
      }
      return list;
    },
    renderLine(createElement) {
      const h = this.$createElement;
      const {
        node,
        treeScope,
        proxyScope
      } = this;
      const {
        line
      } = treeScope;
      const {
        scopedSlots
      } = proxyScope;
      const iconVisible = !!treeScope.icon;
      let lineNode = null;
      if (line === true) {
        if (scopedSlots !== null && scopedSlots !== void 0 && scopedSlots.line) {
          lineNode = scopedSlots.line({
            node: node === null || node === void 0 ? void 0 : node.getModel()
          });
        } else if (node.parent && node.tree) {
          const {
            vmIsLeaf,
            vmIsFirst,
            level
          } = node;
          const lineClasses = [];
          lineClasses.push(`${this.componentName}__line`);
          if (vmIsLeaf || !iconVisible) lineClasses.push(`${this.componentName}__line--leaf`);
          if (vmIsFirst && iconVisible) lineClasses.push(`${this.componentName}__line--first`);
          const shadowStyles = [];
          const parents = node.getParents();
          parents.pop();
          parents.forEach((pnode, index) => {
            if (!pnode.vmIsLast) shadowStyles.push(`calc(-${index + 1} * var(--space)) 0 var(--color)`);
          });
          const styles = {
            "--level": level ? String(level) : void 0,
            "box-shadow": shadowStyles.join(",")
          };
          lineNode = h("span", {
            "class": lineClasses,
            "style": styles
          });
        }
      } else {
        lineNode = getTNode(line, {
          createElement,
          node
        });
      }
      return lineNode;
    },
    getFolderIcon() {
      const h = this.$createElement;
      if (isFunction(this.global.folderIcon)) return this.global.folderIcon(this.$createElement);
      const {
        CaretRightSmallIcon: CaretRightSmallIcon$1
      } = this.useGlobalIcon({
        CaretRightSmallIcon: CaretRightSmallIcon
      });
      return h(CaretRightSmallIcon$1);
    },
    renderIcon(createElement) {
      const h = this.$createElement;
      const {
        node,
        treeScope,
        proxyScope
      } = this;
      const {
        icon
      } = treeScope;
      const {
        scopedSlots
      } = proxyScope;
      let isDefaultIcon = false;
      let iconNode = null;
      if (icon === true) {
        if (scopedSlots !== null && scopedSlots !== void 0 && scopedSlots.icon) {
          iconNode = scopedSlots.icon({
            node: node === null || node === void 0 ? void 0 : node.getModel()
          });
        } else if (!node.vmIsLeaf) {
          isDefaultIcon = true;
          iconNode = this.getFolderIcon();
          if (node.loading && node.expanded) iconNode = h("l-loading");
        } else {
          iconNode = "";
        }
      } else {
        iconNode = getTNode(icon, {
          createElement,
          node
        });
      }
      iconNode = h("span", {
        "class": [`${this.componentName}__icon`, `${this.classPrefix}-folder-icon`, isDefaultIcon ? `${this.componentName}__icon--default` : ""],
        "attrs": {
          "trigger": "expand",
          "ignore": "active"
        }
      }, [iconNode]);
      return iconNode;
    },
    renderLabel(createElement) {
      const h = this.$createElement;
      const {
        node,
        treeScope,
        proxyScope,
        expandOnClickNode
      } = this;
      const {
        label,
        disableCheck
      } = treeScope;
      const {
        scopedSlots
      } = proxyScope;
      const checkProps = treeScope.checkProps || {};
      let labelNode = null;
      if (label === true) {
        if (scopedSlots !== null && scopedSlots !== void 0 && scopedSlots.label) {
          labelNode = scopedSlots.label({
            node: node === null || node === void 0 ? void 0 : node.getModel()
          });
        } else {
          labelNode = node.label || "";
        }
      } else {
        labelNode = getTNode(label, {
          createElement,
          node
        });
      }
      const labelClasses = [`${this.componentName}__label`, {
        [`${this.classPrefix}-is-active`]: node.isActivable() ? node.actived : false
      }];
      if (node.vmCheckable) {
        var _node$children;
        let checkboxDisabled = false;
        if (typeof disableCheck === "function") checkboxDisabled = disableCheck(node);else checkboxDisabled = !!disableCheck;
        if (node.isDisabled()) checkboxDisabled = true;
        const itemCheckProps = {
          ...checkProps,
          disabled: checkboxDisabled
        };
        labelNode = h("l-checkbox", {
          "directives": [{
            name: "ripple",
            value: this.keepAnimation.ripple
          }],
          "class": labelClasses,
          "attrs": {
            "checked": node.checked,
            "indeterminate": node.indeterminate,
            "disabled": node.isDisabled(),
            "name": String(node.value),
            "stopLabelTrigger": expandOnClickNode && ((_node$children = node.children) === null || _node$children === void 0 ? void 0 : _node$children.length) > 0,
            "ignore": "expand,active"
          },
          "on": {
            "change": this.handleChange
          },
          "props": {
            ...itemCheckProps
          }
        }, [labelNode]);
      } else {
        const inner = h("span", {
          "style": "position: relative"
        }, [labelNode]);
        labelNode = node.isActivable() ? h("span", {
          "key": "1",
          "directives": [{
            name: "ripple",
            value: this.keepAnimation.ripple
          }],
          "class": labelClasses
        }, [inner]) : h("span", {
          "key": "2",
          "class": labelClasses
        }, [inner]);
      }
      return labelNode;
    },
    renderOperations(createElement) {
      const h = this.$createElement;
      const {
        node,
        treeScope,
        proxyScope
      } = this;
      const {
        operations
      } = treeScope;
      const {
        scopedSlots
      } = proxyScope;
      let opNode = null;
      if (scopedSlots !== null && scopedSlots !== void 0 && scopedSlots.operations) {
        opNode = scopedSlots.operations({
          node: node === null || node === void 0 ? void 0 : node.getModel()
        });
      } else {
        opNode = getTNode(operations, {
          createElement,
          node
        });
      }
      if (opNode) {
        opNode = h("span", {
          "class": `${this.componentName}__operations`,
          "attrs": {
            "ignore": "active,expand"
          }
        }, [opNode]);
      }
      return opNode;
    },
    renderItem(createElement) {
      const itemNodes = [];
      const iconNode = this.renderIcon(createElement);
      const lineNode = this.renderLine(createElement);
      if (lineNode) itemNodes.push(lineNode);
      if (iconNode) itemNodes.push(iconNode);
      const labelNode = this.renderLabel(createElement);
      if (labelNode) itemNodes.push(labelNode);
      const opNode = this.renderOperations(createElement);
      if (opNode) itemNodes.push(opNode);
      return itemNodes;
    },
    handleClick(evt) {
      var _srcTarget$className, _srcTarget$className$;
      const srcTarget = evt.target;
      const isBranchTrigger = this.node.children && this.expandOnClickNode && (srcTarget.className === `${this.classPrefix}-checkbox__input` || srcTarget.tagName.toLowerCase() === "input");
      if (this.$clicked || isBranchTrigger) return;
      if (this.expandOnClickNode && this.node.children && ((_srcTarget$className = srcTarget.className) === null || _srcTarget$className === void 0 ? void 0 : (_srcTarget$className$ = _srcTarget$className.indexOf) === null || _srcTarget$className$ === void 0 ? void 0 : _srcTarget$className$.call(_srcTarget$className, `${this.classPrefix}-tree__label`)) !== -1) evt.preventDefault();
      this.$clicked = true;
      setTimeout(() => {
        this.$clicked = false;
      });
      const {
        node
      } = this;
      const state = {
        mouseEvent: evt,
        event: evt,
        node,
        path: node.getPath()
      };
      this.$emit("click", state);
    },
    handleChange() {
      const {
        node
      } = this;
      const event = new Event("change");
      const state = {
        event,
        node
      };
      this.$emit("change", state);
    },
    proxyClick(state) {
      this.$emit("click", state);
    },
    proxyChange(state) {
      this.$emit("change", state);
    },
    getNestedItem(node) {
      const h = this.$createElement;
      const {
        nested,
        treeScope,
        proxyScope
      } = this;
      const treeItem = h("l-tree-item", {
        "key": node.value,
        "attrs": {
          "node": node,
          "nested": nested,
          "treeScope": treeScope,
          "proxyScope": proxyScope
        },
        "on": {
          "click": this.proxyClick,
          "change": this.proxyChange
        }
      });
      return treeItem;
    },
    getChildNodes() {
      const {
        node,
        $nodesMap
      } = this;
      let children = [];
      if (Array.isArray(node.children)) children = node.children;
      const curNodesMap = /* @__PURE__ */new Map();
      const childrenNodes = children.map(child => {
        curNodesMap.set(child.value, 1);
        let nodeView = $nodesMap.get(child.value);
        if (!nodeView && child.visible) {
          nodeView = this.getNestedItem(child);
          $nodesMap.set(child.value, nodeView);
        }
        return nodeView;
      });
      this.$nextTick(() => {
        const keys = [...$nodesMap.keys()];
        keys.forEach(value => {
          if (!curNodesMap.get(value)) $nodesMap.delete(value);
        });
        curNodesMap.clear();
      });
      return childrenNodes;
    }
  },
  created() {
    const {
      node
    } = this;
    if (node) this.data = node.data;
    this.$nodesMap = /* @__PURE__ */new Map();
  },
  destroyed() {
    this.data = null;
    this.$nodesMap.clear();
  },
  render(createElement) {
    const h = arguments[0];
    const {
      node,
      nested
    } = this;
    const {
      tree,
      level,
      value
    } = node;
    if (!tree || !tree.nodeMap.get(value)) this.$destroy();
    const styles = this.getStyles();
    const classList = this.getClassList();
    const itemNode = h("div", {
      "class": classList,
      "attrs": {
        "data-value": value,
        "data-level": level
      },
      "style": styles,
      "on": {
        "click": evt => this.handleClick(evt)
      }
    }, [this.renderItem(createElement)]);
    if (!nested) {
      return itemNode;
    }
    const childNodes = this.getChildNodes();
    const childrenClassList = [];
    childrenClassList.push(`${this.componentName}__children`);
    if (node.expanded) childrenClassList.push(`${this.componentName}__children--visible`);else childrenClassList.push(`${this.componentName}__children--hidden`);
    const allChildren = node.walk();
    allChildren.shift();
    const allExpandedChildren = allChildren.filter(node2 => {
      const parent = node2.getParent();
      if (!parent) return true;
      return parent.expanded;
    });
    const childrenStyles = {
      "--hscale": allExpandedChildren.length
    };
    const childrenBox = h("transition-group", {
      "attrs": {
        "tag": "div",
        "enter-active-class": `${this.componentName}__item--enter-active`,
        "leave-active-class": `${this.componentName}__item--leave-active`
      },
      "class": childrenClassList,
      "style": childrenStyles
    }, [childNodes]);
    const branchNode = h("div", {
      "class": `${this.componentName}__branch`
    }, [itemNode, childrenBox]);
    return branchNode;
  }
});

export { TreeItemProps, TreeItem as default };
//# sourceMappingURL=tree-item.js.map
