import assert from 'node:assert/strict';
import test from 'node:test';
import {
  normalizeSavedLayoutDrawShapes,
  serializeSavedLayoutDrawShapes,
} from '../../src/features/layout/draw/layoutDrawStorageShapes.ts';
import type { DrawShape } from '../../src/features/layout/draw/layoutDrawTypes.ts';

function createShapes(): DrawShape[] {
  return [
    {
      id: 'line-1',
      type: 'line',
      x1: 0,
      y1: 0,
      x2: 0,
      y2: 120,
      orientation: 'vertical',
      label: '',
    },
    {
      id: 'region-1',
      type: 'region',
      regionKey: '0:0',
      label: '거실',
      fillColor: '#dbeafe',
    },
  ];
}

test('serializes saved draw shapes into a JSON string for jsonb insert payloads', () => {
  const serialized = serializeSavedLayoutDrawShapes(createShapes());

  assert.equal(typeof serialized, 'string');
  assert.deepEqual(JSON.parse(serialized), createShapes());
});

test('normalizes stringified jsonb array values back into shape arrays', () => {
  const normalized = normalizeSavedLayoutDrawShapes(JSON.stringify(createShapes()));

  assert.deepEqual(normalized, createShapes());
});

test('falls back to an empty array for malformed saved draw shapes', () => {
  assert.deepEqual(normalizeSavedLayoutDrawShapes('{bad json'), []);
});
