/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

import { _ as _defineProperty } from '../_chunks/dep-2db7b3ec.mjs';
import { isVNode, defineComponent, inject, ref, reactive, computed, createVNode, h, mergeProps } from 'vue';
import { i as isFunction_1 } from '../_chunks/dep-5717e263.mjs';
import { CaretRightSmallIcon } from '@ldesign/icons-vue-next';
import { Checkbox } from '../checkbox/index.mjs';
import { Loading } from '../loading/index.mjs';
import useRipple from '../hooks/useRipple.mjs';
import { usePrefixClass } from '../hooks/useConfig.mjs';
import { useGlobalIcon } from '../hooks/useGlobalIcon.mjs';
import { injectKey, useCLASSNAMES } from './constants.mjs';
import { getTNode } from './util.mjs';
import useDraggable from './hooks/useDraggable.mjs';
import { useConfig } from '../config-provider/useConfig.mjs';
import '../_chunks/dep-255303ba.mjs';
import '../_chunks/dep-eba0ea6e.mjs';
import '../_chunks/dep-469b4ae0.mjs';
import '../_chunks/dep-a8bcacea.mjs';
import '../utils/withInstall.mjs';
import '../checkbox/checkbox.mjs';
import '../_chunks/dep-84acf3c2.mjs';
import '../_chunks/dep-b8d7992c.mjs';
import '../hooks/useVModel.mjs';
import '../_chunks/dep-9a85dac1.mjs';
import '../_chunks/dep-1372b2d3.mjs';
import '../_chunks/dep-42cc3015.mjs';
import '../_chunks/dep-b57180d0.mjs';
import '../_chunks/dep-c8d7b6b4.mjs';
import '../_chunks/dep-2bc37d52.mjs';
import '../_chunks/dep-3da99438.mjs';
import '../form/hooks.mjs';
import '../hooks/tnode.mjs';
import '../_chunks/dep-406cd044.mjs';
import '../_chunks/dep-327559e8.mjs';
import '../_chunks/dep-b2235f26.mjs';
import '../_chunks/dep-8b989474.mjs';
import '../utils/render-tnode.mjs';
import '../_chunks/dep-888e80c4.mjs';
import '../_chunks/dep-334a4f6b.mjs';
import '../_chunks/dep-e5eb9f23.mjs';
import '../_chunks/dep-9fe2ef12.mjs';
import '../_chunks/dep-672e7d1a.mjs';
import '../_chunks/dep-4d7a675b.mjs';
import '../_chunks/dep-2e7f59b1.mjs';
import '../_chunks/dep-1434aa15.mjs';
import '../_chunks/dep-9262ee2f.mjs';
import '../_chunks/dep-876fb3ec.mjs';
import '../checkbox/props.mjs';
import '../checkbox/constants.mjs';
import '../utils/set-style.mjs';
import '../hooks/useKeepAnimation.mjs';
import '../_chunks/dep-40d69a9c.mjs';
import '../_chunks/dep-9cf86cf1.mjs';
import '../_chunks/dep-bc1d737a.mjs';
import '../_chunks/dep-855db8bc.mjs';
import '../_chunks/dep-a87f3e4c.mjs';
import '../_chunks/dep-1e566bb5.mjs';
import '../_chunks/dep-42a19209.mjs';
import '../_chunks/dep-dcc83161.mjs';
import '../_chunks/dep-0fc02184.mjs';
import '../_chunks/dep-f1994936.mjs';
import '../_chunks/dep-345e1be3.mjs';
import '../_common/js/global-config/default-config.mjs';
import '../_common/js/global-config/locale/zh_CN.mjs';
import '../_chunks/dep-9888611c.mjs';
import '../_chunks/dep-697a1365.mjs';
import '../_chunks/dep-095107f7.mjs';
import '../_chunks/dep-c989dff3.mjs';
import '../_chunks/dep-8d3e9aa3.mjs';
import '../config-provider/type.mjs';
import '../checkbox/group.mjs';
import '../_chunks/dep-bf1e33c8.mjs';
import '../_chunks/dep-85ed244c.mjs';
import '../_chunks/dep-c42916c7.mjs';
import '../_chunks/dep-78847b2e.mjs';
import '../hooks/slot.mjs';
import '../checkbox/checkbox-group-props.mjs';
import '../checkbox/style/css.mjs';
import '../checkbox/type.mjs';
import '../loading/directive.mjs';
import '../loading/plugin.mjs';
import '../utils/dom.mjs';
import '../utils/easing.mjs';
import '../loading/loading.mjs';
import '../hooks/useTeleport.mjs';
import '../loading/props.mjs';
import '../loading/icon/gradient.mjs';
import '../_common/js/loading/circle-adapter.mjs';
import '../_common/js/utils/set-style.mjs';
import '../_common/js/utils/helper.mjs';
import '../_chunks/dep-60fae668.mjs';
import '../_chunks/dep-ed5ebb35.mjs';
import '../_chunks/dep-3f21269a.mjs';
import '../loading/style/css.mjs';
import '../loading/type.mjs';
import '../_chunks/dep-8932db97.mjs';
import '../_chunks/dep-0d3dd4b5.mjs';
import '../_chunks/dep-550c24c1.mjs';
import '../_chunks/dep-6884627f.mjs';
import '../_chunks/dep-4f3ad3f8.mjs';
import '../_chunks/dep-9b6cf76d.mjs';
import '../_chunks/dep-f709f709.mjs';

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; }
function _isSlot(s) {
  return typeof s === 'function' || Object.prototype.toString.call(s) === '[object Object]' && !isVNode(s);
}
var TreeItem = defineComponent({
  name: "TTreeNode",
  props: {
    node: {
      type: Object
    },
    onClick: Function,
    onChange: Function,
    onDrop: Function,
    onDragStart: Function,
    expandOnClickNode: Boolean
  },
  setup: function setup(props) {
    var treeScope = inject(injectKey);
    var isClicked = ref(false);
    var label = ref();
    useRipple(label);
    var CLASS_NAMES = useCLASSNAMES();
    var _useConfig = useConfig("tree"),
      globalConfig = _useConfig.globalConfig;
    var classPrefix = usePrefixClass();
    var _useGlobalIcon = useGlobalIcon({
        CaretRightSmallIcon: CaretRightSmallIcon
      }),
      CaretRightSmallIcon$1 = _useGlobalIcon.CaretRightSmallIcon;
    var handleClick = function handleClick(evt) {
      var _srcTarget$className, _srcTarget$className$, _props$onClick;
      var node = props.node,
        expandOnClickNode = props.expandOnClickNode;
      var state = {
        mouseEvent: evt,
        event: evt,
        node: node,
        path: node.getPath()
      };
      var srcTarget = evt.target;
      var isBranchTrigger = node.children && props.expandOnClickNode && (srcTarget.className === "".concat(classPrefix.value, "-checkbox__input") || srcTarget.tagName.toLowerCase() === "input");
      if (isClicked.value || isBranchTrigger) return;
      if (expandOnClickNode && 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(classPrefix.value, "-tree__label"))) !== -1) evt.preventDefault();
      isClicked.value = true;
      setTimeout(function () {
        isClicked.value = false;
      });
      (_props$onClick = props.onClick) === null || _props$onClick === void 0 ? void 0 : _props$onClick.call(props, state);
    };
    var nodeRef = ref();
    var _useDraggable = useDraggable(reactive({
        nodeRef: nodeRef,
        node: props.node
      })),
      isDragOver = _useDraggable.isDragOver,
      isDragging = _useDraggable.isDragging,
      dropPosition = _useDraggable.dropPosition,
      setDragStatus = _useDraggable.setDragStatus;
    var handleDragStart = function handleDragStart(evt) {
      var _props$onDragStart;
      var node = props.node;
      if (!node.isDraggable()) return;
      evt.stopPropagation();
      setDragStatus("dragStart", evt);
      evt.dataTransfer.effectAllowed = "move";
      try {
        var _evt$dataTransfer;
        (_evt$dataTransfer = evt.dataTransfer) === null || _evt$dataTransfer === void 0 ? void 0 : _evt$dataTransfer.setData("text/plain", "");
      } catch (e) {}
      (_props$onDragStart = props.onDragStart) === null || _props$onDragStart === void 0 ? void 0 : _props$onDragStart.call(props, node, evt);
    };
    var handleDragEnd = function handleDragEnd(evt) {
      var node = props.node;
      if (!node.isDraggable()) return;
      evt.stopPropagation();
      setDragStatus("dragEnd", evt);
    };
    var handleDragOver = function handleDragOver(evt) {
      var node = props.node;
      if (!node.isDraggable()) return;
      evt.stopPropagation();
      evt.preventDefault();
      setDragStatus("dragOver", evt);
    };
    var handleDragLeave = function handleDragLeave(evt) {
      var node = props.node;
      if (!node.isDraggable()) return;
      evt.stopPropagation();
      setDragStatus("dragLeave", evt);
    };
    var handleDrop = function handleDrop(evt) {
      var _props$onDrop;
      var node = props.node;
      if (!node.isDraggable()) return;
      evt.stopPropagation();
      evt.preventDefault();
      setDragStatus("drop", evt);
      (_props$onDrop = props.onDrop) === null || _props$onDrop === void 0 ? void 0 : _props$onDrop.call(props, node, dropPosition.value, evt);
    };
    var handleChange = function handleChange() {
      var _props$onChange;
      var node = props.node;
      var event = new Event("change");
      var state = {
        event: event,
        node: node
      };
      (_props$onChange = props.onChange) === null || _props$onChange === void 0 ? void 0 : _props$onChange.call(props, state);
    };
    var itemStyles = computed(function () {
      var level = props.node.level;
      var styles = "--level: ".concat(level, ";");
      return styles;
    });
    var itemClassList = computed(function () {
      var _list$push, _list$push2;
      var node = props.node;
      var list = [];
      list.push(CLASS_NAMES.value.treeNode);
      list.push((_list$push = {}, _defineProperty(_list$push, CLASS_NAMES.value.treeNodeOpen, node.expanded), _defineProperty(_list$push, CLASS_NAMES.value.actived, node.isActivable() ? node.actived : false), _defineProperty(_list$push, CLASS_NAMES.value.disabled, node.isDisabled()), _list$push));
      list.push((_list$push2 = {}, _defineProperty(_list$push2, CLASS_NAMES.value.treeNodeDraggable, node.isDraggable()), _defineProperty(_list$push2, CLASS_NAMES.value.treeNodeDragging, isDragging.value), _defineProperty(_list$push2, CLASS_NAMES.value.treeNodeDragTipTop, isDragOver.value && dropPosition.value < 0), _defineProperty(_list$push2, CLASS_NAMES.value.treeNodeDragTipBottom, isDragOver.value && dropPosition.value > 0), _defineProperty(_list$push2, CLASS_NAMES.value.treeNodeDragTipHighlight, !isDragging.value && isDragOver.value && dropPosition.value === 0), _list$push2));
      return list;
    });
    var renderLine = function renderLine() {
      var node = props.node;
      var _treeScope$value = treeScope.value,
        line = _treeScope$value.line,
        scopedSlots = _treeScope$value.scopedSlots;
      var iconVisible = !!treeScope.value.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(CLASS_NAMES.value.line);
          if (vmIsLeaf || !iconVisible) lineClasses.push(CLASS_NAMES.value.lineIsLeaf);
          if (vmIsFirst && iconVisible) lineClasses.push(CLASS_NAMES.value.lineIsFirst);
          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,
            "box-shadow": shadowStyles.join(",")
          };
          lineNode = createVNode("span", {
            "class": lineClasses,
            "style": styles
          }, null);
        }
      } else {
        lineNode = getTNode(line, {
          node: node
        });
      }
      return lineNode;
    };
    var renderIcon = function renderIcon() {
      var getFolderIcon = function getFolderIcon() {
        if (isFunction_1(globalConfig.value.folderIcon)) return globalConfig.value.folderIcon(h);
        return createVNode(CaretRightSmallIcon$1, null, null);
      };
      var node = props.node;
      var _treeScope$value2 = treeScope.value,
        icon = _treeScope$value2.icon,
        scopedSlots = _treeScope$value2.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 = getFolderIcon();
          if (node.loading && node.expanded) iconNode = createVNode(Loading, null, null);
        } else {
          iconNode = "";
        }
      } else {
        iconNode = getTNode(icon, {
          node: node
        });
      }
      var _iconNode = function () {
        return iconNode;
      }();
      iconNode = createVNode("span", mergeProps({
        "class": [CLASS_NAMES.value.treeIcon, CLASS_NAMES.value.folderIcon, isDefaultIcon ? CLASS_NAMES.value.treeIconDefault : ""]
      }, {
        trigger: "expand",
        ignore: "active"
      }), [iconNode]);
      return iconNode;
    };
    var renderLabel = function renderLabel() {
      var node = props.node;
      var _treeScope$value3 = treeScope.value,
        label2 = _treeScope$value3.label,
        scopedSlots = _treeScope$value3.scopedSlots,
        disableCheck = _treeScope$value3.disableCheck;
      var checkProps = treeScope.value.checkProps || {};
      var labelNode = null;
      if (label2 === true) {
        if (scopedSlots.label) {
          labelNode = scopedSlots.label({
            node: node.getModel()
          });
        } else {
          labelNode = node.label || "";
        }
      } else {
        labelNode = getTNode(label2, {
          node: node
        });
      }
      var labelClasses = [CLASS_NAMES.value.treeLabel, CLASS_NAMES.value.treeLabelStrictly, _defineProperty({}, CLASS_NAMES.value.actived, node.isActivable() ? node.actived : false)];
      if (node.vmCheckable) {
        var _node$data, _node$children;
        var checkboxDisabled = false;
        if (isFunction_1(disableCheck)) checkboxDisabled = disableCheck(node);else checkboxDisabled = !!disableCheck || ((_node$data = node.data) === null || _node$data === void 0 ? void 0 : _node$data.checkable) === false;
        if (node.isDisabled()) checkboxDisabled = true;
        var itemCheckProps = _objectSpread(_objectSpread({}, checkProps), {}, {
          disabled: checkboxDisabled
        });
        var stopLabelTrigger = props.expandOnClickNode && Array.isArray(node.children) && ((_node$children = node.children) === null || _node$children === void 0 ? void 0 : _node$children.length) > 0;
        var _labelNode = function () {
          return labelNode;
        }();
        labelNode = createVNode(Checkbox, mergeProps({
          "class": labelClasses,
          "checked": node.checked,
          "indeterminate": node.indeterminate,
          "disabled": node.isDisabled(),
          "name": node.value.toString(),
          "onChange": function onChange() {
            return handleChange();
          },
          "ignore": "expand",
          "stopLabelTrigger": stopLabelTrigger,
          "needRipple": true
        }, itemCheckProps), _isSlot(labelNode) ? labelNode : {
          "default": function _default() {
            return [_labelNode];
          }
        });
      } else {
        var inner = createVNode("span", {
          "style": "position: relative"
        }, [labelNode]);
        labelNode = node.isActivable() ? createVNode("span", {
          "key": "1",
          "ref": "label",
          "class": labelClasses,
          "title": node.label
        }, [inner]) : createVNode("span", {
          "key": "2",
          "class": labelClasses,
          "title": node.label
        }, [inner]);
      }
      return labelNode;
    };
    var renderOperations = function renderOperations() {
      var node = props.node;
      var _treeScope$value4 = treeScope.value,
        operations = _treeScope$value4.operations,
        scopedSlots = _treeScope$value4.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, {
          node: node
        });
      }
      if (opNode) {
        var _opNode = function () {
          return opNode;
        }();
        opNode = createVNode("span", mergeProps({
          "class": CLASS_NAMES.value.treeOperations
        }, {
          ignore: "active,expand"
        }), [opNode]);
      }
      return opNode;
    };
    return function () {
      var node = props.node;
      return createVNode("div", {
        "ref": nodeRef,
        "class": itemClassList.value,
        "data-value": node.value,
        "data-level": node.level,
        "style": itemStyles.value,
        "onClick": function onClick(evt) {
          return handleClick(evt);
        },
        "draggable": node.isDraggable(),
        "onDragstart": function onDragstart(evt) {
          return handleDragStart(evt);
        },
        "onDragend": function onDragend(evt) {
          return handleDragEnd(evt);
        },
        "onDragover": function onDragover(evt) {
          return handleDragOver(evt);
        },
        "onDragleave": function onDragleave(evt) {
          return handleDragLeave(evt);
        },
        "onDrop": function onDrop(evt) {
          return handleDrop(evt);
        }
      }, [renderLine(), renderIcon(), renderLabel(), renderOperations()]);
    };
  }
});

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