"use client"
import { useEffect } from "react"

import useSidePeekStore from "@scandic-hotels/booking-flow/stores/sidepeek"
import { Button } from "@scandic-hotels/design-system/Button"
import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon"

import { trackOpenSidePeekEvent } from "@/utils/tracking"

import type { ReadMoreProps } from "@/types/components/hotelReservation/selectHotel/selectHotel"

export default function ReadMore({
  label,
  hotelId,
  showCTA,
  sidePeekKey,
}: ReadMoreProps) {
  const { openSidePeek, closeSidePeek } = useSidePeekStore((state) => ({
    openSidePeek: state.openSidePeek,
    closeSidePeek: state.closeSidePeek,
  }))

  useEffect(() => {
    return () => {
      closeSidePeek()
    }
  }, [closeSidePeek])

  return (
    <Button
      onPress={() => {
        openSidePeek({ key: sidePeekKey, hotelId, showCTA })
        trackOpenSidePeekEvent({
          name: sidePeekKey,
          hotelId,
          includePathname: true,
        })
      }}
      variant="Text"
      typography="Body/Paragraph/mdBold"
    >
      {label}
      <MaterialIcon icon="chevron_right" size={24} color="CurrentColor" />
    </Button>
  )
}
