"use client"
import { AnimatePresence, motion } from "framer-motion"
import { usePathname } from "next/navigation"
import React, { useState } from "react"
import { Dialog, Modal, ModalOverlay } from "react-aria-components"
import { useIntl } from "react-intl"
import { customerService } from "@/constants/currentWebHrefs"
import { benefits } from "@/constants/routes/myPages"
import { trpc } from "@/lib/trpc/client"
import Link from "@/components/TempDesignSystem/Link"
import Caption from "@/components/TempDesignSystem/Text/Caption"
import { toast } from "@/components/TempDesignSystem/Toasts"
import useLang from "@/hooks/useLang"
import confetti from "./confetti"
import Header from "./Header"
import Initial from "./Initial"
import Navigation from "./Navigation"
import Slide from "./Slide"
import styles from "./surprises.module.css"
import type { SurprisesProps } from "@/types/components/blocks/surprises"
import type { Surprise } from "@/server/routers/contentstack/reward/output"
const MotionModal = motion(Modal)
export default function SurprisesNotification({
surprises: initialData,
}: SurprisesProps) {
const lang = useLang()
const intl = useIntl()
const pathname = usePathname()
const [open, setOpen] = useState(true)
const [[selectedSurprise, direction], setSelectedSurprise] = useState([0, 0])
const [showSurprises, setShowSurprises] = useState(false)
const utils = trpc.useUtils()
const { data: surprises } = trpc.contentstack.rewards.surprises.useQuery<
Surprise[]
>(
{
lang,
},
{
initialData,
refetchInterval: 1000 * 60 * 5, // every 5 minutes
refetchIntervalInBackground: false,
}
)
const unwrap = trpc.contentstack.rewards.unwrap.useMutation({
onSuccess: () => {
utils.contentstack.rewards.current.invalidate({ lang })
if (pathname.indexOf(benefits[lang]) !== 0) {
toast.success(
<>
{intl.formatMessage(
{
id: "{amount, plural, one {Gift} other {Gifts}} added to your benefits",
},
{ amount: surprises.length }
)}
{intl.formatMessage({ id: "Go to My Benefits" })}
>
)
}
},
onError: (error) => {
console.error("Failed to unwrap surprise", error)
toast.error(
<>
{intl.formatMessage(
{
id: "Oops! Something went wrong while showing your surprise. Please refresh the page or try again later. If the issue persists, contact the support.",
},
{
link: (str) => (
{str}
),
}
)}
>
)
},
})
const totalSurprises = surprises.length
if (!totalSurprises) {
return null
}
const surprise = surprises[selectedSurprise]
function showSurprise(newDirection: number) {
setSelectedSurprise(([currentIndex]) => [
currentIndex + newDirection,
newDirection,
])
}
async function viewRewards() {
const updates = surprises
.map((surprise) => {
const coupons = surprise.coupons
.map((coupon) => {
if (coupon.couponCode) {
return {
rewardId: surprise.id,
couponCode: coupon.couponCode,
}
}
})
.filter(
(coupon): coupon is { rewardId: string; couponCode: string } =>
!!coupon
)
return coupons
})
.flat()
unwrap.mutate(updates)
}
return (
{open && (
)}
)
}
const variants = {
enter: (direction: number) => {
return {
x: direction > 0 ? 1000 : -1000,
opacity: 0,
}
},
center: {
x: 0,
opacity: 1,
},
exit: (direction: number) => {
return {
x: direction < 0 ? 1000 : -1000,
opacity: 0,
}
},
}