/// file: public/assets/js/modules/home-diamond/pointer-smooth.js

import { POINTER_RETURN_SPEED, POINTER_SMOOTH_SPEED } from './constants.js';

/**
 * Exponential smoothing toward pointer; drifts to center when inactive.
 * @param {number} smoothSpeed
 * @param {number} returnSpeed
 */
export function createPointerSmoother(
  smoothSpeed = POINTER_SMOOTH_SPEED,
  returnSpeed = POINTER_RETURN_SPEED
) {
  let nx = 0.5;
  let ny = 0.5;
  let vx = 0;
  let vy = 0;

  /**
   * @param {number} dt
   * @param {number} targetNx
   * @param {number} targetNy
   * @param {boolean} active
   */
  function step(dt, targetNx, targetNy, active) {
    const cx = active ? targetNx : 0.5;
    const cy = active ? targetNy : 0.5;
    const k = active ? smoothSpeed : returnSpeed;
    const t = 1 - Math.exp(-k * dt);
    nx += (cx - nx) * t;
    ny += (cy - ny) * t;
    vx = (cx - nx) * k * 0.08;
    vy = (cy - ny) * k * 0.08;
    return { nx, ny, vx, vy };
  }

  function reset() {
    nx = 0.5;
    ny = 0.5;
    vx = 0;
    vy = 0;
  }

  return { step, reset, get nx() { return nx; }, get ny() { return ny; } };
}
