import {
  StateNode,
  Vec
} from "@tldraw/editor";
import { updateArrowTargetState } from "../../../shapes/arrow/arrowTargetState.mjs";
import { getArrowBindings } from "../../../shapes/arrow/shared.mjs";
import {
  getNoteAdjacentPositions,
  getNoteShapeForAdjacentPosition
} from "../../../shapes/note/noteHelpers.mjs";
import { getDisplayValues } from "../../../shapes/shared/getDisplayValues.mjs";
import { startEditingShapeWithRichText } from "../selectHelpers.mjs";
class PointingHandle extends StateNode {
  static id = "pointing_handle";
  didCtrlOnEnter = false;
  info = {};
  onEnter(info) {
    this.info = info;
    this.didCtrlOnEnter = info.accelKey;
    const { shape } = info;
    if (this.editor.isShapeOfType(shape, "arrow")) {
      const initialBindings = getArrowBindings(this.editor, shape);
      const currentBinding = initialBindings[info.handle.id];
      const oppositeBinding = initialBindings[info.handle.id === "start" ? "end" : "start"];
      const arrowTransform = this.editor.getShapePageTransform(shape.id);
      if (currentBinding) {
        updateArrowTargetState({
          editor: this.editor,
          pointInPageSpace: arrowTransform.applyToPoint(info.handle),
          arrow: shape,
          isPrecise: currentBinding.props.isPrecise,
          currentBinding,
          oppositeBinding
        });
      }
    }
    this.editor.setCursor({ type: "grabbing", rotation: 0 });
  }
  onExit() {
    this.editor.setHintingShapes([]);
    this.editor.setCursor({ type: "default", rotation: 0 });
  }
  onPointerUp() {
    const { shape, handle } = this.info;
    if (this.editor.isShapeOfType(shape, "note")) {
      const { editor } = this;
      const nextNote = getNoteForAdjacentPosition(editor, shape, handle, false);
      if (nextNote) {
        startEditingShapeWithRichText(editor, nextNote, { selectAll: true });
        return;
      }
    }
    this.parent.transition("idle", this.info);
  }
  onDoubleClick(info) {
    if (this.editor.inputs.getShiftKey() || info.phase !== "down" || info.ctrlKey || info.shiftKey) {
      return;
    }
    this.parent.transition("idle");
    this.parent.getCurrent()?.handleEvent(info);
  }
  onPointerMove(info) {
    const { editor } = this;
    if (editor.inputs.getIsDragging()) {
      if (this.didCtrlOnEnter) {
        this.parent.transition("brushing", info);
      } else {
        this.startDraggingHandle();
      }
    }
  }
  onLongPress() {
    this.startDraggingHandle();
  }
  startDraggingHandle() {
    const { editor } = this;
    if (editor.getIsReadonly()) return;
    const { shape, handle } = this.info;
    if (editor.isShapeOfType(shape, "note")) {
      const noteUtil = editor.getShapeUtil(shape);
      const dv = getDisplayValues(noteUtil, shape);
      const nextNote = getNoteForAdjacentPosition(editor, shape, handle, true);
      if (nextNote) {
        const centeredOnPointer = editor.getPointInParentSpace(nextNote, editor.inputs.getOriginPagePoint()).sub(
          Vec.Rot(
            new Vec(dv.noteWidth / 2, dv.noteHeight / 2).mul(shape.props.scale),
            nextNote.rotation
          )
        );
        editor.updateShape({ ...nextNote, x: centeredOnPointer.x, y: centeredOnPointer.y });
        editor.setHoveredShape(nextNote.id).select(nextNote.id).setCurrentTool("select.translating", {
          ...this.info,
          target: "shape",
          shape: editor.getShape(nextNote),
          onInteractionEnd: "note",
          isCreating: true,
          onCreate: () => {
            startEditingShapeWithRichText(editor, nextNote, { selectAll: true });
          }
        });
        return;
      }
    }
    this.parent.transition("dragging_handle", this.info);
  }
  onCancel() {
    this.cancel();
  }
  onComplete() {
    this.cancel();
  }
  onInterrupt() {
    this.cancel();
  }
  cancel() {
    this.parent.transition("idle");
  }
}
function getNoteForAdjacentPosition(editor, shape, handle, forceNew) {
  const noteUtil = editor.getShapeUtil(shape);
  const dv = getDisplayValues(noteUtil, shape);
  const pageTransform = editor.getShapePageTransform(shape.id);
  const pagePoint = pageTransform.point();
  const pageRotation = pageTransform.rotation();
  const positions = getNoteAdjacentPositions(editor, {
    pagePoint,
    pageRotation,
    growY: shape.props.growY * shape.props.scale,
    extraHeight: 0,
    scale: shape.props.scale,
    noteWidth: dv.noteWidth,
    noteHeight: dv.noteHeight
  });
  const position = positions[handle.index];
  if (position) {
    return getNoteShapeForAdjacentPosition(editor, {
      shape,
      center: position,
      pageRotation,
      noteWidth: dv.noteWidth,
      noteHeight: dv.noteHeight,
      forceNew
    });
  }
  return void 0;
}
export {
  PointingHandle
};
//# sourceMappingURL=PointingHandle.mjs.map
