/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

import { getMark, emitEvent } from '../utils.js';
import useTreeAction from './useTreeAction.js';
import 'lodash/camelCase';

function useTreeEvents(props, context, state) {
  const {
    cache
  } = state;
  const {
    toggleExpanded,
    toggleActived,
    toggleChecked
  } = useTreeAction(props, context, state);
  const handleClick = evtState => {
    const {
      mouseEvent,
      event,
      node
    } = evtState;
    if (!node) return;
    cache.mouseEvent = mouseEvent;
    let shouldExpand = props.expandOnClickNode;
    let shouldActive = !props.disabled && !node.disabled;
    ["trigger", "ignore"].forEach(markName => {
      const mark = getMark(markName, event.target, event.currentTarget);
      const markValue = mark?.value || "";
      if (markValue.includes("expand")) {
        if (markName === "trigger") shouldExpand = true;else if (markName === "ignore") shouldExpand = false;
      }
      if (markValue.includes("active")) {
        if (markName === "ignore") shouldActive = false;
      }
    });
    if (shouldExpand) toggleExpanded(node);
    const evtCtx = {
      node: node.getModel(),
      e: mouseEvent
    };
    if (shouldActive) {
      toggleActived(node);
      emitEvent(props, context, "click", evtCtx);
    }
    cache.mouseEvent = null;
  };
  const handleChange = evtState => {
    const {
      disabled
    } = props;
    const {
      node
    } = evtState;
    if (!node || disabled || node.disabled) return;
    toggleChecked(node);
  };
  return {
    handleChange,
    handleClick
  };
}

export { useTreeEvents as default };
//# sourceMappingURL=useTreeEvents.js.map
