"use client"

import { useState } from "react"

import { Button } from "@scandic-hotels/design-system/Button"
import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon"

import ButtonLink from "@/components/ButtonLink"
import JsonToHtml from "@/components/JsonToHtml"

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 className={styles.teaserCardSidepeek}>
      <Button
        onPress={() => setSidePeekIsOpen(true)}
        variant="Secondary"
        color="Primary"
        size="Small"
        typography="Body/Supporting text (caption)/smBold"
      >
        {button.call_to_action_text}
        <MaterialIcon icon="chevron_right" size={20} color="CurrentColor" />
      </Button>
      <SidePeek
        title={heading}
        isOpen={sidePeekIsOpen}
        handleClose={() => setSidePeekIsOpen(false)}
        openInRoot
      >
        <JsonToHtml
          nodes={content.json.children}
          embeds={content.embedded_itemsConnection.edges}
        />
        <div className={styles.ctaContainer}>
          {primary_button && (
            <ButtonLink
              variant="Primary"
              color="Primary"
              size="Small"
              typography="Body/Supporting text (caption)/smBold"
              href={primary_button.href}
              target={primary_button.openInNewTab ? "_blank" : undefined}
            >
              {primary_button.title}
            </ButtonLink>
          )}
          {secondary_button && (
            <ButtonLink
              variant="Secondary"
              color="Primary"
              size="Small"
              typography="Body/Supporting text (caption)/smBold"
              href={secondary_button.href}
              target={secondary_button.openInNewTab ? "_blank" : undefined}
            >
              {secondary_button.title}
            </ButtonLink>
          )}
        </div>
      </SidePeek>
    </div>
  )
}
