"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const style = require("../../../../utils/dom/style.js");
const numbers = require("../../../../utils/numbers.js");
const util = require("../util.js");
const tokens = require("../tokens.js");
const vue = require("vue");
const debounce = require("../../../../../../lodash-es/debounce.js");
function useEvents(props) {
  const parent = vue.inject(tokens.TABLE_INJECTION_KEY);
  const tooltipContent = vue.ref("");
  const tooltipTrigger = vue.ref(vue.h("div"));
  const handleEvent = (event, row, name) => {
    var _a, _b;
    const table = parent;
    const cell = util.getCell(event);
    let column = null;
    const namespace = (_a = table == null ? void 0 : table.vnode.el) == null ? void 0 : _a.dataset.prefix;
    if (cell) {
      column = util.getColumnByCell({ columns: ((_b = props.store) == null ? void 0 : _b.states.columns.value) ?? [] }, cell, namespace);
      if (column) table == null ? void 0 : table.emit(`cell-${name}`, row, column, cell, event);
    }
    table == null ? void 0 : table.emit(`row-${name}`, row, column, event);
  };
  const handleDoubleClick = (event, row) => {
    handleEvent(event, row, "dblclick");
  };
  const handleClick = (event, row) => {
    var _a;
    (_a = props.store) == null ? void 0 : _a.commit("setCurrentRow", row);
    handleEvent(event, row, "click");
  };
  const handleContextMenu = (event, row) => {
    handleEvent(event, row, "contextmenu");
  };
  const handleMouseEnter = debounce.default((index) => {
    var _a;
    (_a = props.store) == null ? void 0 : _a.commit("setHoverRow", index);
  }, 30);
  const handleMouseLeave = debounce.default(() => {
    var _a;
    (_a = props.store) == null ? void 0 : _a.commit("setHoverRow", null);
  }, 30);
  const getPadding = (el) => {
    const style2 = window.getComputedStyle(el, null);
    return {
      left: Number.parseInt(style2.paddingLeft, 10) || 0,
      right: Number.parseInt(style2.paddingRight, 10) || 0,
      top: Number.parseInt(style2.paddingTop, 10) || 0,
      bottom: Number.parseInt(style2.paddingBottom, 10) || 0
    };
  };
  const toggleRowClassByCell = (rowSpan, event, toggle) => {
    var _a;
    let node = (_a = event == null ? void 0 : event.target) == null ? void 0 : _a.parentNode;
    while (rowSpan > 1) {
      node = node == null ? void 0 : node.nextSibling;
      if (!node || node.nodeName !== "TR") break;
      toggle(node, "hover-row hover-fixed-row");
      rowSpan--;
    }
  };
  const handleCellMouseEnter = (event, row, tooltipOptions) => {
    var _a, _b, _c, _d, _e, _f, _g;
    if (!parent) return;
    const table = parent;
    const cell = util.getCell(event);
    const namespace = (_a = table == null ? void 0 : table.vnode.el) == null ? void 0 : _a.dataset.prefix;
    let column = null;
    if (cell) {
      column = util.getColumnByCell({ columns: ((_b = props.store) == null ? void 0 : _b.states.columns.value) ?? [] }, cell, namespace);
      if (!column) return;
      if (cell.rowSpan > 1) toggleRowClassByCell(cell.rowSpan, event, style.addClass);
      const hoverState = table.hoverState = {
        cell,
        column,
        row
      };
      table == null ? void 0 : table.emit("cell-mouse-enter", hoverState.row, hoverState.column, hoverState.cell, event);
    }
    if (!tooltipOptions) {
      if (((_c = util.removePopper) == null ? void 0 : _c.trigger) === cell) (_d = util.removePopper) == null ? void 0 : _d.call(util);
      return;
    }
    const cellChild = event.target.querySelector(".cell");
    if (!(style.hasClass(cellChild, `${namespace}-tooltip`) && cellChild.childNodes.length && ((_e = cellChild.textContent) == null ? void 0 : _e.trim()))) return;
    const range = document.createRange();
    range.setStart(cellChild, 0);
    range.setEnd(cellChild, cellChild.childNodes.length);
    const { width: rangeWidth, height: rangeHeight } = range.getBoundingClientRect();
    const { width: cellChildWidth, height: cellChildHeight } = cellChild.getBoundingClientRect();
    const { top, left, right, bottom } = getPadding(cellChild);
    const horizontalPadding = left + right;
    const verticalPadding = top + bottom;
    if (numbers.isGreaterThan(rangeWidth + horizontalPadding, cellChildWidth) || numbers.isGreaterThan(rangeHeight + verticalPadding, cellChildHeight) || numbers.isGreaterThan(cellChild.scrollWidth, cellChildWidth)) util.createTablePopper(tooltipOptions, ((cell == null ? void 0 : cell.innerText) || (cell == null ? void 0 : cell.textContent)) ?? "", row, column, cell, table);
    else if (((_f = util.removePopper) == null ? void 0 : _f.trigger) === cell) (_g = util.removePopper) == null ? void 0 : _g.call(util);
  };
  const handleCellMouseLeave = (event) => {
    const cell = util.getCell(event);
    if (!cell) return;
    if (cell.rowSpan > 1) toggleRowClassByCell(cell.rowSpan, event, style.removeClass);
    const oldHoverState = parent == null ? void 0 : parent.hoverState;
    parent == null ? void 0 : parent.emit("cell-mouse-leave", oldHoverState == null ? void 0 : oldHoverState.row, oldHoverState == null ? void 0 : oldHoverState.column, oldHoverState == null ? void 0 : oldHoverState.cell, event);
  };
  return {
    handleDoubleClick,
    handleClick,
    handleContextMenu,
    handleMouseEnter,
    handleMouseLeave,
    handleCellMouseEnter,
    handleCellMouseLeave,
    tooltipContent,
    tooltipTrigger
  };
}
exports.default = useEvents;
