import * as TabsPrimitive from '@radix-ui/react-tabs'
import type * as React from 'react'
import { cn } from '../../utils'

const Tabs = TabsPrimitive.Root

Tabs.displayName = 'Tabs'

type TabsListProps = React.ComponentPropsWithoutRef<
  typeof TabsPrimitive.List
> & {
  ref?: React.Ref<React.ElementRef<typeof TabsPrimitive.List>>
}

const TabsList = ({ className, ref, ...props }: TabsListProps) => (
  <TabsPrimitive.List
    ref={ref}
    className={cn(
      'h-15 inline-flex items-center justify-center gap-6 border-0 border-b border-solid border-b-border text-muted-foreground',
      className,
    )}
    {...props}
  />
)
TabsList.displayName = TabsPrimitive.List.displayName

type TabsTriggerProps = React.ComponentPropsWithoutRef<
  typeof TabsPrimitive.Trigger
> & {
  ref?: React.Ref<React.ElementRef<typeof TabsPrimitive.Trigger>>
}

const TabsTrigger = ({ className, ref, ...props }: TabsTriggerProps) => (
  <TabsPrimitive.Trigger
    ref={ref}
    className={cn(
      '-mb-[1px] inline-flex items-center justify-center whitespace-nowrap border-0 border-b-[3px] border-transparent px-0 py-4 font-medium text-muted-foreground ring-offset-background transition-all',
      'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-4 disabled:pointer-events-none disabled:opacity-50',
      'data-[state=active]:border-b-[3px] data-[state=active]:border-link data-[state=active]:text-foreground',
      className,
    )}
    {...props}
  />
)
TabsTrigger.displayName = TabsPrimitive.Trigger.displayName

type TabsContentProps = React.ComponentPropsWithoutRef<
  typeof TabsPrimitive.Content
> & {
  ref?: React.Ref<React.ElementRef<typeof TabsPrimitive.Content>>
}

const TabsContent = ({ className, ref, ...props }: TabsContentProps) => (
  <TabsPrimitive.Content
    ref={ref}
    className={cn(
      'mt-4 ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2',
      className,
    )}
    {...props}
  />
)
TabsContent.displayName = TabsPrimitive.Content.displayName

export { Tabs, TabsList, TabsTrigger, TabsContent }
