const assert = require('assert');
const {
  crossAxisOffset,
  findGroupSnapCandidate,
} = require('../app-ui/display-layout');

const remoteMachine = [
  { id: 'remote-left', rect: { x: 300, y: 100, width: 240, height: 160 } },
  { id: 'remote-right', rect: { x: 540, y: 100, width: 240, height: 160 } },
];

{
  const localMachine = [
    { id: 'local', rect: { x: 380, y: 24, width: 100, height: 80 } },
  ];
  const candidate = findGroupSnapCandidate(
    localMachine,
    [{ groupId: 'remote', members: remoteMachine }],
    48
  );
  assert(candidate, 'the local display should dock above the remote machine');
  assert.strictEqual(candidate.edge, 'top');
  assert.strictEqual(candidate.sourceMemberId, 'remote-left');
  assert.strictEqual(candidate.rect.x, 380, 'snapping must preserve the dragged horizontal position');
  assert.strictEqual(candidate.rect.y, 20);
  assert.strictEqual(candidate.crossOffset, -110);
}

{
  const localMachine = [
    { id: 'local', rect: { x: 650, y: 24, width: 100, height: 80 } },
  ];
  const candidate = findGroupSnapCandidate(
    localMachine,
    [{ groupId: 'remote', members: remoteMachine }],
    48
  );
  assert(candidate, 'the local display should slide to the other remote display');
  assert.strictEqual(candidate.edge, 'top');
  assert.strictEqual(candidate.sourceMemberId, 'remote-right');
  assert.strictEqual(candidate.rect.x, 650);
  assert.strictEqual(candidate.crossOffset, 160);
}

assert.strictEqual(
  crossAxisOffset(
    { x: 100, y: 180, width: 80, height: 60 },
    { x: 200, y: 100, width: 240, height: 160 },
    'left'
  ),
  30,
  'vertical edge offsets should be measured center to center'
);

console.log('display layout perimeter tests passed');
