import { serverClient } from "@/lib/trpc/server"

import LoginButton from "@/components/Current/Header/LoginButton"
import ArrowRight from "@/components/Icons/ArrowRight"
import { ScandicFriends } from "@/components/Levels"
import Button from "@/components/TempDesignSystem/Button"
import Link from "@/components/TempDesignSystem/Link"
import Body from "@/components/TempDesignSystem/Text/Body"
import Title from "@/components/TempDesignSystem/Text/Title"
import { getIntl } from "@/i18n"

import Contact from "./Contact"

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

import type { JoinLoyaltyContactProps } from "@/types/components/loyalty/sidebar"

export default async function JoinLoyaltyContact({
  block,
}: JoinLoyaltyContactProps) {
  const { formatMessage } = await getIntl()
  const user = await serverClient().user.name()

  // Check if we have user, that means we are logged in.
  if (user) {
    return null
  }
  return (
    <section>
      <article className={styles.wrapper}>
        <Title as="h4" level="h3" textTransform="capitalize">
          {block.title}
        </Title>
        <ScandicFriends color="red" />
        {block.preamble ? <Body>{block.preamble}</Body> : null}
        {block.button ? (
          <Button
            asChild
            intent="primary"
            theme="base"
            className={styles.button}
          >
            <Link
              href={block.button.href}
              color="white"
              target={block.button.openInNewTab ? "_blank" : "_self"}
            >
              {block.button.title}
            </Link>
          </Button>
        ) : null}
        <section className={styles.loginContainer}>
          <Body>{formatMessage({ id: "Already a friend?" })}</Body>
          <LoginButton
            className={styles.link}
            trackingId="loginJoinLoyalty"
            position="join scandic friends sidebar"
            color="burgundy"
          >
            <ArrowRight
              color="burgundy"
              className={styles.icon}
              height="20"
              width="20"
            />
            {formatMessage({ id: "Log in here" })}
          </LoginButton>
        </section>
      </article>
      {block.contact ? <Contact contactBlock={block.contact} /> : null}
    </section>
  )
}
