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

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var isFunction = require('lodash/isFunction');
var iconsVueNext = require('@ldesign/icons-vue-next');
var checkbox_index = require('../checkbox/index.js');
var loading_index = require('../loading/index.js');
var hooks_useRipple = require('../hooks/useRipple.js');
var hooks_useConfig = require('../hooks/useConfig.js');
var hooks_useGlobalIcon = require('../hooks/useGlobalIcon.js');
var tree_constants = require('./constants.js');
var tree_util = require('./util.js');
var tree_hooks_useDraggable = require('./hooks/useDraggable.js');
var configProvider_useConfig = require('../config-provider/useConfig.js');
require('../utils/withInstall.js');
require('../checkbox/checkbox.js');
require('@babel/runtime/helpers/slicedToArray');
require('../hooks/useVModel.js');
require('lodash/kebabCase');
require('../form/hooks.js');
require('../hooks/tnode.js');
require('lodash/camelCase');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/isString');
require('lodash/isObject');
require('../checkbox/props.js');
require('../checkbox/constants.js');
require('../utils/set-style.js');
require('../hooks/useKeepAnimation.js');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('lodash/isArray');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../_chunks/dep-f4db2590.js');
require('dayjs');
require('lodash/cloneDeep');
require('../checkbox/group.js');
require('@babel/runtime/helpers/toConsumableArray');
require('lodash/intersection');
require('lodash/isUndefined');
require('../hooks/slot.js');
require('../checkbox/checkbox-group-props.js');
require('../loading/directive.js');
require('../loading/plugin.js');
require('../utils/dom.js');
require('../utils/easing.js');
require('../loading/loading.js');
require('../hooks/useTeleport.js');
require('../loading/props.js');
require('../loading/icon/gradient.js');
require('../_common/js/loading/circle-adapter.js');
require('../_common/js/utils/set-style.js');
require('../_common/js/utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('lodash/isNull');
require('lodash/isNumber');
require('lodash/pick');
require('lodash/throttle');

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

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);

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__default["default"](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]' && !vue.isVNode(s);
}
var TreeItem = vue.defineComponent({
  name: "TTreeNode",
  props: {
    node: {
      type: Object
    },
    onClick: Function,
    onChange: Function,
    onDrop: Function,
    onDragStart: Function,
    expandOnClickNode: Boolean
  },
  setup: function setup(props) {
    var treeScope = vue.inject(tree_constants.injectKey);
    var isClicked = vue.ref(false);
    var label = vue.ref();
    hooks_useRipple["default"](label);
    var CLASS_NAMES = tree_constants.useCLASSNAMES();
    var _useConfig = configProvider_useConfig.useConfig("tree"),
      globalConfig = _useConfig.globalConfig;
    var classPrefix = hooks_useConfig.usePrefixClass();
    var _useGlobalIcon = hooks_useGlobalIcon.useGlobalIcon({
        CaretRightSmallIcon: iconsVueNext.CaretRightSmallIcon
      }),
      CaretRightSmallIcon = _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 = vue.ref();
    var _useDraggable = tree_hooks_useDraggable["default"](vue.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 = vue.computed(function () {
      var level = props.node.level;
      var styles = "--level: ".concat(level, ";");
      return styles;
    });
    var itemClassList = vue.computed(function () {
      var _list$push, _list$push2;
      var node = props.node;
      var list = [];
      list.push(CLASS_NAMES.value.treeNode);
      list.push((_list$push = {}, _defineProperty__default["default"](_list$push, CLASS_NAMES.value.treeNodeOpen, node.expanded), _defineProperty__default["default"](_list$push, CLASS_NAMES.value.actived, node.isActivable() ? node.actived : false), _defineProperty__default["default"](_list$push, CLASS_NAMES.value.disabled, node.isDisabled()), _list$push));
      list.push((_list$push2 = {}, _defineProperty__default["default"](_list$push2, CLASS_NAMES.value.treeNodeDraggable, node.isDraggable()), _defineProperty__default["default"](_list$push2, CLASS_NAMES.value.treeNodeDragging, isDragging.value), _defineProperty__default["default"](_list$push2, CLASS_NAMES.value.treeNodeDragTipTop, isDragOver.value && dropPosition.value < 0), _defineProperty__default["default"](_list$push2, CLASS_NAMES.value.treeNodeDragTipBottom, isDragOver.value && dropPosition.value > 0), _defineProperty__default["default"](_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 = vue.createVNode("span", {
            "class": lineClasses,
            "style": styles
          }, null);
        }
      } else {
        lineNode = tree_util.getTNode(line, {
          node: node
        });
      }
      return lineNode;
    };
    var renderIcon = function renderIcon() {
      var getFolderIcon = function getFolderIcon() {
        if (isFunction__default["default"](globalConfig.value.folderIcon)) return globalConfig.value.folderIcon(vue.h);
        return vue.createVNode(CaretRightSmallIcon, 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 = vue.createVNode(loading_index.Loading, null, null);
        } else {
          iconNode = "";
        }
      } else {
        iconNode = tree_util.getTNode(icon, {
          node: node
        });
      }
      iconNode = vue.createVNode("span", vue.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 = tree_util.getTNode(label2, {
          node: node
        });
      }
      var labelClasses = [CLASS_NAMES.value.treeLabel, CLASS_NAMES.value.treeLabelStrictly, _defineProperty__default["default"]({}, CLASS_NAMES.value.actived, node.isActivable() ? node.actived : false)];
      if (node.vmCheckable) {
        var _node$data, _node$children;
        var checkboxDisabled = false;
        if (isFunction__default["default"](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 = vue.createVNode(checkbox_index.Checkbox, vue.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 = vue.createVNode("span", {
          "style": "position: relative"
        }, [labelNode]);
        labelNode = node.isActivable() ? vue.createVNode("span", {
          "key": "1",
          "ref": "label",
          "class": labelClasses,
          "title": node.label
        }, [inner]) : vue.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 = tree_util.getTNode(operations, {
          node: node
        });
      }
      if (opNode) {
        opNode = vue.createVNode("span", vue.mergeProps({
          "class": CLASS_NAMES.value.treeOperations
        }, {
          ignore: "active,expand"
        }), [opNode]);
      }
      return opNode;
    };
    return function () {
      var node = props.node;
      return vue.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()]);
    };
  }
});

exports["default"] = TreeItem;
//# sourceMappingURL=tree-item.js.map
