import { Pipe, PipeTransform } from '@angular/core';
import { formatSlotForDisplay } from '@calendar/core';
import { DateTime } from 'luxon';

/**
 * Formats a slot instant for display.
 * Prefer UTC start/end + guest IANA timezone so times follow the selector
 * (Luxon fromISO alone converts offsets into the browser local zone).
 */
@Pipe({ name: 'slotTime', standalone: true })
export class SlotTimePipe implements PipeTransform {
  transform(iso: string, timezone?: string | null): string {
    if (timezone) {
      return formatSlotForDisplay(iso, timezone);
    }
    // Keep the offset from the string (do not convert to browser local).
    const dt = DateTime.fromISO(iso, { setZone: true });
    return dt.isValid ? dt.toFormat('h:mm a') : iso;
  }
}
