'use client'

import { Button } from '@/shared/components/ui/button'
import { Spinner } from '@/shared/components/ui/spinner'
import { useReadAllNotifications } from '../hooks/use-read-all-notifications'
import { useGetNotificationsCounts } from '../hooks/use-get-notifications-counts'
import { CheckCircle2Icon } from 'lucide-react'
import { useTransition } from 'react'

export const ReadAllButton = () => {
  const [isLoading, startTransition] = useTransition()
  const { mutateAsync } = useReadAllNotifications()
  const { data, isSuccess, isPending } = useGetNotificationsCounts()

  const handleClick = () => {
    startTransition(async () => {
      await mutateAsync()
    })
  }

  return (
    <Button
      size='sm'
      variant='secondary'
      onClick={handleClick}
      disabled={isLoading || isPending || (isSuccess && data.total === 0)}
    >
      Прочитать все
      {isPending || isLoading ? <Spinner /> : <CheckCircle2Icon />}
    </Button>
  )
}
