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

import { asPlayerId, asPlayerTeamId } from '../types/ids';
import { layoutPlayerTokensBottom } from '../types/appPlayers';

import { resolveLaunchPlayerIds } from './resolveLaunchPlayerIds';

void test('layoutPlayerTokensBottom spaces tokens along bottom', () => {
  const layout = layoutPlayerTokensBottom(['a', 'b', 'c'], 0.1);
  assert.equal(layout.a?.ny, 0.88);
  assert.equal(layout.a?.nx, 0.25);
  assert.equal(layout.b?.nx, 0.5);
  assert.equal(layout.c?.nx, 0.75);
});

void test('resolveLaunchPlayerIds merges players and teams with dedupe', () => {
  const teamId = asPlayerTeamId('t1');
  const players = [
    {
      id: asPlayerId('p1'),
      name: 'A',
      imageRelPath: 'x',
      sha256: '1',
      teamId,
      ringColor: '#fff',
      imageOffset: { x: 0, y: 0 },
      imageScale: 1,
    },
    {
      id: asPlayerId('p2'),
      name: 'B',
      imageRelPath: 'y',
      sha256: '2',
      teamId: null,
      ringColor: '#fff',
      imageOffset: { x: 0, y: 0 },
      imageScale: 1,
    },
  ];
  const ids = resolveLaunchPlayerIds(players, new Set(['p1', 'p2']), new Set(['t1']));
  assert.deepEqual([...ids].sort(), ['p1', 'p2']);
});
