import { Slot } from '@radix-ui/react-slot'
import type * as React from 'react'
import { SvgTaillessLineArrowRight1 } from '@chainlink/blocks-icons'
import { cn } from '../../utils'
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from '../DropdownMenu'
import { typographyVariants } from '../Typography'

type BreadcrumbProps = React.ComponentPropsWithoutRef<'nav'> & {
  separator?: React.ReactNode
  ref?: React.Ref<HTMLElement>
}

const Breadcrumb = ({ ref, ...props }: BreadcrumbProps) => (
  <nav ref={ref} aria-label="breadcrumb" {...props} />
)
Breadcrumb.displayName = 'Breadcrumb'

type BreadcrumbListProps = React.ComponentPropsWithoutRef<'ol'> & {
  ref?: React.Ref<HTMLOListElement>
}

const BreadcrumbList = ({ className, ref, ...props }: BreadcrumbListProps) => (
  <ol
    ref={ref}
    className={cn('flex flex-nowrap items-center gap-2', className)}
    {...props}
  />
)
BreadcrumbList.displayName = 'BreadcrumbList'

type BreadcrumbItemProps = React.ComponentPropsWithoutRef<'li'> & {
  ref?: React.Ref<HTMLLIElement>
}

const BreadcrumbItem = ({ className, ref, ...props }: BreadcrumbItemProps) => (
  <li
    ref={ref}
    className={cn('inline-flex list-none items-center gap-2', className)}
    {...props}
  />
)
BreadcrumbItem.displayName = 'BreadcrumbItem'

type BreadcrumbLinkProps = React.ComponentPropsWithoutRef<'a'> & {
  asChild?: boolean
  ref?: React.Ref<HTMLAnchorElement>
}

const BreadcrumbLink = ({
  asChild,
  className,
  ref,
  ...props
}: BreadcrumbLinkProps) => {
  const Comp = asChild ? Slot : 'a'

  return (
    <Comp
      ref={ref}
      className={typographyVariants({
        color: 'muted',
        className: cn('transition-colors hover:text-foreground', className),
      })}
      {...props}
    />
  )
}
BreadcrumbLink.displayName = 'BreadcrumbLink'

type BreadcrumbPageProps = React.ComponentPropsWithoutRef<'span'> & {
  ref?: React.Ref<HTMLSpanElement>
}

const BreadcrumbPage = ({ className, ref, ...props }: BreadcrumbPageProps) => (
  <span
    ref={ref}
    role="link"
    aria-disabled="true"
    aria-current="page"
    className={cn(
      typographyVariants({ variant: 'body-semi', color: 'foreground' }),
      className,
    )}
    {...props}
  />
)
BreadcrumbPage.displayName = 'BreadcrumbPage'

const BreadcrumbSeparator = ({
  children,
  className,
  ...props
}: React.ComponentProps<'div'>) => (
  <div
    role="presentation"
    aria-hidden="true"
    className={cn('[&>svg]:h-3.5 [&>svg]:w-3.5', className)}
    {...props}
  >
    {children ?? <SvgTaillessLineArrowRight1 color="muted" />}
  </div>
)
BreadcrumbSeparator.displayName = 'BreadcrumbSeparator'

type BreadcrumbEllipsisItem = {
  href?: string
  label: React.ReactNode
}

type RenderLinkProps = {
  href: string
  className?: string
  children: React.ReactNode
}

type BreadcrumbEllipsisProps = React.ComponentProps<'button'> & {
  items: BreadcrumbEllipsisItem[]
  renderLink?: (props: RenderLinkProps) => React.ReactNode
  defaultOpen?: boolean
}

const BreadcrumbEllipsis = ({
  className,
  items,
  renderLink,
  defaultOpen,
  ...props
}: BreadcrumbEllipsisProps) => (
  <DropdownMenu defaultOpen={defaultOpen}>
    <DropdownMenuTrigger asChild>
      <button
        type="button"
        aria-label="Show more breadcrumbs"
        className={cn(
          typographyVariants({ color: 'muted' }),
          'cursor-pointer rounded transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring',
          className,
        )}
        {...props}
      >
        ...
      </button>
    </DropdownMenuTrigger>
    <DropdownMenuContent align="start">
      {items.map((item, index) => (
        <DropdownMenuItem key={index} asChild={!!item.href}>
          {item.href ? (
            renderLink ? (
              renderLink({ href: item.href, children: item.label })
            ) : (
              <a href={item.href}>{item.label}</a>
            )
          ) : (
            item.label
          )}
        </DropdownMenuItem>
      ))}
    </DropdownMenuContent>
  </DropdownMenu>
)
BreadcrumbEllipsis.displayName = 'BreadcrumbEllipsis'

export {
  Breadcrumb,
  BreadcrumbList,
  BreadcrumbItem,
  BreadcrumbLink,
  BreadcrumbPage,
  BreadcrumbSeparator,
  BreadcrumbEllipsis,
}
export type {
  BreadcrumbEllipsisItem,
  RenderLinkProps as BreadcrumbRenderLinkProps,
}
