import { ICalendarEvent } from '../../types/CalendarTypes';

/**
 * EventSerialization - Handles Date field serialization for IndexedDB
 *
 * IndexedDB doesn't store Date objects directly, so we convert:
 * - Date → ISO string (serialize) when writing
 * - ISO string → Date (deserialize) when reading
 */
export class EventSerialization {
  /**
   * Serialize event for IndexedDB storage
   */
  static serialize(event: ICalendarEvent): unknown {
    return {
      ...event,
      start: event.start instanceof Date ? event.start.toISOString() : event.start,
      end: event.end instanceof Date ? event.end.toISOString() : event.end
    };
  }

  /**
   * Deserialize event from IndexedDB storage
   */
  static deserialize(data: Record<string, unknown>): ICalendarEvent {
    return {
      ...data,
      start: typeof data.start === 'string' ? new Date(data.start) : data.start,
      end: typeof data.end === 'string' ? new Date(data.end) : data.end
    } as ICalendarEvent;
  }
}
