import type { SavedLayoutDrawRecord } from './layoutDrawTypes.ts';

export function normalizeSavedLayoutDrawShapes(
  value: SavedLayoutDrawRecord['shapes'] | string | null | undefined,
): SavedLayoutDrawRecord['shapes'] {
  if (Array.isArray(value)) {
    return value;
  }

  if (typeof value === 'string') {
    try {
      const parsed = JSON.parse(value) as unknown;
      return Array.isArray(parsed) ? (parsed as SavedLayoutDrawRecord['shapes']) : [];
    } catch {
      return [];
    }
  }

  return [];
}

export function serializeSavedLayoutDrawShapes(shapes: SavedLayoutDrawRecord['shapes']) {
  return JSON.stringify(Array.isArray(shapes) ? shapes : []);
}
