import { getIntl } from "@/i18n"

import { truncateTextAfterLastPeriod } from "../truncate"

import type { RawMetadataSchema } from "@/types/trpc/routers/contentstack/metadata"

export async function getDestinationCountryPageDescription(
  data: RawMetadataSchema
) {
  const intl = await getIntl()

  if (!data.destinationData?.location) {
    return null
  }

  const { hotelCount, location, cities } = data.destinationData

  let destinationCountryDescription: string | null = null

  if (!hotelCount) {
    destinationCountryDescription = intl.formatMessage(
      {
        defaultMessage:
          "Discover {location}. Enjoy your stay at a Scandic hotel. Book now!",
      },
      { location }
    )
  } else if (!cities || cities.length < 2) {
    destinationCountryDescription = intl.formatMessage(
      {
        defaultMessage:
          "Discover all our {hotelCount} Scandic hotels in {location}. Enjoy your stay at a Scandic hotel. Book now!",
      },
      { hotelCount, location }
    )
  } else {
    destinationCountryDescription = intl.formatMessage(
      {
        defaultMessage:
          "Discover all our {hotelCount} Scandic hotels in {location}. Explore {city1}, {city2}, and more! All while enjoying your stay at a Scandic hotel. Book now!",
      },
      {
        hotelCount: hotelCount,
        location: location,
        city1: cities[0],
        city2: cities[1],
      }
    )
  }

  return truncateTextAfterLastPeriod(destinationCountryDescription)
}
