import {
  Select,
  SelectGroup,
  SelectSeparator,
  SelectValue,
  SelectContent as ShadcnSelectContent,
  SelectItem as ShadcnSelectItem,
  SelectLabel as ShadcnSelectLabel,
  SelectScrollDownButton as ShadcnSelectScrollDownButton,
  SelectScrollUpButton as ShadcnSelectScrollUpButton,
  SelectTrigger as ShadcnSelectTrigger,
} from '@/shadcn/select';
import { cn } from '@/utils';
import * as SelectPrimitive from '@radix-ui/react-select';
import { ChevronDown, ChevronUp } from 'lucide-react';
import React from 'react';
import styles from './BaseSelect.module.css';

const BaseSelect = Select;
const BaseSelectGroup = SelectGroup;
const BaseSelectValue = SelectValue;

const BaseSelectTrigger = React.forwardRef<
  React.ElementRef<typeof ShadcnSelectTrigger>,
  React.ComponentPropsWithoutRef<typeof ShadcnSelectTrigger>
>(({ className, children, ...props }, ref) => (
  <ShadcnSelectTrigger
    ref={ref}
    className={cn(styles.BaseSelectTrigger, className)}
    {...props}
  >
    {children}
  </ShadcnSelectTrigger>
));

const BaseSelectScrollUpButton = React.forwardRef<
  React.ElementRef<typeof ShadcnSelectScrollUpButton>,
  React.ComponentPropsWithoutRef<typeof ShadcnSelectScrollUpButton>
>(({ className, ...props }, ref) => (
  <ShadcnSelectScrollUpButton
    ref={ref}
    className={cn(styles.BaseSelectScrollUpButton, className)}
    {...props}
  >
    <ChevronUp className={styles.BaseSelectScrollIcon} />
  </ShadcnSelectScrollUpButton>
));

const BaseSelectScrollDownButton = React.forwardRef<
  React.ElementRef<typeof ShadcnSelectScrollDownButton>,
  React.ComponentPropsWithoutRef<typeof ShadcnSelectScrollDownButton>
>(({ className, ...props }, ref) => (
  <ShadcnSelectScrollDownButton
    ref={ref}
    className={cn(styles.BaseSelectScrollDownButton, className)}
    {...props}
  >
    <ChevronDown className={styles.BaseSelectScrollIcon} />
  </ShadcnSelectScrollDownButton>
));

const BaseSelectContent = React.forwardRef<
  React.ElementRef<typeof ShadcnSelectContent>,
  React.ComponentPropsWithoutRef<typeof ShadcnSelectContent>
>(({ className, position, children, ...props }, ref) => (
  <ShadcnSelectContent
    ref={ref}
    className={cn(styles.BaseSelectContent, className)}
    {...props}
  >
    <BaseSelectScrollUpButton className={styles.BaseSelectScrollUpButton} />
    <SelectPrimitive.Viewport
      className={cn(
        styles.BaseSelectViewport,
        position === 'popper' && styles.BaseSelectViewportPopper,
      )}
    >
      {children}
    </SelectPrimitive.Viewport>
    <BaseSelectScrollDownButton className={styles.BaseSelectScrollDownButton} />
  </ShadcnSelectContent>
));

const BaseSelectItem = React.forwardRef<
  React.ElementRef<typeof ShadcnSelectItem>,
  React.ComponentPropsWithoutRef<typeof ShadcnSelectItem>
>(({ className, ...props }, ref) => (
  <ShadcnSelectItem
    ref={ref}
    className={cn(styles.BaseSelectItem, className)}
    {...props}
  />
));

const BaseSelectLabel = React.forwardRef<
  React.ElementRef<typeof ShadcnSelectLabel>,
  React.ComponentPropsWithoutRef<typeof ShadcnSelectLabel>
>(({ className, ...props }, ref) => (
  <ShadcnSelectLabel
    ref={ref}
    className={cn(styles.BaseSelectLabel, className)}
    {...props}
  />
));

const BaseSelectSeparator = React.forwardRef<
  React.ElementRef<typeof SelectSeparator>,
  React.ComponentPropsWithoutRef<typeof SelectSeparator>
>(({ className, ...props }, ref) => (
  <SelectSeparator
    ref={ref}
    className={cn(styles.BaseSelectSeparator, className)}
    {...props}
  />
));

BaseSelectTrigger.displayName = ShadcnSelectTrigger.displayName;
BaseSelectContent.displayName = ShadcnSelectContent.displayName;
BaseSelectItem.displayName = ShadcnSelectItem.displayName;
BaseSelectLabel.displayName = ShadcnSelectLabel.displayName;
BaseSelectSeparator.displayName = SelectSeparator.displayName;
BaseSelectScrollUpButton.displayName = ShadcnSelectScrollUpButton.displayName;
BaseSelectScrollDownButton.displayName =
  ShadcnSelectScrollDownButton.displayName;

export {
  BaseSelect,
  BaseSelectGroup,
  BaseSelectValue,
  BaseSelectTrigger,
  BaseSelectContent,
  BaseSelectLabel,
  BaseSelectItem,
  BaseSelectSeparator,
  BaseSelectScrollUpButton,
  BaseSelectScrollDownButton,
};
