"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const shared_esmBundler = require("../../../../../@vue/shared/dist/shared.esm-bundler.js");
const error = require("../../../utils/error.js");
const index$4 = require("../../../hooks/use-namespace/index.js");
const index$1 = require("../../icon/index.js");
const index$2 = require("../../checkbox/index.js");
const index$3 = require("../../collapse-transition/index.js");
const util = require("./model/util.js");
const node = require("./model/node.js");
const tokens = require("./tokens.js");
const treeNodeContent = require("./tree-node-content.js");
const useNodeExpandEventBroadcast = require("./model/useNodeExpandEventBroadcast.js");
const useDragNode = require("./model/useDragNode.js");
const index = require("../../../../../@element-plus/icons-vue/dist/index.js");
const Vue = require("vue");
var tree_node_vue_vue_type_script_lang_default = Vue.defineComponent({
  name: "ElTreeNode",
  components: {
    ElCollapseTransition: index$3.ElCollapseTransition,
    ElCheckbox: index$2.ElCheckbox,
    NodeContent: treeNodeContent.default,
    ElIcon: index$1.ElIcon,
    Loading: index.Loading
  },
  props: {
    node: {
      type: node.default,
      default: () => ({})
    },
    props: {
      type: Object,
      default: () => ({})
    },
    accordion: Boolean,
    renderContent: Function,
    renderAfterExpand: Boolean,
    showCheckbox: Boolean
  },
  emits: ["node-expand"],
  setup(props, ctx) {
    const ns = index$4.useNamespace("tree");
    const { broadcastExpanded } = useNodeExpandEventBroadcast.useNodeExpandEventBroadcast(props);
    const tree = Vue.inject(tokens.ROOT_TREE_INJECTION_KEY);
    const expanded = Vue.ref(false);
    const childNodeRendered = Vue.ref(false);
    const oldChecked = Vue.ref();
    const oldIndeterminate = Vue.ref();
    const node$ = Vue.ref();
    const dragEvents = Vue.inject(useDragNode.dragEventsKey);
    const instance = Vue.getCurrentInstance();
    Vue.provide(tokens.NODE_INSTANCE_INJECTION_KEY, instance);
    if (!tree) error.debugWarn("Tree", "Can not find node's tree.");
    if (props.node.expanded) {
      expanded.value = true;
      childNodeRendered.value = true;
    }
    const childrenKey = tree.props.props["children"] || "children";
    Vue.watch(() => {
      var _a;
      const children = (_a = props.node.data) == null ? void 0 : _a[childrenKey];
      return children && [...children];
    }, () => {
      props.node.updateChildren();
    });
    Vue.watch(() => props.node.indeterminate, (val) => {
      handleSelectChange(props.node.checked, val);
    });
    Vue.watch(() => props.node.checked, (val) => {
      handleSelectChange(val, props.node.indeterminate);
    });
    Vue.watch(() => props.node.childNodes.length, () => props.node.reInitChecked());
    Vue.watch(() => props.node.expanded, (val) => {
      Vue.nextTick(() => expanded.value = val);
      if (val) childNodeRendered.value = true;
    });
    const getNodeKey$1 = (node2) => {
      return tree.props.nodeKey ? util.getNodeKey(tree.props.nodeKey, node2.data) : node2.id;
    };
    const getNodeClass = (node2) => {
      const nodeClassFunc = props.props.class;
      if (!nodeClassFunc) return {};
      let className;
      if (shared_esmBundler.isFunction(nodeClassFunc)) {
        const { data } = node2;
        className = nodeClassFunc(data, node2);
      } else className = nodeClassFunc;
      if (shared_esmBundler.isString(className)) return { [className]: true };
      else return className;
    };
    const handleSelectChange = (checked, indeterminate) => {
      if (oldChecked.value !== checked || oldIndeterminate.value !== indeterminate) tree.ctx.emit("check-change", props.node.data, checked, indeterminate);
      oldChecked.value = checked;
      oldIndeterminate.value = indeterminate;
    };
    const handleClick = (e) => {
      util.handleCurrentChange(tree.store, tree.ctx.emit, () => {
        var _a;
        if ((_a = tree == null ? void 0 : tree.props) == null ? void 0 : _a.nodeKey) {
          const curNodeKey = getNodeKey$1(props.node);
          tree.store.value.setCurrentNodeKey(curNodeKey);
        } else tree.store.value.setCurrentNode(props.node);
      });
      tree.currentNode.value = props.node;
      if (tree.props.expandOnClickNode) handleExpandIconClick();
      if ((tree.props.checkOnClickNode || props.node.isLeaf && tree.props.checkOnClickLeaf && props.showCheckbox) && !props.node.disabled) handleCheckChange(!props.node.checked);
      tree.ctx.emit("node-click", props.node.data, props.node, instance, e);
    };
    const handleContextMenu = (event) => {
      var _a;
      if ((_a = tree.instance.vnode.props) == null ? void 0 : _a["onNodeContextmenu"]) {
        event.stopPropagation();
        event.preventDefault();
      }
      tree.ctx.emit("node-contextmenu", event, props.node.data, props.node, instance);
    };
    const handleExpandIconClick = () => {
      if (props.node.isLeaf) return;
      if (expanded.value) {
        tree.ctx.emit("node-collapse", props.node.data, props.node, instance);
        props.node.collapse();
      } else props.node.expand(() => {
        ctx.emit("node-expand", props.node.data, props.node, instance);
      });
    };
    const handleCheckChange = (value) => {
      const checkStrictly = tree == null ? void 0 : tree.props.checkStrictly;
      const childNodes = props.node.childNodes;
      if (!checkStrictly && childNodes.length) value = childNodes.some((node2) => !node2.isEffectivelyChecked);
      props.node.setChecked(value, !checkStrictly);
      Vue.nextTick(() => {
        const store = tree.store.value;
        tree.ctx.emit("check", props.node.data, {
          checkedNodes: store.getCheckedNodes(),
          checkedKeys: store.getCheckedKeys(),
          halfCheckedNodes: store.getHalfCheckedNodes(),
          halfCheckedKeys: store.getHalfCheckedKeys()
        });
      });
    };
    const handleChildNodeExpand = (nodeData, node2, instance2) => {
      broadcastExpanded(node2);
      tree.ctx.emit("node-expand", nodeData, node2, instance2);
    };
    const handleDragStart = (event) => {
      if (!tree.props.draggable) return;
      dragEvents.treeNodeDragStart({
        event,
        treeNode: props
      });
    };
    const handleDragOver = (event) => {
      event.preventDefault();
      if (!tree.props.draggable) return;
      dragEvents.treeNodeDragOver({
        event,
        treeNode: {
          $el: node$.value,
          node: props.node
        }
      });
    };
    const handleDrop = (event) => {
      event.preventDefault();
    };
    const handleDragEnd = (event) => {
      if (!tree.props.draggable) return;
      dragEvents.treeNodeDragEnd(event);
    };
    return {
      ns,
      node$,
      tree,
      expanded,
      childNodeRendered,
      oldChecked,
      oldIndeterminate,
      getNodeKey: getNodeKey$1,
      getNodeClass,
      handleSelectChange,
      handleClick,
      handleContextMenu,
      handleExpandIconClick,
      handleCheckChange,
      handleChildNodeExpand,
      handleDragStart,
      handleDragOver,
      handleDrop,
      handleDragEnd,
      CaretRight: index.CaretRight
    };
  }
});
exports.default = tree_node_vue_vue_type_script_lang_default;
