import { getIntl } from "@/i18n"
import { isFacilityCard, setFacilityCardGrids } from "@/utils/facilityCards"

import ActivitiesCardGrid from "./CardGrid/ActivitiesCardGrid"
import FacilitiesCardGrid from "./CardGrid"

import styles from "./facilities.module.css"

import {
  type Facilities,
  type FacilitiesProps,
  FacilityCardButtonText,
  type FacilityCardType,
  type FacilityGrid,
} from "@/types/components/hotelPage/facilities"
import { HotelHashValues } from "@/types/enums/hotelPage"

export default async function Facilities({
  facilities,
  amenities,
  healthFacilities,
  activitiesCards,
}: FacilitiesProps) {
  const intl = await getIntl()

  const facilityCardGrids = setFacilityCardGrids(
    facilities,
    amenities,
    healthFacilities
  )

  const translatedFacilityGrids: Facilities = facilityCardGrids.map(
    (cardGrid: FacilityGrid) => {
      return cardGrid.map((card: FacilityCardType) => {
        if (isFacilityCard(card)) {
          return {
            ...card,
            secondaryButton: {
              ...card.secondaryButton,
              title: translateButtonText(card.secondaryButton.title),
            },
          }
        }
        return card
      })
    }
  )

  function translateButtonText(text: string) {
    switch (text) {
      case FacilityCardButtonText.MEETINGS:
      case FacilityCardButtonText.RESTAURANT:
      case FacilityCardButtonText.WELLNESS:
        return intl.formatMessage({
          defaultMessage: "Read more",
        })
      default:
        console.warn(`Unsupported option given: ${text}`)
        return intl.formatMessage({
          defaultMessage: "Read more",
        })
    }
  }

  return (
    <section className={styles.facilitiesSection}>
      {translatedFacilityGrids.map((cardGrid: FacilityGrid) => (
        <FacilitiesCardGrid
          key={cardGrid[0].id}
          facilitiesCardGrid={cardGrid}
        />
      ))}
      {activitiesCards.length ? (
        <div id={HotelHashValues.activities} className={styles.activitiesCards}>
          {activitiesCards.map((card) => (
            <ActivitiesCardGrid
              key={card.upcoming_activities_card.contentPage.href}
              {...card.upcoming_activities_card}
            />
          ))}
        </div>
      ) : null}
    </section>
  )
}
