'use client'

import { useState } from 'react'
import { useIntl } from 'react-intl'

import { MaterialIcon } from '../../Icons/MaterialIcon'
import { JsonToHtml } from '../../JsonToHtml/JsonToHtml'
import { Button } from '../../Button'
import SidePeek from '../../SidePeek'

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

import type { AlertSidepeekProps } from './sidepeek'

export default function AlertSidepeek({
  ctaText,
  sidePeekContent,
}: AlertSidepeekProps) {
  const intl = useIntl()
  const [sidePeekIsOpen, setSidePeekIsOpen] = useState(false)
  const { heading, content } = sidePeekContent

  return (
    <div className={styles.alertSidepeek}>
      <Button
        onPress={() => setSidePeekIsOpen(true)}
        variant="Text"
        color="Primary"
        size="Small"
        wrapping
        typography="Body/Supporting text (caption)/smBold"
      >
        {ctaText}
        <MaterialIcon icon="chevron_right" size={20} color="CurrentColor" />
      </Button>

      <SidePeek
        title={heading}
        isOpen={sidePeekIsOpen}
        handleClose={() => setSidePeekIsOpen(false)}
        closeLabel={intl.formatMessage({
          defaultMessage: 'Close',
        })}
      >
        <JsonToHtml
          nodes={content.json.children}
          embeds={content.embedded_itemsConnection.edges}
        />
      </SidePeek>
    </div>
  )
}
