import { getIntl } from "@/i18n"

import type { MeetingRooms } from "@/types/components/hotelPage/meetingRooms"

export async function getConferenceRoomTexts(
  meetingRooms: MeetingRooms | null
) {
  if (!meetingRooms) {
    return { seatingText: null, roomText: null }
  }
  const roomSizes = meetingRooms.map((room) => room.attributes.size)
  const roomSeating = meetingRooms
    .map((room) => {
      return room.attributes.seatings.map((seating) => seating.capacity)
    })
    .flat()

  const seatingText = await getSeatingText(roomSeating)
  const roomText = await getRoomText(roomSizes)

  return { seatingText, roomText }
}

export async function getRoomText(roomSizes: number[]) {
  const largestRoom = Math.max(...roomSizes)
  const smallestRoom = Math.min(...roomSizes)
  const intl = await getIntl()

  let roomText
  if (largestRoom === smallestRoom) {
    roomText = intl.formatMessage(
      {
        defaultMessage: "{value} m²",
      },
      { value: largestRoom }
    )
  } else if (smallestRoom != null && largestRoom) {
    {
      roomText = intl.formatMessage(
        {
          defaultMessage: "{smallest}–{largest} m²",
        },
        { largest: largestRoom, smallest: smallestRoom }
      )
    }
  }
  return roomText
}

export async function getSeatingText(roomSeating: number[]) {
  const biggestSeating = Math.max(...roomSeating)
  const smallestSeating = Math.min(...roomSeating)
  const intl = await getIntl()

  let seatingText
  if (biggestSeating === smallestSeating) {
    seatingText = intl.formatMessage(
      {
        defaultMessage: "{value, plural, one {# guest} other {# guests}}",
      },
      { value: biggestSeating }
    )
  } else if (smallestSeating != null && biggestSeating) {
    {
      seatingText = intl.formatMessage(
        {
          defaultMessage: "{lowest}–{highest} guests",
        },
        { highest: biggestSeating, lowest: smallestSeating }
      )
    }
  }
  return seatingText
}
