All files / web/src/arcade-games/know-your-world/features/magnifier useMagnifierTouchHandlers.ts

0% Statements 0/807
0% Branches 0/1
0% Functions 0/1
0% Lines 0/807

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
/**
 * Magnifier Touch Handlers Hook
 *
 * Extracts touch handling logic for the magnifier overlay.
 * Handles:
 * - Single-finger panning (drag to move cursor position)
 * - Two-finger pinch-to-zoom
 * - Tap-to-select (tap on region to select it)
 *
 * This hook MUST be called inside MagnifierProvider and MapGameProvider.
 * It consumes from both contexts to get the state it needs.
 */

'use client'

import { useCallback, useRef } from 'react'

import { findOptimalZoom } from '../../utils/adaptiveZoomSearch'
import { getMagnifierDimensions } from '../../utils/magnifierDimensions'
import { useMapGameContext } from '../game'
import { getRenderedViewport } from '../labels'
import { MAX_ZOOM } from '../shared/constants'
import { useMagnifierContext } from './MagnifierContext'
import { calculateTouchMultiplier, clampToSvgBounds, parseViewBoxDimensions } from './panningMath'

// ============================================================================
// Constants
// ============================================================================

const SAFE_ZONE_MARGINS = {
  top: 60,
  right: 8,
  bottom: 8,
  left: 8,
}

// ============================================================================
// Types
// ============================================================================

export interface UseMagnifierTouchHandlersOptions {
  /** Callback when cursor position updates (for multiplayer) */
  onCursorUpdate?: (
    position: { x: number; y: number } | null,
    hoveredRegionId: string | null
  ) => void
  /** Current game mode */
  gameMode?: 'cooperative' | 'race' | 'turn-based'
  /** Current player (for turn-based) */
  currentPlayer?: string | null
  /** Local player ID */
  localPlayerId?: string
  /** Check hot/cold feedback */
  checkHotCold?: (params: {
    cursorPosition: { x: number; y: number }
    targetCenter: { x: number; y: number }
    hoveredRegionId: string | null
    cursorSvgPosition: { x: number; y: number }
  }) => void
  /** Whether in takeover state (multiplayer) */
  isInTakeover?: boolean
  /** Display viewBox string */
  displayViewBox: string
  /** Regions found (for filtering zoom calculations) */
  regionsFound: string[]
  /** Ref to hot/cold enabled state */
  hotColdEnabledRef: React.MutableRefObject<boolean>
  /** Cache of largest piece sizes */
  largestPieceSizesRef: React.MutableRefObject<Map<string, { width: number; height: number }>>
  /** Detect regions at cursor position */
  detectRegions: (
    cursorX: number,
    cursorY: number
  ) => {
    regionUnderCursor: string | null
    detectedRegions: Array<{
      id: string
      pixelWidth: number
      pixelHeight: number
      pixelArea: number
      isVerySmall: boolean
      screenSize: number
    }>
    detectedSmallestSize: number
  }
}

export interface UseMagnifierTouchHandlersReturn {
  /** Handle touch start on magnifier */
  handleMagnifierTouchStart: (e: React.TouchEvent<HTMLDivElement>) => void
  /** Handle touch move on magnifier */
  handleMagnifierTouchMove: (e: React.TouchEvent<HTMLDivElement>) => void
  /** Handle touch end on magnifier */
  handleMagnifierTouchEnd: (e: React.TouchEvent<HTMLDivElement>) => void
}

// ============================================================================
// Hook Implementation
// ============================================================================

