import { Plugin } from "./../../../tiptap/pm/state.js";
import { preserveScrollDuring } from "./preserve-scroll.js";
import { isSelectingCells } from "./table-editing-plugin.js";
const resizeTicksByView = /* @__PURE__ */ new WeakMap();
const setResizeTick = (view, cb) => {
  resizeTicksByView.set(view, cb);
};
const clearResizeTick = (view, cb) => {
  if (resizeTicksByView.get(view) === cb) {
    resizeTicksByView.delete(view);
  }
};
const triggerResizeTick = (view) => {
  resizeTicksByView.get(view)?.();
};
const createResizeDragController = (editor, handlers) => {
  let activeDragState = null;
  let capturedElement = null;
  let capturedPointerId = null;
  let globalCleanup = null;
  let pendingDragRaf = 0;
  let pendingDragEvent = null;
  const releasePointer = () => {
    if (capturedElement && capturedPointerId !== null) {
      try {
        capturedElement.releasePointerCapture(capturedPointerId);
      } catch {
      }
    }
    capturedElement = null;
    capturedPointerId = null;
  };
  const cancelPendingFrame = () => {
    if (pendingDragRaf !== 0) {
      cancelAnimationFrame(pendingDragRaf);
      pendingDragRaf = 0;
    }
    pendingDragEvent = null;
  };
  const flushPendingFrame = (view, state) => {
    if (pendingDragRaf !== 0) {
      cancelAnimationFrame(pendingDragRaf);
      pendingDragRaf = 0;
    }
    const event = pendingDragEvent;
    pendingDragEvent = null;
    if (event) {
      handlers.applyFrame(view, event, state);
    }
  };
  const cleanupDrag = (view) => {
    releasePointer();
    if (globalCleanup) {
      globalCleanup();
    }
    handlers.setActive(view, false);
    activeDragState = null;
  };
  const finish = (view) => {
    const state = activeDragState;
    if (!state) {
      return false;
    }
    flushPendingFrame(view, state);
    cleanupDrag(view);
    preserveScrollDuring(view, () => handlers.commit(view, state));
    return true;
  };
  const start = (view, args) => {
    const { event } = args;
    if (event.button !== 0) {
      return false;
    }
    if (handlers.isOtherActive(view) || isSelectingCells(view.state) || !editor.isEditable) {
      return false;
    }
    const target = event.target;
    if (target) {
      try {
        target.setPointerCapture(event.pointerId);
        capturedElement = target;
        capturedPointerId = event.pointerId;
      } catch {
      }
    }
    const cleanupGlobalListeners = () => {
      document.removeEventListener("pointerup", handleGlobalPointerUp, true);
      document.removeEventListener("pointercancel", handleGlobalPointerUp, true);
      globalCleanup = null;
    };
    const handleGlobalPointerUp = (e) => {
      if (e.pointerId !== event.pointerId) {
        return;
      }
      if (activeDragState) {
        finish(view);
      } else {
        cleanupGlobalListeners();
      }
    };
    document.addEventListener("pointerup", handleGlobalPointerUp, true);
    document.addEventListener("pointercancel", handleGlobalPointerUp, true);
    globalCleanup = cleanupGlobalListeners;
    const state = handlers.begin(view, args);
    if (state === null) {
      releasePointer();
      cleanupGlobalListeners();
      return false;
    }
    activeDragState = state;
    handlers.setActive(view, true);
    event.preventDefault();
    event.stopPropagation();
    return true;
  };
  const guardEvent = (view) => activeDragState !== null && !handlers.isOtherActive(view) && editor.isEditable;
  const plugin = new Plugin({
    key: handlers.pluginKey,
    props: {
      handleDOMEvents: {
        pointermove: (view, event) => {
          if (!guardEvent(view)) {
            return false;
          }
          pendingDragEvent = event;
          if (pendingDragRaf === 0) {
            const drag = activeDragState;
            pendingDragRaf = requestAnimationFrame(() => {
              pendingDragRaf = 0;
              const e = pendingDragEvent;
              pendingDragEvent = null;
              if (e && activeDragState === drag && drag !== null) {
                handlers.applyFrame(view, e, drag);
              }
            });
          }
          event.preventDefault();
          event.stopPropagation();
          return true;
        },
        pointerup: (view) => guardEvent(view) ? finish(view) : false,
        pointercancel: (view) => guardEvent(view) ? finish(view) : false
      }
    },
    view(view) {
      return {
        destroy() {
          cancelPendingFrame();
          releasePointer();
          if (globalCleanup) {
            globalCleanup();
          }
          if (activeDragState) {
            handlers.setActive(view, false);
            activeDragState = null;
          }
        }
      };
    }
  });
  return { plugin, start };
};
export {
  clearResizeTick,
  createResizeDragController,
  setResizeTick,
  triggerResizeTick
};
//# sourceMappingURL=resize-drag-controller.js.map
