import { Lang } from "@/constants/languages"
import { dt } from "@/lib/dt"

import Body from "@/components/TempDesignSystem/Text/Body"
import { getIntl } from "@/i18n"
import { getLang } from "@/i18n/serverContext"
import { getFriendsMembership } from "@/utils/user"

import type { UserProps } from "@/types/components/myPages/user"

export default async function ExpiringPoints({ user }: UserProps) {
  const intl = await getIntl()
  const membership = user.loyalty ? getFriendsMembership(user.loyalty) : null

  if (!membership || !membership.pointsToExpire) {
    //  TODO: handle this case?
    return null
  }
  const d = dt(membership.pointsExpiryDate)

  const lang = await getLang()
  const dateFormat = lang == Lang.fi ? "DD.MM.YYYY" : "YYYY-MM-DD"

  return (
    <section>
      <Body color="white" textTransform="bold" textAlign="center">
        {intl.formatMessage(
          {
            defaultMessage: "{points} spendable points expiring by {date}",
          },
          {
            points: intl.formatNumber(membership.pointsToExpire),
            date: d.format(dateFormat),
          }
        )}
      </Body>
    </section>
  )
}
