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

import { Slot } from '@radix-ui/react-slot';
import { type VariantProps, cva } from 'class-variance-authority';

import styles from './BaseTabs.module.css';

import { cn } from '../..';

const baseTabsVariants = cva(styles.BaseTabs, {
  variants: {
    variant: {
      //   primary: styles.primary,
      //   destructive: styles.destructive,
      //   outline: styles.outline,
      //   secondary: styles.secondary,
      //   ghost: styles.ghost,
      //   link: styles.link,
    },
    size: {
      default: styles['size-default'],
      sm: styles['size-sm'],
      lg: styles['size-lg'],
    },
  },
  defaultVariants: {
    // variant: 'primary',
    size: 'default',
  },
});

const TabsList = React.forwardRef<
  React.ElementRef<typeof TabsPrimitive.List>,
  React.ComponentPropsWithoutRef<typeof TabsPrimitive.List>
>(({ className, ...props }, ref) => (
  <TabsPrimitive.List
    ref={ref}
    className={cn(styles.BaseTabsList, className)}
    {...props}
  />
));
TabsList.displayName = TabsPrimitive.List.displayName;

const TabsTrigger = React.forwardRef<
  React.ElementRef<typeof TabsPrimitive.Trigger>,
  React.ComponentPropsWithoutRef<typeof TabsPrimitive.Trigger>
>(({ className, ...props }, ref) => (
  <TabsPrimitive.Trigger
    ref={ref}
    className={cn(styles.BaseTabsTrigger, className)}
    {...props}
  />
));
TabsTrigger.displayName = TabsPrimitive.Trigger.displayName;

const TabsContent = React.forwardRef<
  React.ElementRef<typeof TabsPrimitive.Content>,
  React.ComponentPropsWithoutRef<typeof TabsPrimitive.Content>
>(({ className, ...props }, ref) => (
  <TabsPrimitive.Content
    ref={ref}
    className={cn(styles.BaseTabsContent, className)}
    {...props}
  />
));
TabsContent.displayName = TabsPrimitive.Content.displayName;

export type BaseTabsProps = TabsPrimitive.TabsProps &
  VariantProps<typeof baseTabsVariants> & {
    asChild?: boolean;
  };

const BaseTabs = React.forwardRef<HTMLDivElement, BaseTabsProps>(
  ({ className, variant, size, asChild = false, ...props }, ref) => {
    const Comp = asChild ? Slot : TabsPrimitive.Root;
    return (
      <Comp
        className={cn(
          styles.BaseTabs,
          baseTabsVariants({ variant, size, className }),
        )}
        ref={ref}
        {...props}
      />
    );
  },
);

export { BaseTabs, TabsList, TabsTrigger, TabsContent, baseTabsVariants };
