'use client'

import * as React from 'react'
import * as TabsPrimitive from '@radix-ui/react-tabs'

import { cn } from '@/lib/utils'

function Tabs({
  className,
  ...props
}: React.ComponentProps<typeof TabsPrimitive.Root>) {
  return (
    <TabsPrimitive.Root
      data-slot="tabs"
      className={cn('flex flex-col gap-2', className)}
      {...props}
    />
  )
}

function TabsList({
  className,
  orientation = 'horizontal',
  ...props
}: React.ComponentProps<typeof TabsPrimitive.List> & {
  orientation?: 'horizontal' | 'vertical'
}) {
  return (
    <TabsPrimitive.List
      data-slot="tabs-list"
      className={cn(
        'bg-input text-muted-foreground rounded-lg p-[2px]',
        orientation === 'horizontal'
          ? 'inline-flex h-8 w-fit items-center justify-center'
          : 'flex flex-col w-fit h-fit',
        className
      )}
      {...props}
    />
  )
}

function TabsTrigger({
  className,
  ...props
}: React.ComponentProps<typeof TabsPrimitive.Trigger>) {
  return (
    <TabsPrimitive.Trigger
      data-slot="tabs-trigger"
      className={cn(
        "data-[state=active]:bg-background dark:data-[state=active]:text-foreground focus-visible:border-ring/0 focus-visible:ring-ring/0 focus-visible:outline-ring dark:data-[state=active]:border-input/0 dark:data-[state=active]:bg-secondary data-[state=active]:text-foreground text-muted-foreground dark:text-muted-foreground inline-flex h-[calc(100%-1px)] flex-1 items-center justify-center gap-1.5 rounded-md border border-transparent px-1 py-1 text-xs font-medium whitespace-nowrap transition-[color,box-shadow] focus-visible:ring-[0px] focus-visible:outline-1 cursor-pointer disabled:opacity-50 disabled:cursor-not-allowed data-[state=active]:shadow-sm [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-3",
        className
      )}
      {...props}
    />
  )
}

function TabsContent({
  className,
  forceMount,
  ...props
}: React.ComponentProps<typeof TabsPrimitive.Content> & { forceMount?: true }) {
  return (
    <TabsPrimitive.Content
      data-slot="tabs-content"
      className={cn(
        'flex-1 outline-none',
        // When forceMount is used, collapse inactive tabs but keep them rendered so images preload
        forceMount && 'data-[state=inactive]:flex-none! data-[state=inactive]:h-0! data-[state=inactive]:p-0! data-[state=inactive]:overflow-hidden! data-[state=inactive]:pointer-events-none!',
        className
      )}
      forceMount={forceMount}
      {...props}
    />
  )
}

export { Tabs, TabsList, TabsTrigger, TabsContent }