export function useMagnifierTouchHandlers(
  options: UseMagnifierTouchHandlersOptions
): UseMagnifierTouchHandlersReturn {
  const {
    onCursorUpdate,
    gameMode,
    currentPlayer,
    localPlayerId,
    checkHotCold,
    isInTakeover = false,
    displayViewBox,
    regionsFound,
    hotColdEnabledRef,
    largestPieceSizesRef,
    detectRegions,
  } = options

  // -------------------------------------------------------------------------
  // Context Consumption
  // -------------------------------------------------------------------------
  const {
    magnifierRef,
    svgRef,
    containerRef,
    cursorPositionRef,
    scaleProbe1Ref,
    scaleProbe2Ref,
    anchorSvgPositionRef,
    isMagnifierExpanded,
    setIsMagnifierExpanded,
    getCurrentZoom,
    setTargetZoom,
    zoomSpring,
    interaction,
    parsedViewBox,
  } = useMagnifierContext()

  // Fixed distance between scale probes in SVG units (must match MagnifierOverlay)
  const SCALE_PROBE_SVG_DISTANCE = 100

  const {
    mapData,
    currentPrompt,
    celebration,
    handleRegionClickWithCelebration,
    isGiveUpAnimating,
  } = useMapGameContext()
  // Note: hoveredRegion and setHoveredRegion are no longer used from context
  // State machine (interaction.hoveredRegionId) is now authoritative via TOUCH_MOVE dispatch

  // Get isPinching from state machine
  const isPinchingFromMachine = interaction.isPinching

  // -------------------------------------------------------------------------
  // Empirical Scale Measurement
  // -------------------------------------------------------------------------
  /**
   * Measure the actual pixels-per-SVG-unit by comparing screen positions
   * of the two probe circles. This is robust to any transform pipeline changes.
   */
  const measureEmpiricalScale = useCallback((): {
    pixelsPerSvgUnit: number
    isValid: boolean
  } => {
    const probe1 = scaleProbe1Ref.current
    const probe2 = scaleProbe2Ref.current

    if (!probe1 || !probe2) {
      return { pixelsPerSvgUnit: 1, isValid: false }
    }

    // Get screen positions of the probes
    const rect1 = probe1.getBoundingClientRect()
    const rect2 = probe2.getBoundingClientRect()

    // Use center of each probe
    const x1 = rect1.left + rect1.width / 2
    const y1 = rect1.top + rect1.height / 2
    const x2 = rect2.left + rect2.width / 2
    const y2 = rect2.top + rect2.height / 2

    // Calculate pixel distance between probes
    const dx = x2 - x1
    const dy = y2 - y1
    const pixelDistance = Math.sqrt(dx * dx + dy * dy)

    // Guard against zero/invalid measurements
    if (pixelDistance < 1 || !Number.isFinite(pixelDistance)) {
      return { pixelsPerSvgUnit: 1, isValid: false }
    }

    // Calculate pixels per SVG unit
    const pixelsPerSvgUnit = pixelDistance / SCALE_PROBE_SVG_DISTANCE

    return { pixelsPerSvgUnit, isValid: true }
  }, [scaleProbe1Ref, scaleProbe2Ref, SCALE_PROBE_SVG_DISTANCE])

  // -------------------------------------------------------------------------
  // Direct Anchor Tracking
  // -------------------------------------------------------------------------
  /**
   * Direct approach to 1:1 tracking:
   * 1. On touch start, record anchor's INITIAL screen position
   * 2. On touch move, measure finger delta from its start position
   * 3. Apply inverse delta to cursor (in container coords via empirical scale)
   *
   * This avoids the feedback loop issue where measuring anchor error after
   * cursor update causes instability.
   */
  const anchorInitialScreenRef = useRef<{ x: number; y: number } | null>(null)

  // -------------------------------------------------------------------------
  // Refs for touch tracking (internal to this hook)
  // -------------------------------------------------------------------------
  const magnifierTouchStartRef = useRef<{ x: number; y: number } | null>(null)
  const magnifierDidMoveRef = useRef(false)
  const magnifierTapPositionRef = useRef<{ x: number; y: number } | null>(null)
  const pinchStartDistanceRef = useRef<number | null>(null)
  const pinchStartZoomRef = useRef<number | null>(null)
  // Track last zoom level to detect zoom changes and re-anchor
  const lastZoomRef = useRef<number | null>(null)

  // -------------------------------------------------------------------------
  // Helper: Calculate distance between two touch points
  // -------------------------------------------------------------------------
  const getTouchDistance = useCallback((touches: React.TouchList): number => {
    if (touches.length < 2) return 0
    const dx = touches[0].clientX - touches[1].clientX
    const dy = touches[0].clientY - touches[1].clientY
    return Math.sqrt(dx * dx + dy * dy)
  }, [])

  // -------------------------------------------------------------------------
  // Touch Start Handler
  // -------------------------------------------------------------------------
  const handleMagnifierTouchStart = useCallback(
    (e: React.TouchEvent<HTMLDivElement>) => {
      // Stop propagation to prevent map container from receiving this touch
      e.stopPropagation()

      // Handle two-finger touch (pinch start)
      if (e.touches.length === 2) {
        const distance = getTouchDistance(e.touches)
        pinchStartDistanceRef.current = distance
        pinchStartZoomRef.current = getCurrentZoom()
        // Dispatch to state machine (single source of truth for isPinching)
        interaction.dispatch({ type: 'PINCH_START' })
        setIsMagnifierExpanded(true) // Expand magnifier to fill leftover area during pinch
        // State machine handles dragging state via PINCH_START (transitions to magnifierPinching)
        magnifierTouchStartRef.current = null
        // Note: touchAction: 'none' CSS prevents browser gestures
        return
      }

      // Handle single-finger touch (pan/tap)
      if (e.touches.length === 1) {
        const touch = e.touches[0]
        magnifierTouchStartRef.current = { x: touch.clientX, y: touch.clientY }
        magnifierDidMoveRef.current = false // Reset movement tracking

        // =======================================================================
        // ANCHOR TRACKING SETUP (for empirical scale measurement)
        // =======================================================================
        // Calculate where the finger touch is in SVG coordinates
        // This becomes the anchor position - a fixed point in SVG space that we'll
        // try to keep under the finger as it moves
        if (
          magnifierRef.current &&
          svgRef.current &&
          containerRef.current &&
          cursorPositionRef.current
        ) {
          const containerRect = containerRef.current.getBoundingClientRect()
          const svgRect = svgRef.current.getBoundingClientRect()
          const magnifierRect = magnifierRef.current.getBoundingClientRect()

          // Get viewport info for coordinate conversion
          const viewBox = parseViewBoxDimensions(displayViewBox)
          const viewport = getRenderedViewport(
            svgRect,
            parsedViewBox.x,
            parsedViewBox.y,
            parsedViewBox.width,
            parsedViewBox.height
          )
          const svgOffsetX = svgRect.left - containerRect.left + viewport.letterboxX
          const svgOffsetY = svgRect.top - containerRect.top + viewport.letterboxY

          // Current cursor position in SVG coordinates (center of magnifier view)
          const cursorSvgX =
            (cursorPositionRef.current.x - svgOffsetX) / viewport.scale + parsedViewBox.x
          const cursorSvgY =
            (cursorPositionRef.current.y - svgOffsetY) / viewport.scale + parsedViewBox.y

          // Calculate magnified viewBox dimensions
          const currentZoom = getCurrentZoom()
          const magnifiedWidth = parsedViewBox.width / currentZoom
          const magnifiedHeight = parsedViewBox.height / currentZoom

          // Where did the finger touch within the magnifier? (0-1 normalized)
          const touchInMagnifierX = (touch.clientX - magnifierRect.left) / magnifierRect.width
          const touchInMagnifierY = (touch.clientY - magnifierRect.top) / magnifierRect.height

          // Convert to SVG coordinates within the magnified view
          const touchSvgX = cursorSvgX - magnifiedWidth / 2 + touchInMagnifierX * magnifiedWidth
          const touchSvgY = cursorSvgY - magnifiedHeight / 2 + touchInMagnifierY * magnifiedHeight

          // Set anchor position - this SVG coordinate should stay under the finger
          anchorSvgPositionRef.current = { x: touchSvgX, y: touchSvgY }

          // Record the finger's initial screen position for delta tracking
          // We'll use this to track how far the finger has moved from start
          anchorInitialScreenRef.current = {
            x: touch.clientX,
            y: touch.clientY,
          }

          console.log('[MagnifierTouchStart] ANCHOR SETUP:', {
            fingerScreen: { x: touch.clientX, y: touch.clientY },
            touchInMagnifier: {
              x: touchInMagnifierX.toFixed(2),
              y: touchInMagnifierY.toFixed(2),
            },
            cursorSvg: { x: cursorSvgX.toFixed(1), y: cursorSvgY.toFixed(1) },
            anchorSvg: { x: touchSvgX.toFixed(1), y: touchSvgY.toFixed(1) },
            magnifiedSize: {
              w: magnifiedWidth.toFixed(1),
              h: magnifiedHeight.toFixed(1),
            },
            zoom: currentZoom.toFixed(2),
          })

          // Record tap position relative to magnifier for tap-to-select
          magnifierTapPositionRef.current = {
            x: touch.clientX - magnifierRect.left,
            y: touch.clientY - magnifierRect.top,
          }

          // Initialize lastZoomRef for zoom change detection
          lastZoomRef.current = currentZoom
        }

        // State machine handles dragging state via TOUCH_MOVE (transitions to magnifierPanning)
        // Note: touchAction: 'none' CSS prevents scrolling
      }
    },
    [
      getTouchDistance,
      getCurrentZoom,
      interaction,
      setIsMagnifierExpanded,
      magnifierRef,
      svgRef,
      containerRef,
      cursorPositionRef,
      anchorSvgPositionRef,
      parsedViewBox,
      displayViewBox,
    ]
  )

  // -------------------------------------------------------------------------
  // Ref to throttle logging (don't spam console on every move)
  // -------------------------------------------------------------------------
  const lastLogTimeRef = useRef(0)

  // -------------------------------------------------------------------------
  // Touch Move Handler
  // -------------------------------------------------------------------------
  const handleMagnifierTouchMove = useCallback(
    (e: React.TouchEvent<HTMLDivElement>) => {
      // Stop propagation to prevent map container from receiving this touch
      e.stopPropagation()

      // Handle two-finger pinch gesture
      if (e.touches.length === 2 && isPinchingFromMachine) {
        const currentDistance = getTouchDistance(e.touches)
        const startDistance = pinchStartDistanceRef.current
        const startZoom = pinchStartZoomRef.current

        if (startDistance && startZoom && currentDistance > 0) {
          // Calculate new zoom based on pinch scale
          const scale = currentDistance / startDistance
          const newZoom = Math.max(1, Math.min(MAX_ZOOM, startZoom * scale))
          setTargetZoom(newZoom)
        }

        // Note: touchAction: 'none' CSS prevents browser zoom gestures
        return
      }

      // Handle single-finger panning
      const touchStart = magnifierTouchStartRef.current
      if (!touchStart || e.touches.length !== 1) return
      if (!cursorPositionRef.current) return
      if (!svgRef.current || !containerRef.current) return

      const touch = e.touches[0]
      const deltaX = touch.clientX - touchStart.x
      const deltaY = touch.clientY - touchStart.y

      // Get container and SVG measurements early (needed for coordinate conversion)
      const containerRect = containerRef.current.getBoundingClientRect()
      const svgRect = svgRef.current.getBoundingClientRect()

      // Convert touch position to container coordinates for state machine
      // (State machine stores touchCenter in container coords, not client coords)
      const touchContainerX = touch.clientX - containerRect.left
      const touchContainerY = touch.clientY - containerRect.top

      // Dispatch TOUCH_MOVE to state machine - this transitions to magnifierPanning phase
      // Note: position must be in container coordinates to match how map panning works
      interaction.dispatch({
        type: 'TOUCH_MOVE',
        position: { x: touchContainerX, y: touchContainerY },
        touchCount: 1,
      })

      // Track if user has moved significantly (more than 5px = definitely a drag, not a tap)
      if (Math.abs(deltaX) > 5 || Math.abs(deltaY) > 5) {
        magnifierDidMoveRef.current = true
      }

      // Update start position for next move (keep in client coords for delta calculation)
      magnifierTouchStartRef.current = { x: touch.clientX, y: touch.clientY }

      // =========================================================================
      // DIRECT DELTA TRACKING for True 1:1 Touch Tracking
      // =========================================================================
      // Simple approach that avoids feedback loops:
      // 1. On touch start, record finger's initial screen position
      // 2. On each move, calculate how far finger moved from START
      // 3. Convert that screen delta to cursor delta using empirical scale
      // 4. Apply inverse delta to cursor (dragging right moves view left)
      //
      // This uses the same "screen pixels per SVG unit" measurement but applies
      // it to the delta from start, not frame-by-frame deltas. This is more
      // stable because we're not accumulating errors.

      const currentZoom = getCurrentZoom()
      const anchorInitialScreen = anchorInitialScreenRef.current

      let cursorDeltaX: number
      let cursorDeltaY: number

      // Get empirical scale - this tells us how many screen pixels = 1 SVG unit in magnifier
      const empiricalScale = measureEmpiricalScale()
      const viewBox = parseViewBoxDimensions(displayViewBox)

      // viewport scale = container pixels per SVG unit (for the main map, not magnifier)
      const viewportScale =
        svgRect.width / viewBox.width > svgRect.height / viewBox.height
          ? svgRect.height / viewBox.height
          : svgRect.width / viewBox.width

      if (empiricalScale.isValid && anchorInitialScreen) {
        // EMPIRICAL APPROACH: Use measured scale for accurate conversion
        //
        // The user drags in screen pixels. We need to move the cursor in container pixels.
        // - In the magnifier: 1 SVG unit = empiricalScale.pixelsPerSvgUnit screen pixels
        // - In the main map: 1 SVG unit = viewportScale container pixels
        //
        // So: touchMultiplier = viewportScale / empiricalScale.pixelsPerSvgUnit
        //
        // When finger moves 1 screen pixel in magnifier:
        // - That's (1 / pixelsPerSvgUnit) SVG units
        // - Which is (viewportScale / pixelsPerSvgUnit) container pixels of cursor movement

        const touchMultiplier = viewportScale / empiricalScale.pixelsPerSvgUnit

        // Apply delta from this frame (deltaX, deltaY already calculated above)
        // Inverted because dragging right should move cursor left (panning view)
        cursorDeltaX = -deltaX * touchMultiplier
        cursorDeltaY = -deltaY * touchMultiplier

        // DEBUG: Log tracking (throttled)
        const now = performance.now()
        if (now - lastLogTimeRef.current > 200) {
          lastLogTimeRef.current = now
          console.log('[MagnifierPan] DIRECT delta tracking:', {
            frameDelta: { x: deltaX.toFixed(1), y: deltaY.toFixed(1) },
            touchMultiplier: touchMultiplier.toFixed(4),
            cursorDelta: {
              x: cursorDeltaX.toFixed(2),
              y: cursorDeltaY.toFixed(2),
            },
            empiricalPxPerSvg: empiricalScale.pixelsPerSvgUnit.toFixed(2),
            viewportScale: viewportScale.toFixed(2),
            zoom: currentZoom.toFixed(2),
          })
        }
      } else {
        // FALLBACK: Calculate touch multiplier from dimensions
        const leftoverWidth = containerRect.width - SAFE_ZONE_MARGINS.left - SAFE_ZONE_MARGINS.right
        const leftoverHeight =
          containerRect.height - SAFE_ZONE_MARGINS.top - SAFE_ZONE_MARGINS.bottom
        const { width: magnifierWidth, height: magnifierHeight } = getMagnifierDimensions(
          leftoverWidth,
          leftoverHeight
        )
        const actualMagnifierWidth = isMagnifierExpanded ? leftoverWidth : magnifierWidth
        const actualMagnifierHeight = isMagnifierExpanded ? leftoverHeight : magnifierHeight
        const touchMultiplierResult = calculateTouchMultiplier(
          {
            viewBoxWidth: viewBox.width,
            viewBoxHeight: viewBox.height,
            svgWidth: svgRect.width,
            svgHeight: svgRect.height,
          },
          {
            width: actualMagnifierWidth,
            height: actualMagnifierHeight,
            zoom: currentZoom,
          }
        )
        const touchMultiplier = touchMultiplierResult.multiplier

        // Apply delta-based calculation (inverted because dragging right should move view left)
        cursorDeltaX = -deltaX * touchMultiplier
        cursorDeltaY = -deltaY * touchMultiplier

        const now = performance.now()
        if (now - lastLogTimeRef.current > 500) {
          lastLogTimeRef.current = now
          console.log('[MagnifierPan] FALLBACK calculated tracking:', {
            touchDelta: { x: deltaX.toFixed(1), y: deltaY.toFixed(1) },
            touchMultiplier: touchMultiplier.toFixed(4),
            zoom: currentZoom.toFixed(2),
          })
        }
      }

      // Apply cursor delta and clamp to SVG bounds
      const svgOffsetX = svgRect.left - containerRect.left
      const svgOffsetY = svgRect.top - containerRect.top
      const newCursor = {
        x: cursorPositionRef.current.x + cursorDeltaX,
        y: cursorPositionRef.current.y + cursorDeltaY,
      }
      const clamped = clampToSvgBounds(newCursor, {
        left: svgOffsetX,
        top: svgOffsetY,
        width: svgRect.width,
        height: svgRect.height,
      })
      const clampedX = clamped.x
      const clampedY = clamped.y

      // Update cursor position ref for synchronous access
      cursorPositionRef.current = clamped

      // Run region detection to get hoveredRegionId and regions for adaptive zoom
      const {
        regionUnderCursor,
        detectedRegions: detectedRegionObjects,
        detectedSmallestSize,
      } = detectRegions(clampedX, clampedY)

      // Dispatch to state machine with clamped position AND regionId
      // This updates both cursor position and hovered region in one dispatch
      interaction.dispatch({
        type: 'TOUCH_MOVE',
        position: clamped,
        touchCount: 1,
        regionId: regionUnderCursor,
      })
      // hoveredRegion is now derived from state machine (interaction.hoveredRegionId)

      // Hot/cold feedback for magnifier panning
      if (hotColdEnabledRef.current && currentPrompt && !isGiveUpAnimating && !isInTakeover) {
        const targetRegion = mapData.regions.find((r) => r.id === currentPrompt)
        if (targetRegion && checkHotCold) {
          const { x: viewBoxX, y: viewBoxY, width: viewBoxW, height: viewBoxH } = parsedViewBox
          const viewport = getRenderedViewport(svgRect, viewBoxX, viewBoxY, viewBoxW, viewBoxH)
          const svgOffsetXWithLetterbox = svgRect.left - containerRect.left + viewport.letterboxX
          const svgOffsetYWithLetterbox = svgRect.top - containerRect.top + viewport.letterboxY
          const targetPixelX =
            (targetRegion.center[0] - viewBoxX) * viewport.scale + svgOffsetXWithLetterbox
          const targetPixelY =
            (targetRegion.center[1] - viewBoxY) * viewport.scale + svgOffsetYWithLetterbox
          const cursorSvgX = (clampedX - svgOffsetXWithLetterbox) / viewport.scale + viewBoxX
          const cursorSvgY = (clampedY - svgOffsetYWithLetterbox) / viewport.scale + viewBoxY

          checkHotCold({
            cursorPosition: { x: clampedX, y: clampedY },
            targetCenter: { x: targetPixelX, y: targetPixelY },
            hoveredRegionId: regionUnderCursor,
            cursorSvgPosition: { x: cursorSvgX, y: cursorSvgY },
          })
        }
      }

      // Auto-zoom based on regions at cursor position (same as map drag behavior)
      // Filter out found regions from zoom calculations
      const unfoundRegionObjects = detectedRegionObjects.filter((r) => !regionsFound.includes(r.id))

      // Calculate optimal zoom for the new cursor position
      const zoomSearchResult = findOptimalZoom({
        detectedRegions: unfoundRegionObjects,
        detectedSmallestSize,
        cursorX: clampedX,
        cursorY: clampedY,
        containerRect,
        svgRect,
        mapData,
        svgElement: svgRef.current,
        largestPieceSizesCache: largestPieceSizesRef.current,
        maxZoom: MAX_ZOOM,
        minZoom: 1,
        pointerLocked: false, // Mobile never uses pointer lock
      })

      // Set the target zoom (no re-anchoring needed with direct delta tracking)
      setTargetZoom(zoomSearchResult.zoom)

      // Broadcast cursor update to other players (if in multiplayer)
      if (
        onCursorUpdate &&
        (gameMode !== 'turn-based' || currentPlayer === localPlayerId) &&
        containerRef.current &&
        svgRef.current
      ) {
        const { x: viewBoxX, y: viewBoxY, width: viewBoxW, height: viewBoxH } = parsedViewBox
        const viewport = getRenderedViewport(svgRect, viewBoxX, viewBoxY, viewBoxW, viewBoxH)
        const svgOffsetXWithLetterbox = svgRect.left - containerRect.left + viewport.letterboxX
        const svgOffsetYWithLetterbox = svgRect.top - containerRect.top + viewport.letterboxY
        const cursorSvgX = (clampedX - svgOffsetXWithLetterbox) / viewport.scale + viewBoxX
        const cursorSvgY = (clampedY - svgOffsetYWithLetterbox) / viewport.scale + viewBoxY
        onCursorUpdate({ x: cursorSvgX, y: cursorSvgY }, regionUnderCursor)
      }

      // Note: No preventDefault() needed here - the container has touchAction: 'none' CSS
      // which prevents scrolling without triggering passive event listener warnings
    },
    [
      isPinchingFromMachine,
      isMagnifierExpanded,
      getTouchDistance,
      setTargetZoom,
      measureEmpiricalScale,
      detectRegions,
      onCursorUpdate,
      gameMode,
      currentPlayer,
      localPlayerId,
      displayViewBox,
      getCurrentZoom,
      regionsFound,
      mapData,
      currentPrompt,
      isGiveUpAnimating,
      isInTakeover,
      checkHotCold,
      svgRef,
      containerRef,
      cursorPositionRef,
      anchorSvgPositionRef,
      // Note: hoveredRegion and setHoveredRegion removed - state machine is authoritative
      hotColdEnabledRef,
      largestPieceSizesRef,
      parsedViewBox,
      interaction,
      magnifierRef,
    ]
  )

  // -------------------------------------------------------------------------
  // Touch End Handler
  // -------------------------------------------------------------------------
  const handleMagnifierTouchEnd = useCallback(
    (e: React.TouchEvent<HTMLDivElement>) => {
      const currentPhase = interaction.state.mode === 'mobile' ? interaction.state.phase : 'N/A'
      console.log('[handleMagnifierTouchEnd] Called', {
        currentPhase,
        isPinchingFromMachine,
        touchesLength: e.touches.length,
        changedTouchesLength: e.changedTouches.length,
        didMove: magnifierDidMoveRef.current,
      })
      // Always stop propagation to prevent map container from receiving touch end
      // (which would trigger dismissMagnifier via handleMapTouchEnd)
      e.stopPropagation()

      // Reset pinch state
      if (isPinchingFromMachine) {
        // Dispatch to state machine (single source of truth for isPinching)
        interaction.dispatch({ type: 'PINCH_END' })
        pinchStartDistanceRef.current = null
        pinchStartZoomRef.current = null
        // If still have one finger down, don't reset drag state - they might continue panning
        if (e.touches.length === 1) {
          // User lifted one finger but still has one down - start panning
          const touch = e.touches[0]
          magnifierTouchStartRef.current = {
            x: touch.clientX,
            y: touch.clientY,
          }
          // State machine will transition to magnifierPanning on TOUCH_MOVE
        }
        return
      }

      // Check if this was a tap (no significant movement) vs a drag
      // If the user just tapped on the magnifier, select the region at the tap position
      const didMove = magnifierDidMoveRef.current
      const tapPosition = magnifierTapPositionRef.current

      // Dispatch TOUCH_END to state machine - this transitions from magnifierPanning back to magnifierActive
      interaction.dispatch({
        type: 'TOUCH_END',
        touchCount: e.touches.length, // Number of fingers still touching
      })
      console.log(
        '[handleMagnifierTouchEnd] After dispatch, new phase:',
        interaction.state.mode === 'mobile' ? interaction.state.phase : 'N/A'
      )

      // State machine is authoritative for dragging state (magnifierPanning phase)
      magnifierTouchStartRef.current = null
      magnifierDidMoveRef.current = false
      magnifierTapPositionRef.current = null

      // Clear anchor tracking refs
      anchorSvgPositionRef.current = null
      anchorInitialScreenRef.current = null

      // If there was a changed touch that ended and it wasn't a drag, check for tap-to-select
      if (e.changedTouches.length === 1 && !didMove && tapPosition) {
        // Convert tap position on magnifier to SVG coordinates
        if (
          magnifierRef.current &&
          svgRef.current &&
          containerRef.current &&
          cursorPositionRef.current
        ) {
          const magnifierRect = magnifierRef.current.getBoundingClientRect()
          const containerRect = containerRef.current.getBoundingClientRect()
          const svgRect = svgRef.current.getBoundingClientRect()

          // Get the current zoom level
          const currentZoom = zoomSpring.get()

          // Parse the main map viewBox
          const { x: viewBoxX, y: viewBoxY, width: viewBoxW, height: viewBoxH } = parsedViewBox

          // Get viewport info for coordinate conversion
          const viewport = getRenderedViewport(svgRect, viewBoxX, viewBoxY, viewBoxW, viewBoxH)
          const svgOffsetX = svgRect.left - containerRect.left + viewport.letterboxX
          const svgOffsetY = svgRect.top - containerRect.top + viewport.letterboxY

          // Current cursor position in SVG coordinates (center of magnifier view)
          const cursorSvgX = (cursorPositionRef.current.x - svgOffsetX) / viewport.scale + viewBoxX
          const cursorSvgY = (cursorPositionRef.current.y - svgOffsetY) / viewport.scale + viewBoxY

          // Magnifier viewBox dimensions
          const magnifiedWidth = viewBoxW / currentZoom
          const magnifiedHeight = viewBoxH / currentZoom

          // Convert tap position (relative to magnifier) to SVG coordinates
          // Tap at (0,0) is top-left of magnifier = cursorSvg - magnifiedSize/2
          // Tap at (magnifierWidth, magnifierHeight) is bottom-right = cursorSvg + magnifiedSize/2
          const tapSvgX =
            cursorSvgX - magnifiedWidth / 2 + (tapPosition.x / magnifierRect.width) * magnifiedWidth
          const tapSvgY =
            cursorSvgY -
            magnifiedHeight / 2 +
            (tapPosition.y / magnifierRect.height) * magnifiedHeight

          // Convert SVG coordinates back to container coordinates for region detection
          const tapContainerX = (tapSvgX - viewBoxX) * viewport.scale + svgOffsetX
          const tapContainerY = (tapSvgY - viewBoxY) * viewport.scale + svgOffsetY

          // Run region detection at the tap position
          const { regionUnderCursor } = detectRegions(tapContainerX, tapContainerY)

          if (regionUnderCursor && !celebration) {
            const region = mapData.regions.find((r) => r.id === regionUnderCursor)
            if (region) {
              handleRegionClickWithCelebration(regionUnderCursor, region.name)
            }
          }
        }
      }
    },
    [
      isPinchingFromMachine,
      interaction,
      detectRegions,
      mapData.regions,
      handleRegionClickWithCelebration,
      celebration,
      zoomSpring,
      magnifierRef,
      svgRef,
      containerRef,
      cursorPositionRef,
      parsedViewBox,
      anchorSvgPositionRef,
    ]
  )

  return {
    handleMagnifierTouchStart,
    handleMagnifierTouchMove,
    handleMagnifierTouchEnd,
  }
}