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

import {
  clampPlayerImageOffset,
  clampPlayerImageScale,
  DEFAULT_PLAYER_IMAGE_SCALE,
  DEFAULT_PLAYER_RING_COLOR,
  normalizeAppPlayer,
  normalizeSceneNpcToken,
  PLAYER_IMAGE_OFFSET_MAX,
  PLAYER_IMAGE_SCALE_MAX,
  PLAYER_IMAGE_SCALE_MIN,
} from './appPlayers';
import { asNpcId, asSceneNpcTokenId } from './ids';

void test('clampPlayerImageOffset clamps and defaults', () => {
  assert.deepEqual(clampPlayerImageOffset(null), { x: 0, y: 0 });
  assert.deepEqual(clampPlayerImageOffset({ x: 99, y: -99 }), {
    x: PLAYER_IMAGE_OFFSET_MAX,
    y: -PLAYER_IMAGE_OFFSET_MAX,
  });
});

void test('clampPlayerImageScale clamps and defaults', () => {
  assert.equal(clampPlayerImageScale(null), DEFAULT_PLAYER_IMAGE_SCALE);
  assert.equal(clampPlayerImageScale(0.1), PLAYER_IMAGE_SCALE_MIN);
  assert.equal(clampPlayerImageScale(9), PLAYER_IMAGE_SCALE_MAX);
});

void test('normalizeAppPlayer fills ringColor and offset', () => {
  const p = normalizeAppPlayer({
    id: 'player_1',
    name: 'Ada',
    imageRelPath: 'files/a.png',
    sha256: 'abc',
  });
  assert.ok(p);
  assert.equal(p!.ringColor, DEFAULT_PLAYER_RING_COLOR);
  assert.deepEqual(p!.imageOffset, { x: 0, y: 0 });
  assert.equal(p!.imageScale, DEFAULT_PLAYER_IMAGE_SCALE);
  assert.equal(p!.teamId, null);
});

void test('normalizeAppPlayer drops unknown teamId when set provided', () => {
  const p = normalizeAppPlayer(
    {
      id: 'player_1',
      name: 'Ada',
      imageRelPath: 'files/a.png',
      sha256: 'abc',
      teamId: 'missing',
    },
    new Set(['other']),
  );
  assert.equal(p!.teamId, null);
});

void test('normalizeSceneNpcToken validates and clamps', () => {
  const t = normalizeSceneNpcToken({
    id: 'snt_1',
    npcId: 'npc_1',
    nx: 1.5,
    ny: -0.2,
    sizeN: 0.2,
  });
  assert.ok(t);
  assert.equal(t!.id, asSceneNpcTokenId('snt_1'));
  assert.equal(t!.npcId, asNpcId('npc_1'));
  assert.equal(t!.nx, 1);
  assert.equal(t!.ny, 0);
  assert.equal(normalizeSceneNpcToken({ id: 'x' }), null);
});
