import assert from 'node:assert/strict';
import test from 'node:test';
import { findRegionAtPoint, resolveDrawRegions } from '../../src/features/layout/draw/layoutDrawRegions.ts';
import type { DrawShape } from '../../src/features/layout/draw/layoutDrawTypes.ts';

function createSplitShapes(): DrawShape[] {
  return [
    {
      id: 'line-vertical',
      type: 'line',
      x1: 100,
      y1: 0,
      x2: 100,
      y2: 200,
      orientation: 'vertical',
      label: '',
    },
    {
      id: 'line-horizontal',
      type: 'line',
      x1: 0,
      y1: 120,
      x2: 100,
      y2: 120,
      orientation: 'horizontal',
      label: '',
    },
  ];
}

test('resolves line-divided empty areas into separate selectable regions', () => {
  const regions = resolveDrawRegions(createSplitShapes(), 200, 200);

  assert.equal(regions.length, 3);
  assert.equal(findRegionAtPoint(regions, 50, 60)?.key, regions[0]?.key);
  assert.equal(findRegionAtPoint(regions, 50, 160)?.key, regions[1]?.key);
  assert.equal(findRegionAtPoint(regions, 150, 80)?.key, regions[2]?.key);
});

test('maps stored region labels and fill colors back onto the computed region', () => {
  const baseShapes = createSplitShapes();
  const baseRegions = resolveDrawRegions(baseShapes, 200, 200);
  const leftTopRegion = findRegionAtPoint(baseRegions, 50, 60);

  assert.ok(leftTopRegion);

  const regions = resolveDrawRegions(
    [
      ...baseShapes,
      {
        id: 'region-1',
        type: 'region',
        regionKey: leftTopRegion.key,
        label: '거실',
        fillColor: '#bfdbfe',
      },
    ],
    200,
    200,
  );
  const resolved = findRegionAtPoint(regions, 50, 60);

  assert.equal(resolved?.label, '거실');
  assert.equal(resolved?.fillColor, '#bfdbfe');
});
