"use client"

import { useState } from "react"

import { ChevronRightIcon } from "@/components/Icons"
import JsonToHtml from "@/components/JsonToHtml"
import Button from "@/components/TempDesignSystem/Button"

import Link from "../../Link"
import SidePeek from "../../SidePeek"

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

import type { TeaserCardSidepeekProps } from "@/types/components/teaserCard"

export default function TeaserCardSidepeek({
  button,
  sidePeekContent,
}: TeaserCardSidepeekProps) {
  const [sidePeekIsOpen, setSidePeekIsOpen] = useState(false)
  const { heading, content, primary_button, secondary_button } = sidePeekContent

  return (
    <div>
      <Button
        onPress={() => setSidePeekIsOpen(true)}
        theme="base"
        variant="icon"
        intent="text"
        size="small"
        wrapping
      >
        {button.call_to_action_text}
        <ChevronRightIcon height={20} width={20} />
      </Button>
      <SidePeek
        title={heading}
        isOpen={sidePeekIsOpen}
        handleClose={() => setSidePeekIsOpen(false)}
      >
        <JsonToHtml
          nodes={content.json.children}
          embeds={content.embedded_itemsConnection.edges}
        />
        <div className={styles.ctaContainer}>
          {primary_button && (
            <Button
              asChild
              theme="base"
              intent="primary"
              size="small"
              className={styles.ctaButton}
            >
              <Link
                href={primary_button.href}
                target={primary_button.openInNewTab ? "_blank" : undefined}
                color="none"
              >
                {primary_button.title}
              </Link>
            </Button>
          )}
          {secondary_button && (
            <Button
              asChild
              intent="secondary"
              size="small"
              className={styles.ctaButton}
            >
              <Link
                href={secondary_button.href}
                target={secondary_button.openInNewTab ? "_blank" : undefined}
              >
                {secondary_button.title}
              </Link>
            </Button>
          )}
        </div>
      </SidePeek>
    </div>
  )
}
