All files / web/src/arcade-games/know-your-world/features/debug AutoZoomDebugOverlay.tsx

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

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                                                                                                                                                                                                                                                                                                                                                                                                                     
/**
 * Auto Zoom Debug Overlay
 *
 * Shows debug visualization for the auto-zoom detection system:
 * - Detection box (50px) around cursor
 * - Region analysis with acceptance/rejection info
 * - Zoom decision details
 *
 * Only rendered when visual debug mode is enabled.
 */

'use client'

import type { RefObject } from 'react'
import type { AdaptiveZoomSearchResult, RegionZoomDecision } from '../../utils/adaptiveZoomSearch'

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

export interface DetectedRegion {
  id: string
  pixelWidth: number
  pixelHeight: number
  isVerySmall: boolean
}

export interface AutoZoomDebugOverlayProps {
  /** Current cursor position in container coordinates */
  cursorPosition: { x: number; y: number }
  /** Reference to the container element */
  containerRef: RefObject<HTMLDivElement | null>
  /** Function to detect regions at a point */
  detectRegions: (
    x: number,
    y: number
  ) => {
    detectedRegions: DetectedRegion[]
    hasSmallRegion: boolean
    detectedSmallestSize: number
  }
  /** Debug info from the zoom search algorithm */
  zoomSearchDebugInfo: AdaptiveZoomSearchResult | null
  /** Target left position for magnifier (to position overlay on opposite side) */
  targetLeft: number
  /** Function to get current zoom level */
  getCurrentZoom: () => number
  /** Current target zoom level */
  targetZoom: number
}

// ============================================================================
// Component
// ============================================================================

/**
 * Debug overlay showing auto-zoom detection visualization.
 */
export function AutoZoomDebugOverlay({
  cursorPosition,
  containerRef,
  detectRegions,
  zoomSearchDebugInfo,
  targetLeft,
  getCurrentZoom,
  targetZoom,
}: AutoZoomDebugOverlayProps) {
  const { detectedRegions, hasSmallRegion, detectedSmallestSize } = detectRegions(
    cursorPosition.x,
    cursorPosition.y
  )

  // Position on opposite side from magnifier
  const containerWidth = containerRef.current?.getBoundingClientRect().width ?? 0
  const magnifierOnLeft = targetLeft < containerWidth / 2

  return (
    <>
      {/* Detection box - 50px box around cursor */}
      <div
        style={{
          position: 'absolute',
          left: `${cursorPosition.x - 25}px`,
          top: `${cursorPosition.y - 25}px`,
          width: '50px',
          height: '50px',
          border: '2px dashed yellow',
          pointerEvents: 'none',
          zIndex: 150,
        }}
      />

      {/* Detection info overlay - opposite side from magnifier */}
      <div
        style={{
          position: 'absolute',
          bottom: '10px',
          left: magnifierOnLeft ? undefined : '10px',
          right: magnifierOnLeft ? '10px' : undefined,
          backgroundColor: 'rgba(0, 0, 0, 0.8)',
          color: 'white',
          padding: '10px',
          borderRadius: '4px',
          fontSize: '12px',
          fontFamily: 'monospace',
          pointerEvents: 'none',
          zIndex: 150,
          maxWidth: '300px',
        }}
      >
        <div>
          <strong>Detection Box (50px)</strong>
        </div>
        <div>Regions detected: {detectedRegions.length}</div>
        <div>Has small region: {hasSmallRegion ? 'YES' : 'NO'}</div>
        <div>
          Smallest size:{' '}
          {detectedSmallestSize === Number.POSITIVE_INFINITY
            ? '∞'
            : `${detectedSmallestSize.toFixed(1)}px`}
        </div>

        {/* Zoom Decision Details */}
        {zoomSearchDebugInfo && (
          <>
            <div
              style={{
                marginTop: '8px',
                paddingTop: '8px',
                borderTop: '1px solid #444',
              }}
            >
              <strong>Zoom Decision:</strong>
            </div>
            <div style={{ fontSize: '10px', marginLeft: '8px' }}>
              Final zoom: <strong>{zoomSearchDebugInfo.zoom.toFixed(1)}×</strong>
              {!zoomSearchDebugInfo.foundGoodZoom && ' (fallback to min)'}
            </div>
            <div style={{ fontSize: '10px', marginLeft: '8px' }}>
              Accepted: <strong>{zoomSearchDebugInfo.acceptedRegionId || 'none'}</strong>
            </div>
            <div style={{ fontSize: '10px', marginLeft: '8px' }}>
              Thresholds: {(zoomSearchDebugInfo.acceptanceThresholds.min * 100).toFixed(0)}% -{' '}
              {(zoomSearchDebugInfo.acceptanceThresholds.max * 100).toFixed(0)}% of magnifier
            </div>

            <div style={{ marginTop: '8px' }}>
              <strong>Region Analysis (top 3):</strong>
            </div>
            {Array.from(
              new Map<string, RegionZoomDecision>(
                zoomSearchDebugInfo.regionDecisions.map((d) => [d.regionId, d])
              ).values()
            )
              .sort((a, b) => b.importance - a.importance)
              .slice(0, 3)
              .map((decision) => {
                const marker = decision.wasAccepted ? '✓' : '✗'
                const color = decision.wasAccepted ? '#0f0' : '#888'
                return (
                  <div
                    key={`decision-${decision.regionId}`}
                    style={{
                      fontSize: '9px',
                      marginLeft: '8px',
                      color,
                    }}
                  >
                    {marker} {decision.regionId}: {decision.currentSize.width.toFixed(0)}×
                    {decision.currentSize.height.toFixed(0)}px
                    {decision.rejectionReason && ` (${decision.rejectionReason})`}
                  </div>
                )
              })}
          </>
        )}

        <div
          style={{
            marginTop: '8px',
            paddingTop: '8px',
            borderTop: '1px solid #444',
          }}
        >
          <strong>Detected Regions ({detectedRegions.length}):</strong>
        </div>
        {detectedRegions.map((region) => (
          <div key={region.id} style={{ fontSize: '10px', marginLeft: '8px' }}>
            • {region.id}: {region.pixelWidth.toFixed(1)}×{region.pixelHeight.toFixed(1)}px
            {region.isVerySmall ? ' (SMALL)' : ''}
          </div>
        ))}
        <div style={{ marginTop: '8px' }}>
          <strong>Current Zoom:</strong> {getCurrentZoom().toFixed(1)}×
        </div>
        <div>
          <strong>Target Zoom:</strong> {targetZoom.toFixed(1)}×
        </div>
      </div>
    </>
  )
}