// Touch/portrait tuning, kept dependency-free so node:test can import it.
//
// A phone in portrait is the primary screen now (Telegram Mini App), and a
// chase camera tuned for 16:9 collapses there: THREE's `fov` is VERTICAL, so
// at 390x844 a 62deg camera leaves ~31deg of horizontal view — you cannot see
// the trails you are about to hit. The rig below trades a little style for
// awareness as the viewport gets taller: wider lens, further back, higher up,
// and pitched down so the tall screen shows ground instead of sky.

const clamp01 = (v) => (v < 0 ? 0 : v > 1 ? 1 : v);
const lerp = (a, b, t) => a + (b - a) * t;

// 0 at 16:9-ish and wider, 1 at a tall phone in portrait.
export function portraitT(aspect) {
  return clamp01((1.15 - aspect) / (1.15 - 0.46));
}

// Chase-camera geometry for an aspect ratio. Landscape values are the originals.
export function camRig(aspect) {
  const t = portraitT(aspect);
  return {
    t,
    fov: lerp(62, 84, t),
    dist: lerp(12.5, 17, t),     // behind the bike
    height: lerp(7.4, 11, t),    // above the ground under it
    ahead: lerp(11, 12, t),      // look-at point in front of the bike
    lookH: lerp(1.4, 1.2, t),
  };
}

// Analog steering from a drag offset. Small dead zone so a resting thumb goes
// straight; `radius` px of travel reaches full lock. The sim clamps to [-1,1]
// and accelerates towards it, so partial values give real, gentle arcs.
export const STEER_RADIUS = 52;
export const STEER_DEAD = 6;

export function steerFromDrag(dx, radius = STEER_RADIUS, dead = STEER_DEAD) {
  const mag = Math.abs(dx);
  if (!(mag > dead)) return 0;
  const v = Math.min(1, (mag - dead) / (radius - dead));
  return dx < 0 ? -v : v;
}

export const CONTROL_SCHEMES = ['wheel', 'pads'];
export function validScheme(s) { return CONTROL_SCHEMES.includes(s) ? s : 'wheel'; }
