import { getIntl } from "@/i18n"

import {
  ExternalGymDetails,
  HealthFacilitiesEnum,
  IndoorPoolDetails,
  SaunaDetails,
} from "@/types/components/hotelPage/facilities"

interface WellnessDetails {
  details: {
    value: string
    type: string
    name: string
  }[]
  type: string
}
export async function translateWellnessDetails({
  details,
  type,
}: WellnessDetails) {
  const intl = await getIntl()

  switch (type) {
    case HealthFacilitiesEnum.Sauna:
      return details
        .map(({ name, value }) => {
          switch (name) {
            case SaunaDetails.SeparateMenAndWomen:
              if (value === "True") {
                return intl.formatMessage({
                  defaultMessage: "Gender-separated sauna",
                })
              }
              return intl.formatMessage({
                defaultMessage: "Mixed-gender sauna",
              })
          }
        })
        .filter((d): d is string => !!d)

    case HealthFacilitiesEnum.OutdoorPool:
    case HealthFacilitiesEnum.IndoorPool:
      const poolDepth = await getPoolDepthTranslation(details)
      const poolTranslations = details.map(({ name, value }) => {
        switch (name) {
          case IndoorPoolDetails.Length:
            return intl.formatMessage(
              {
                defaultMessage: "Pool length: {value} m",
              },
              { value: value }
            )
          case IndoorPoolDetails.Width:
            return intl.formatMessage(
              {
                defaultMessage: "Pool width: {value} m",
              },
              { value: value }
            )
          /* TODO: Awaiting clarification from Content on how to handle these parts
            case OutdoorPoolDetails.OpenMonthFrom:
              if (type === HealthFacilitiesEnum.OutdoorPool)
                return intl.formatMessage(
                  {
                    defaultMessage: "Open from: {value}",
                  },
                  { value: value }
                )
            case OutdoorPoolDetails.OpenMonthTo:
              if (type === HealthFacilitiesEnum.OutdoorPool)
                return intl.formatMessage(
                  {
                    defaultMessage: "Open to: {value}",
                  },
                  { value: value }
                )*/
        }
      })

      return poolTranslations
        .concat(poolDepth)
        .filter((d): d is string => !!d)
        .sort()

    case HealthFacilitiesEnum.Gym:
      if (
        details.find((d) => d.name === ExternalGymDetails.ExternalGym)
          ?.value === "False"
      )
        return null
      return details
        .map(({ name, value }) => {
          switch (name) {
            case ExternalGymDetails.NameOfExternalGym:
              return intl.formatMessage(
                {
                  defaultMessage: "External gym: {value}",
                },
                { value: value }
              )
            case ExternalGymDetails.DistanceToExternalGym:
              return intl.formatMessage(
                {
                  defaultMessage: "Distance to gym: {value} m",
                },
                { value: value }
              )
          }
        })
        .filter((d): d is string => !!d)
        .sort()

    default:
      console.warn(`Unsupported type given: ${type}`)
  }
}

async function getPoolDepthTranslation(details: WellnessDetails["details"]) {
  const intl = await getIntl()

  const depthTo = details.find(
    (d) => d.name === IndoorPoolDetails.DepthTo
  )?.value
  const depthFrom = details.find(
    (d) => d.name === IndoorPoolDetails.DepthFrom
  )?.value

  if (depthFrom && depthTo) {
    return depthFrom !== depthTo
      ? intl.formatMessage(
          { defaultMessage: "Pool depth: {from}–{to} m" },
          { from: depthFrom, to: depthTo }
        )
      : intl.formatMessage(
          { defaultMessage: "Pool depth: {value} m" },
          { value: depthFrom }
        )
  }

  if (depthFrom || depthTo) {
    const value = depthFrom || depthTo
    return intl.formatMessage(
      { defaultMessage: "Pool depth: {value} m" },
      { value }
    )
  }
}
