export {
  getMeetingCalendarEvents,
  getChangedMeetingDate,
  MEETING_CHANGED_EVENT,
  MEETING_CHANGED_STORAGE_KEY,
  type MeetingCalendarEvent,
  type MeetingCalendarEventColor,
  type MeetingCalendarIssue,
  type MeetingCalendarStatus,
} from "@/features/pswg/meeting-request/service/meeting-calendar-service";

export { getMeetingById } from "@/features/ministry/meeting-calendar/meeting-calendar-service";

import type { MeetingCalendarEvent } from "@/features/pswg/meeting-request/service/meeting-calendar-service";

export function getCalendarEventVenue(event: MeetingCalendarEvent) {
  const venue = event.meetingUpdate?.solutionIndicatorsDescription?.trim();

  if (venue && venue !== "—") {
    return venue;
  }

  return event.governmentAgency || "—";
}

export function getCalendarEventTimeRange(event: MeetingCalendarEvent) {
  const meetingTime = event.meetingUpdate?.meetingTime?.trim();

  if (meetingTime) {
    return meetingTime;
  }

  return event.time || "TBA";
}

export function formatCompactCalendarTimeRange(timeRange: string) {
  const match = timeRange.match(/^(.+?)\s*-\s*(.+)$/);
  if (!match) return timeRange;

  const start = match[1].trim();
  const end = match[2].trim();
  const startMeridiem = start.match(/\s(AM|PM)$/i)?.[1];
  const endMeridiem = end.match(/\s(AM|PM)$/i)?.[1];

  if (startMeridiem && endMeridiem && startMeridiem === endMeridiem) {
    const startWithoutMeridiem = start.replace(/\s(AM|PM)$/i, "").trim();
    const endWithoutMeridiem = end.replace(/\s(AM|PM)$/i, "").trim();
    return `${startWithoutMeridiem} – ${endWithoutMeridiem} ${endMeridiem}`;
  }

  return timeRange;
}
