'use client'

import { ItemGroup } from '@/shared/components/ui/item'
import { useGetNotifications } from '../hooks/use-get-notifications'
import { Empty } from './empty'
import { Loading } from './loading'
import { NotificationItem } from './item'
import { Pagination } from './pagination'
import { Typography } from '@/shared/components/ui/typography'

type NotificationListProps = {
  params: Parameters<typeof useGetNotifications>[0]
} & (
  | {
      withPagination: true
      paginationCb: (p: number) => void
    }
  | { withPagination: false }
)

export const NotificationList = ({
  params,
  ...props
}: NotificationListProps) => {
  const { data, isSuccess, isPending } = useGetNotifications(params)

  if (isPending) {
    return <Loading />
  }

  if (isSuccess && data.data?.length === 0) {
    return <Empty isNaked={!props.withPagination} />
  }

  return (
    isSuccess && (
      <>
        {props.withPagination && (
          <Typography variant='large'>
            Всего уведомлений: {data.meta.total}
          </Typography>
        )}
        <ItemGroup className='gap-4'>
          {data.data?.map((notification) => (
            <NotificationItem
              key={notification.id}
              notification={notification}
            />
          ))}
        </ItemGroup>
        {props.withPagination && (
          <Pagination meta={data.meta} onPageChange={props.paginationCb} />
        )}
      </>
    )
  )
}
