import { isElement } from "../../../../utils/types.mjs";
import { hasClass, removeClass, addClass } from "../../../../utils/dom/style.mjs";
import { TABLE_INJECTION_KEY } from "../tokens.mjs";
import { getCurrentInstance, inject, ref } from "vue";
import isNull from "../../../../../../lodash-es/isNull.mjs";
import { isClient } from "../../../../../node_modules/@vueuse/shared/index.mjs";
function useEvent(props, emit) {
  const instance = getCurrentInstance();
  const parent = inject(TABLE_INJECTION_KEY);
  const handleFilterClick = (event) => {
    event.stopPropagation();
  };
  const handleHeaderClick = (event, column) => {
    if (!column.filters && column.sortable) handleSortClick(event, column, false);
    else if (column.filterable && !column.sortable) handleFilterClick(event);
    parent == null ? void 0 : parent.emit("header-click", column, event);
  };
  const handleHeaderContextMenu = (event, column) => {
    parent == null ? void 0 : parent.emit("header-contextmenu", column, event);
  };
  const draggingColumn = ref(null);
  const dragging = ref(false);
  const dragState = ref();
  const handleMouseDown = (event, column) => {
    var _a, _b, _c;
    if (!isClient) return;
    if (column.children && column.children.length > 0) return;
    if (draggingColumn.value && props.border && draggingColumn.value.id === column.id) {
      dragging.value = true;
      const table = parent;
      emit("set-drag-visible", true);
      const tableLeft = (_a = table == null ? void 0 : table.vnode.el) == null ? void 0 : _a.getBoundingClientRect().left;
      const columnEl = (_c = (_b = instance == null ? void 0 : instance.vnode) == null ? void 0 : _b.el) == null ? void 0 : _c.querySelector(`th.${column.id}`);
      const columnRect = columnEl.getBoundingClientRect();
      const minLeft = columnRect.left - tableLeft + 30;
      addClass(columnEl, "noclick");
      dragState.value = {
        startMouseLeft: event.clientX,
        startLeft: columnRect.right - tableLeft,
        startColumnLeft: columnRect.left - tableLeft,
        tableLeft
      };
      const resizeProxy = table == null ? void 0 : table.refs.resizeProxy;
      resizeProxy.style.left = `${dragState.value.startLeft}px`;
      document.onselectstart = function() {
        return false;
      };
      document.ondragstart = function() {
        return false;
      };
      const handleMouseMove2 = (event2) => {
        const deltaLeft = event2.clientX - dragState.value.startMouseLeft;
        const proxyLeft = dragState.value.startLeft + deltaLeft;
        resizeProxy.style.left = `${Math.max(minLeft, proxyLeft)}px`;
      };
      const handleMouseUp = () => {
        if (dragging.value) {
          const { startColumnLeft, startLeft } = dragState.value;
          column.width = column.realWidth = Number.parseInt(resizeProxy.style.left, 10) - startColumnLeft;
          table == null ? void 0 : table.emit("header-dragend", column.width, startLeft - startColumnLeft, column, event);
          requestAnimationFrame(() => {
            props.store.scheduleLayout(false, true);
          });
          document.body.style.cursor = "";
          dragging.value = false;
          draggingColumn.value = null;
          dragState.value = void 0;
          emit("set-drag-visible", false);
        }
        document.removeEventListener("mousemove", handleMouseMove2);
        document.removeEventListener("mouseup", handleMouseUp);
        document.onselectstart = null;
        document.ondragstart = null;
        setTimeout(() => {
          removeClass(columnEl, "noclick");
        }, 0);
      };
      document.addEventListener("mousemove", handleMouseMove2);
      document.addEventListener("mouseup", handleMouseUp);
    }
  };
  const handleMouseMove = (event, column) => {
    var _a;
    if (!props.border || column.children && column.children.length > 0) return;
    const el = event.target;
    const target = isElement(el) ? el.closest("th") : null;
    if (!target) return;
    const isSortable = hasClass(target, "is-sortable");
    if (isSortable) {
      const cursor2 = dragging.value ? "col-resize" : "";
      target.style.cursor = cursor2;
      const caret = target.querySelector(".caret-wrapper");
      if (caret) caret.style.cursor = cursor2;
    }
    if (!column.resizable || dragging.value) {
      draggingColumn.value = null;
      return;
    }
    const rect = target.getBoundingClientRect();
    const isLastTh = ((_a = target.parentNode) == null ? void 0 : _a.lastElementChild) === target;
    const allowDrag = props.allowDragLastColumn || !isLastTh;
    const isResizeHandleActive = rect.width > 12 && rect.right - event.clientX < 8 && allowDrag;
    const cursor = isResizeHandleActive ? "col-resize" : "";
    document.body.style.cursor = cursor;
    draggingColumn.value = isResizeHandleActive ? column : null;
    if (isSortable) target.style.cursor = cursor;
  };
  const handleMouseOut = () => {
    if (!isClient || dragging.value) return;
    document.body.style.cursor = "";
  };
  const toggleOrder = ({ order, sortOrders }) => {
    if (order === "") return sortOrders[0];
    const index = sortOrders.indexOf(order || null);
    return sortOrders[index > sortOrders.length - 2 ? 0 : index + 1];
  };
  const handleSortClick = (event, column, givenOrder) => {
    var _a;
    event.stopPropagation();
    const order = column.order === givenOrder ? null : givenOrder || toggleOrder(column);
    const target = (_a = event.target) == null ? void 0 : _a.closest("th");
    if (target) {
      if (hasClass(target, "noclick")) {
        removeClass(target, "noclick");
        return;
      }
    }
    if (!column.sortable) return;
    const clickTarget = event.currentTarget;
    if (["ascending", "descending"].some((str) => hasClass(clickTarget, str) && !column.sortOrders.includes(str))) return;
    const states = props.store.states;
    let sortProp = states.sortProp.value;
    let sortOrder;
    const sortingColumn = states.sortingColumn.value;
    if (sortingColumn !== column || sortingColumn === column && isNull(sortingColumn.order)) {
      if (sortingColumn) sortingColumn.order = null;
      states.sortingColumn.value = column;
      sortProp = column.property;
    }
    if (!order) sortOrder = column.order = null;
    else sortOrder = column.order = order;
    states.sortProp.value = sortProp;
    states.sortOrder.value = sortOrder;
    parent == null ? void 0 : parent.store.commit("changeSortCondition");
  };
  return {
    handleHeaderClick,
    handleHeaderContextMenu,
    handleMouseDown,
    handleMouseMove,
    handleMouseOut,
    handleSortClick,
    handleFilterClick
  };
}
export {
  useEvent as default
};
