import { tap, expect } from '@git.zone/tstest/tapbundle';

import {
  resolveWidgetPlacement,
  collectCollisions,
} from '../ts_web/elements/dees-dashboardgrid/layout.ts';
import type { DashboardWidget } from '../ts_web/elements/dees-dashboardgrid/types.ts';

tap.test('dashboardgrid does not overlap widgets after swap attempt', async () => {
  const widgets: DashboardWidget[] = [
    { id: 'w0', x: 6, y: 5, w: 1, h: 3 },
    { id: 'w1', x: 6, y: 1, w: 1, h: 3 },
    { id: 'w2', x: 3, y: 0, w: 2, h: 2 },
    { id: 'w3', x: 9, y: 0, w: 1, h: 2 },
    { id: 'w4', x: 4, y: 3, w: 1, h: 2 },
  ];

  const placement = resolveWidgetPlacement(widgets, 'w0', { x: 6, y: 3 }, 12);
  expect(placement).toBeTruthy();

  const layout = placement!.widgets;
  for (const widget of layout) {
    const collisions = collectCollisions(layout, widget, widget.x, widget.y, widget.w, widget.h);
    expect(collisions).toBeEmptyArray();
  }
});

export default tap.start();
