"use client"

import { useTheme } from "next-themes"
import { Toaster as Sonner, type ToasterProps } from "sonner"
import { CircleCheckIcon, InfoIcon, TriangleAlertIcon, OctagonXIcon, Loader2Icon } from "lucide-react"

const Toaster = ({ ...props }: ToasterProps) => {
  const { theme = "system" } = useTheme()

  return (
    <Sonner
      theme={theme as ToasterProps["theme"]}
      position="top-center"
      offset={10}
      mobileOffset={{
        top: "calc(8px + env(safe-area-inset-top, 0px))",
      }}
      gap={6}
      visibleToasts={2}
      duration={2800}
      closeButton={false}
      className="toaster group"
      icons={{
        success: <CircleCheckIcon className="size-3.5 shrink-0" />,
        info: <InfoIcon className="size-3.5 shrink-0" />,
        warning: <TriangleAlertIcon className="size-3.5 shrink-0" />,
        error: <OctagonXIcon className="size-3.5 shrink-0" />,
        loading: <Loader2Icon className="size-3.5 shrink-0 animate-spin" />,
      }}
      style={
        {
          "--normal-bg": "var(--popover)",
          "--normal-text": "var(--popover-foreground)",
          "--normal-border": "var(--border)",
          "--border-radius": "calc(var(--radius) * 0.8)",
          "--width": "min(280px, calc(var(--player-viewport-max, 480px) - 24px))",
        } as React.CSSProperties
      }
      toastOptions={{
        classNames: {
          toast:
            "cn-toast !min-h-0 !w-full !max-w-[min(280px,calc(var(--player-viewport-max,480px)-24px))] !items-center !gap-2 !rounded-lg !border !px-3 !py-2 !text-xs !shadow-md",
          title: "!text-xs !font-semibold !leading-snug",
          description: "!text-[11px] !leading-snug !text-muted-foreground",
          icon: "!mr-0 !size-3.5",
        },
      }}
      {...props}
    />
  )
}

export { Toaster }
