"use client"

import { Children, cloneElement, PropsWithChildren } from "react"
import { Dialog, DialogTrigger, Modal } from "react-aria-components"

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

type SidePeekProps = {
  activeContent: string | null
  onClose: (isOpen: boolean) => void
}
export default function SidePeek({
  children,
  onClose,
  activeContent,
}: PropsWithChildren<SidePeekProps>) {
  return (
    <DialogTrigger>
      <Modal
        className={styles.sidePeek}
        isOpen={!!activeContent}
        onOpenChange={onClose}
      >
        <Dialog className={styles.dialog}>
          {({ close }) => (
            <>
              {Children.map(children, (child) => {
                return cloneElement(child as React.ReactElement, {
                  onClose: close,
                })
              })}
            </>
          )}
        </Dialog>
      </Modal>
    </DialogTrigger>
  )
}
