"use client"

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

import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon"
import { JsonToHtml } from "@scandic-hotels/design-system/JsonToHtml"
import { OldDSButton as Button } from "@scandic-hotels/design-system/OldDSButton"
import SidePeek from "@scandic-hotels/design-system/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)}
        theme="base"
        variant="icon"
        intent="text"
        size="small"
        wrapping
      >
        {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>
  )
}
