import { Platform } from 'react-native';
import * as Calendar from 'expo-calendar';
import { Conference } from './api';

// Opens the system "new event" sheet, so no calendar permission is needed.
export async function addToCalendar(c: Conference): Promise<void> {
  if (Platform.OS === 'web') return;
  const start = new Date(c.start);
  const end = c.end ? new Date(c.end) : new Date(start.getTime() + 2 * 3600e3);
  await Calendar.createEventInCalendarAsync({
    title: c.title,
    startDate: start,
    endDate: end,
    location: c.place ?? undefined,
    url: c.url ?? undefined,
    notes: c.organizer ? `Organizator: ${c.organizer}` : undefined,
  });
}
