import assert from 'node:assert/strict';
import test from 'node:test';

import { snapNormToGridCell } from './sceneGridSnap';

void test('snapNormToGridCell: disabled / invalid size = no-op', () => {
  assert.deepEqual(
    snapNormToGridCell(0.33, 0.77, { enabled: false, type: 'square', sizeN: 0.1 }, 1000, 800),
    { nx: 0.33, ny: 0.77 },
  );
  assert.deepEqual(
    snapNormToGridCell(0.33, 0.77, { enabled: true, type: 'square', sizeN: 0.1 }, 0, 800),
    { nx: 0.33, ny: 0.77 },
  );
});

void test('snapNormToGridCell: square snaps to cell center', () => {
  // minDim=1000, sizeN=0.1 → cell=100. Center of (0,0) at (50,50) → (0.05, 0.05) in 1000×1000.
  const at = snapNormToGridCell(0.04, 0.06, { enabled: true, type: 'square', sizeN: 0.1 }, 1000, 1000);
  assert.ok(Math.abs(at.nx - 0.05) < 1e-9);
  assert.ok(Math.abs(at.ny - 0.05) < 1e-9);

  const next = snapNormToGridCell(0.14, 0.16, { enabled: true, type: 'square', sizeN: 0.1 }, 1000, 1000);
  assert.ok(Math.abs(next.nx - 0.15) < 1e-9);
  assert.ok(Math.abs(next.ny - 0.15) < 1e-9);
});

void test('snapNormToGridCell: hex snaps to flat-top center', () => {
  const cell = 100;
  const horiz = cell * 0.75;
  const vert = (Math.sqrt(3) / 2) * cell;
  // Center of col=1,row=0: (75, vert/2) because odd col offset.
  const cx = 1 * horiz;
  const cy = 0 * vert + vert / 2;
  const near = snapNormToGridCell(
    (cx + 3) / 1000,
    (cy - 4) / 1000,
    { enabled: true, type: 'hex', sizeN: 0.1 },
    1000,
    1000,
  );
  assert.ok(Math.abs(near.nx - cx / 1000) < 1e-9);
  assert.ok(Math.abs(near.ny - cy / 1000) < 1e-9);
});
