/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import isFunction from 'lodash/isFunction';
import { CaretRightSmallIcon } from 'tdesign-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 { getTNode } from './util.js';
import Ripple from '../utils/ripple.js';
import 'vue';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../config-provider/type.js';
import '../checkbox/group.js';
import '../_chunks/dep-a4bd011a.js';
import '@babel/runtime/helpers/typeof';
import 'lodash/intersection';
import '../checkbox/checkbox.js';
import '../utils/render-tnode.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../checkbox/props.js';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import '../utils/event.js';
import '../checkbox/checkbox-group-props.js';
import '../utils/map-props.js';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import './style/css.js';
import '../checkbox/type.js';
import '../loading/loading.js';
import '../loading/icon/gradient.js';
import '../_common/js/loading/circle-adapter.js';
import '../_common/js/utils/set-style.js';
import '../_common/js/utils/helper.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/type.js';
import '../loading/plugin.js';
import '../config.js';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var keepAnimationMixins = getKeepAnimationMixins();
var TreeItemProps = {
  nested: {
    type: Boolean,
    "default": false
  },
  node: {
    type: Object
  },
  treeScope: {
    type: Object
  },
  proxyScope: {
    type: Object
  },
  expandOnClickNode: {
    type: Boolean
  }
};
var TreeItem = mixins(getConfigReceiverMixins("tree"), keepAnimationMixins, getGlobalIconMixins()).extend({
  name: "TTreeItem",
  props: TreeItemProps,
  directives: {
    ripple: Ripple
  },
  data: function data() {
    return {
      data: null,
      $clicked: false,
      $nodesMap: null
    };
  },
  methods: {
    getStyles: function getStyles() {
      var _this$node = this.node,
          level = _this$node.level,
          visible = _this$node.visible;
      var levelStyle = "--level: ".concat(level, ";");
      var hiddenStyle = "display:none;";
      if (visible) return levelStyle;
      return "".concat(hiddenStyle, " ").concat(levelStyle);
    },
    getClassList: function getClassList() {
      var _list$push;

      var node = this.node,
          nested = this.nested;
      var list = [];
      list.push("".concat(this.componentName, "__item"));
      list.push((_list$push = {}, _defineProperty(_list$push, "".concat(this.componentName, "__item--open"), node.expanded), _defineProperty(_list$push, "".concat(this.classPrefix, "-is-active"), node.isActivable() ? node.actived : false), _defineProperty(_list$push, "".concat(this.classPrefix, "-is-disabled"), node.isDisabled()), _list$push));

      if (!nested) {
        if (node.visible) {
          list.push("".concat(this.componentName, "__item--visible"));
        } else {
          list.push("".concat(this.componentName, "__item--hidden"));
        }
      }

      return list;
    },
    renderLine: function renderLine(createElement) {
      var h = this.$createElement;
      var node = this.node,
          treeScope = this.treeScope,
          proxyScope = this.proxyScope;
      var line = treeScope.line;
      var scopedSlots = proxyScope.scopedSlots;
      var iconVisible = !!treeScope.icon;
      var 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) {
          var vmIsLeaf = node.vmIsLeaf,
              vmIsFirst = node.vmIsFirst,
              level = node.level;
          var lineClasses = [];
          lineClasses.push("".concat(this.componentName, "__line"));

          if (vmIsLeaf || !iconVisible) {
            lineClasses.push("".concat(this.componentName, "__line--leaf"));
          }

          if (vmIsFirst && iconVisible) {
            lineClasses.push("".concat(this.componentName, "__line--first"));
          }

          var shadowStyles = [];
          var parents = node.getParents();
          parents.pop();
          parents.forEach(function (pnode, index) {
            if (!pnode.vmIsLast) {
              shadowStyles.push("calc(-".concat(index + 1, " * var(--space)) 0 var(--color)"));
            }
          });
          var styles = {
            "--level": level ? String(level) : void 0,
            "box-shadow": shadowStyles.join(",")
          };
          lineNode = h("span", {
            "class": lineClasses,
            "style": styles
          });
        }
      } else {
        lineNode = getTNode(line, {
          createElement: createElement,
          node: node
        });
      }

      return lineNode;
    },
    getFolderIcon: function getFolderIcon() {
      var h = this.$createElement;

      if (isFunction(this.global.folderIcon)) {
        return this.global.folderIcon(this.$createElement);
      }

      var _this$useGlobalIcon = this.useGlobalIcon({
        CaretRightSmallIcon: CaretRightSmallIcon
      }),
          CaretRightSmallIcon$1 = _this$useGlobalIcon.CaretRightSmallIcon;

      return h(CaretRightSmallIcon$1);
    },
    renderIcon: function renderIcon(createElement) {
      var h = this.$createElement;
      var node = this.node,
          treeScope = this.treeScope,
          proxyScope = this.proxyScope;
      var icon = treeScope.icon;
      var scopedSlots = proxyScope.scopedSlots;
      var isDefaultIcon = false;
      var 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(Loading);
          }
        } else {
          iconNode = "";
        }
      } else {
        iconNode = getTNode(icon, {
          createElement: createElement,
          node: node
        });
      }

      iconNode = h("span", {
        "class": ["".concat(this.componentName, "__icon"), "".concat(this.classPrefix, "-folder-icon"), isDefaultIcon ? "".concat(this.componentName, "__icon--default") : ""],
        "attrs": {
          "trigger": "expand",
          "ignore": "active"
        }
      }, [iconNode]);
      return iconNode;
    },
    renderLabel: function renderLabel(createElement) {
      var h = this.$createElement;
      var node = this.node,
          treeScope = this.treeScope,
          proxyScope = this.proxyScope,
          expandOnClickNode = this.expandOnClickNode;
      var label = treeScope.label,
          disableCheck = treeScope.disableCheck;
      var scopedSlots = proxyScope.scopedSlots;
      var checkProps = treeScope.checkProps || {};
      var 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: createElement,
          node: node
        });
      }

      var labelClasses = ["".concat(this.componentName, "__label"), _defineProperty({}, "".concat(this.classPrefix, "-is-active"), node.isActivable() ? node.actived : false)];

      if (node.vmCheckable) {
        var _node$children;

        var checkboxDisabled = false;

        if (typeof disableCheck === "function") {
          checkboxDisabled = disableCheck(node);
        } else {
          checkboxDisabled = !!disableCheck;
        }

        if (node.isDisabled()) {
          checkboxDisabled = true;
        }

        var itemCheckProps = _objectSpread(_objectSpread({}, checkProps), {}, {
          disabled: checkboxDisabled
        });

        labelNode = h(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": _objectSpread({}, itemCheckProps)
        }, [labelNode]);
      } else {
        var 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: function renderOperations(createElement) {
      var h = this.$createElement;
      var node = this.node,
          treeScope = this.treeScope,
          proxyScope = this.proxyScope;
      var operations = treeScope.operations;
      var scopedSlots = proxyScope.scopedSlots;
      var 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: createElement,
          node: node
        });
      }

      if (opNode) {
        opNode = h("span", {
          "class": "".concat(this.componentName, "__operations"),
          "attrs": {
            "ignore": "active,expand"
          }
        }, [opNode]);
      }

      return opNode;
    },
    renderItem: function renderItem(createElement) {
      var itemNodes = [];
      var iconNode = this.renderIcon(createElement);
      var lineNode = this.renderLine(createElement);

      if (lineNode) {
        itemNodes.push(lineNode);
      }

      if (iconNode) {
        itemNodes.push(iconNode);
      }

      var labelNode = this.renderLabel(createElement);

      if (labelNode) {
        itemNodes.push(labelNode);
      }

      var opNode = this.renderOperations(createElement);

      if (opNode) {
        itemNodes.push(opNode);
      }

      return itemNodes;
    },
    handleClick: function handleClick(evt) {
      var _srcTarget$className,
          _srcTarget$className$,
          _this = this;

      var srcTarget = evt.target;
      var isBranchTrigger = this.node.children && this.expandOnClickNode && (srcTarget.className === "".concat(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, "".concat(this.classPrefix, "-tree__label"))) !== -1) evt.preventDefault();
      this.$clicked = true;
      setTimeout(function () {
        _this.$clicked = false;
      });
      var node = this.node;
      var state = {
        mouseEvent: evt,
        event: evt,
        node: node,
        path: node.getPath()
      };
      this.$emit("click", state);
    },
    handleChange: function handleChange() {
      var node = this.node;
      var event = new Event("change");
      var state = {
        event: event,
        node: node
      };
      this.$emit("change", state);
    },
    proxyClick: function proxyClick(state) {
      this.$emit("click", state);
    },
    proxyChange: function proxyChange(state) {
      this.$emit("change", state);
    },
    getNestedItem: function getNestedItem(node) {
      var h = this.$createElement;
      var nested = this.nested,
          treeScope = this.treeScope,
          proxyScope = this.proxyScope;
      var treeItem = h(TreeItem, {
        "key": node.value,
        "attrs": {
          "node": node,
          "nested": nested,
          "treeScope": treeScope,
          "proxyScope": proxyScope
        },
        "on": {
          "click": this.proxyClick,
          "change": this.proxyChange
        }
      });
      return treeItem;
    },
    getChildNodes: function getChildNodes() {
      var _this2 = this;

      var node = this.node,
          $nodesMap = this.$nodesMap;
      var children = [];

      if (Array.isArray(node.children)) {
        children = node.children;
      }

      var curNodesMap = /* @__PURE__ */new Map();
      var childrenNodes = children.map(function (child) {
        curNodesMap.set(child.value, 1);
        var nodeView = $nodesMap.get(child.value);

        if (!nodeView && child.visible) {
          nodeView = _this2.getNestedItem(child);
          $nodesMap.set(child.value, nodeView);
        }

        return nodeView;
      });
      this.$nextTick(function () {
        var keys = _toConsumableArray($nodesMap.keys());

        keys.forEach(function (value) {
          if (!curNodesMap.get(value)) {
            $nodesMap["delete"](value);
          }
        });
        curNodesMap.clear();
      });
      return childrenNodes;
    }
  },
  created: function created() {
    var node = this.node;

    if (node) {
      this.data = node.data;
    }

    this.$nodesMap = /* @__PURE__ */new Map();
  },
  destroyed: function destroyed() {
    this.data = null;
    this.$nodesMap.clear();
  },
  render: function render(createElement) {
    var _this3 = this;

    var h = arguments[0];
    var node = this.node,
        nested = this.nested;
    var tree = node.tree,
        level = node.level,
        value = node.value;

    if (!tree || !tree.nodeMap.get(value)) {
      this.$destroy();
    }

    var styles = this.getStyles();
    var classList = this.getClassList();
    var itemNode = h("div", {
      "class": classList,
      "attrs": {
        "data-value": value,
        "data-level": level
      },
      "style": styles,
      "on": {
        "click": function click(evt) {
          return _this3.handleClick(evt);
        }
      }
    }, [this.renderItem(createElement)]);

    if (!nested) {
      return itemNode;
    }

    var childNodes = this.getChildNodes();
    var childrenClassList = [];
    childrenClassList.push("".concat(this.componentName, "__children"));

    if (node.expanded) {
      childrenClassList.push("".concat(this.componentName, "__children--visible"));
    } else {
      childrenClassList.push("".concat(this.componentName, "__children--hidden"));
    }

    var allChildren = node.walk();
    allChildren.shift();
    var allExpandedChildren = allChildren.filter(function (node2) {
      var parent = node2.getParent();
      if (!parent) return true;
      return parent.expanded;
    });
    var childrenStyles = {
      "--hscale": allExpandedChildren.length
    };
    var childrenBox = 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": childrenClassList,
      "style": childrenStyles
    }, [childNodes]);
    var branchNode = h("div", {
      "class": "".concat(this.componentName, "__branch")
    }, [itemNode, childrenBox]);
    return branchNode;
  }
});

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