import clsx from "clsx"
import type * as React from "react"
import { SvgUse } from "../../shared/SvgUse"
import { Button } from "../button"

function Pagination({ className, ...props }: React.ComponentProps<"nav">) {
  return (
    <nav
      aria-label="pagination"
      data-slot="pagination"
      className={clsx("flex justify-start", className)}
      {...props}
    />
  )
}

function PaginationContent({ className, ...props }: React.ComponentProps<"ul">) {
  return (
    <ul
      data-slot="pagination-content"
      className={clsx("flex flex-row items-center gap-1", className)}
      {...props}
    />
  )
}

function PaginationItem({ ...props }: React.ComponentProps<"li">) {
  return <li data-slot="pagination-item" {...props} />
}

type PaginationLinkProps = {
  isActive?: boolean
} & Pick<React.ComponentProps<typeof Button>, "size"> &
  React.ComponentProps<"a">

function PaginationLink({ className, isActive, size = "2", ...props }: PaginationLinkProps) {
  return (
    <Button
      asChild
      variant="outline"
      size={size}
      className={clsx(
        isActive
          ? "!border-[#0037ed54] !bg-[#0040ff08] cursor-default"
          : "!cursor-pointer !shadow-none !text-black",
        className,
      )}
    >
      <a
        aria-current={isActive ? "page" : undefined}
        data-slot="pagination-link"
        data-active={isActive}
        {...props}
      />
    </Button>
  )
}

type PaginationPreviousProps = {
  text?: string
  disabled?: boolean
} & React.ComponentProps<typeof PaginationLink>

function PaginationPrevious({
  className,
  text = "Previous",
  disabled,
  ...props
}: PaginationPreviousProps) {
  return (
    <PaginationLink
      aria-label="Go to previous page"
      aria-disabled={disabled}
      size="2"
      className={clsx(
        "!text-[#002bb7c4] sm:pl-2.5 cursor-pointer flex items-center justify-center",
        !text && "!gap-0",
        disabled && "!text-[#00083046] !cursor-default",
        className,
      )}
      {...props}
    >
      <SvgUse icon="chevron-left" className="size-6" />
      <span className="hidden sm:block">{text}</span>
    </PaginationLink>
  )
}

type PaginationNextProps = {
  text?: string
  disabled?: boolean
} & React.ComponentProps<typeof PaginationLink>

function PaginationNext({ className, text = "Next", disabled, ...props }: PaginationNextProps) {
  return (
    <PaginationLink
      aria-label="Go to next page"
      aria-disabled={disabled}
      size="2"
      className={clsx(
        "!text-[#002bb7c4] sm:pr-2.5 cursor-pointer flex items-center justify-center",
        !text && "!gap-0",
        disabled && "!text-[#00083046] !cursor-default",
        className,
      )}
      {...props}
    >
      <span className="hidden sm:block">{text}</span>
      <SvgUse icon="chevron-right" className="size-6" />
    </PaginationLink>
  )
}

type PaginationEllipsisProps = {
  text?: string
} & React.ComponentProps<"span">

function PaginationEllipsis({ className, text = "More pages", ...props }: PaginationEllipsisProps) {
  return (
    <span
      aria-hidden
      data-slot="pagination-ellipsis"
      className={clsx("flex size-8 items-center justify-center pt-1.5", className)}
      {...props}
    >
      <SvgUse icon="ellipsis" className="size-4" />
      <span className="sr-only">{text}</span>
    </span>
  )
}

export {
  Pagination,
  PaginationContent,
  PaginationLink,
  PaginationItem,
  PaginationPrevious,
  PaginationNext,
  PaginationEllipsis,
}
