/**
 * How much of a board's baked vertical exaggeration is in force at a stand-off.
 *
 * The merged California is built at 15x so the Sierra reads from orbit, and
 * ramps toward San Francisco's own 5.78x as the camera comes in. The number
 * this returns is the `scale.y` on the ground group: 1 at the state pose,
 * `near / built` once you are inside a city.
 *
 * Stand-off, not altitude. Altitude is measured against the ground, and this
 * moves the ground — driving the ramp from camera height is a feedback loop.
 * Interpolated on the log of the stand-off because the chapter rungs themselves
 * are spaced logarithmically.
 */

export const RELIEF_FAR_M = 160_000;
export const RELIEF_NEAR_M = 9_000;

export function reliefScaleAt(
  standoffM: number,
  nearExaggeration: number,
  builtExaggeration: number,
): number {
  if (!(builtExaggeration > 0) || Math.abs(nearExaggeration - builtExaggeration) < 1e-6) {
    return 1;
  }
  const t = Math.min(
    1,
    Math.max(
      0,
      Math.log(Math.max(standoffM, RELIEF_NEAR_M) / RELIEF_NEAR_M) /
        Math.log(RELIEF_FAR_M / RELIEF_NEAR_M),
    ),
  );
  return (nearExaggeration + (builtExaggeration - nearExaggeration) * t) / builtExaggeration;
}

/**
 * World Y of a surface point after the ground group's scale.y is `scale`.
 *
 * Camera targets live in world space. The ground group does not. A look-at
 * written from `world.groundAt` (baked, 15x) against a group at 0.385 is a
 * look-at hanging in the air above downtown. Multiply by the current scale.
 */
export function surfaceWorldY(groundAt: number, scale: number): number {
  return groundAt * scale;
}
