import * as React from "react";
import * as SelectPrimitive from "@radix-ui/react-select";
import { Check, ChevronDown, ChevronUp } from "lucide-react";

import { cn } from "../lib/utils";

const Select = SelectPrimitive.Root;

const SelectGroup = React.forwardRef<
  React.ElementRef<typeof SelectPrimitive.Group>,
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.Group>
>(({ className, children }, ref) => (
  <SelectPrimitive.Group
    className={cn("tw-figr-text-neutral-700", className)}
    ref={ref}
  >
    {children}
  </SelectPrimitive.Group>
));

const SelectValue = SelectPrimitive.Value;

const SelectTrigger = React.forwardRef<
  React.ElementRef<typeof SelectPrimitive.Trigger>,
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger>
>(({ className, children, ...props }, ref) => (
  <SelectPrimitive.Trigger
    ref={ref}
    className={cn(
      "tw-figr-flex tw-figr-h-9 tw-figr-w-full tw-figr-items-center tw-figr-justify-between tw-figr-whitespace-nowrap tw-figr-rounded-m tw-figr-border tw-figr-border-neutral-200 tw-figr-bg-base-white tw-figr-px-3 tw-figr-py-2 tw-figr-font-secondary tw-figr-text-desktop-caption-regular tw-figr-text-base-black tw-figr-shadow-sm tw-figr-ring-transparent tw-figr-ring-offset-background placeholder:tw-figr-text-neutral-500 focus:tw-figr-outline-none focus:tw-figr-ring-1 focus:tw-figr-ring-primary disabled:tw-figr-cursor-not-allowed disabled:tw-figr-opacity-50 [&>span]:tw-figr-line-clamp-1",
      className
    )}
    {...props}
  >
    {children}
    <SelectPrimitive.Icon asChild>
      <ChevronDown className="tw-figr-h-4 tw-figr-w-4 tw-figr-opacity-50" />
    </SelectPrimitive.Icon>
  </SelectPrimitive.Trigger>
));
SelectTrigger.displayName = SelectPrimitive.Trigger.displayName;

const SelectScrollUpButton = React.forwardRef<
  React.ElementRef<typeof SelectPrimitive.ScrollUpButton>,
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollUpButton>
>(({ className, ...props }, ref) => (
  <SelectPrimitive.ScrollUpButton
    ref={ref}
    className={cn(
      "tw-figr-flex tw-figr-cursor-default tw-figr-items-center tw-figr-justify-center tw-figr-py-1",
      className
    )}
    {...props}
  >
    <ChevronUp />
  </SelectPrimitive.ScrollUpButton>
));
SelectScrollUpButton.displayName = SelectPrimitive.ScrollUpButton.displayName;

const SelectScrollDownButton = React.forwardRef<
  React.ElementRef<typeof SelectPrimitive.ScrollDownButton>,
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollDownButton>
>(({ className, ...props }, ref) => (
  <SelectPrimitive.ScrollDownButton
    ref={ref}
    className={cn(
      "tw-figr-flex tw-figr-cursor-default tw-figr-items-center tw-figr-justify-center tw-figr-py-1",
      className
    )}
    {...props}
  >
    <ChevronDown className="h-4 w-4" />
  </SelectPrimitive.ScrollDownButton>
));
SelectScrollDownButton.displayName =
  SelectPrimitive.ScrollDownButton.displayName;

const SelectContent = React.forwardRef<
  React.ElementRef<typeof SelectPrimitive.Content>,
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.Content>
>(({ className, children, position = "popper", ...props }, ref) => (
  <SelectPrimitive.Portal>
    <SelectPrimitive.Content
      ref={ref}
      className={cn(
        "tw-figr-relative tw-figr-z-50  tw-figr-max-h-96 tw-figr-min-w-[8rem] tw-figr-border-collapse  tw-figr-overflow-hidden tw-figr-rounded-b-s tw-figr-rounded-t-s tw-figr-border-neutral-200 tw-figr-bg-base-white tw-figr-font-secondary tw-figr-text-neutral-700 tw-figr-shadow-md data-[state=open]:tw-figr-animate-in data-[state=closed]:tw-figr-animate-out data-[state=closed]:tw-figr-fade-out-0 data-[state=open]:tw-figr-fade-in-0 data-[state=closed]:tw-figr-zoom-out-95 data-[state=open]:tw-figr-zoom-in-95 data-[side=bottom]:tw-figr-slide-in-from-top-2 data-[side=left]:tw-figr-slide-in-from-right-2 data-[side=right]:tw-figr-slide-in-from-left-2 data-[side=top]:tw-figr-slide-in-from-bottom-2",
        position === "popper" &&
          "data-[side=bottom]:tw-figr-translate-y-1 data-[side=left]:-tw-figr-translate-x-1 data-[side=right]:tw-figr-translate-x-1 data-[side=top]:-tw-figr-translate-y-1",
        className
      )}
      position={position}
      {...props}
    >
      <SelectScrollUpButton />
      <SelectPrimitive.Viewport
        className={cn(
          "tw-figr-p-1",
          position === "popper" &&
            "tw-figr-h-[var(--radix-select-trigger-height)] tw-figr-w-full tw-figr-min-w-[var(--radix-select-trigger-width)]"
        )}
      >
        {children}
      </SelectPrimitive.Viewport>
      <SelectScrollDownButton />
    </SelectPrimitive.Content>
  </SelectPrimitive.Portal>
));
SelectContent.displayName = SelectPrimitive.Content.displayName;

const SelectLabel = React.forwardRef<
  React.ElementRef<typeof SelectPrimitive.Label>,
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.Label>
>(({ className, ...props }, ref) => (
  <SelectPrimitive.Label
    ref={ref}
    className={cn(
      "tw-figr-px-2 tw-figr-py-1.5 tw-figr-text-desktop-caption-accent",
      className
    )}
    {...props}
  />
));
SelectLabel.displayName = SelectPrimitive.Label.displayName;

const SelectItem = React.forwardRef<
  React.ElementRef<typeof SelectPrimitive.Item>,
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.Item>
>(({ className, children, ...props }, ref) => (
  <SelectPrimitive.Item
    ref={ref}
    className={cn(
      "tw-figr-relative tw-figr-flex tw-figr-w-full tw-figr-cursor-default tw-figr-select-none tw-figr-items-center tw-figr-rounded-b-s tw-figr-rounded-t-s tw-figr-py-1.5 tw-figr-pl-2 tw-figr-pr-8 tw-figr-text-desktop-caption-regular tw-figr-outline-none focus:tw-figr-bg-neutral-100 focus:tw-figr-text-neutral-700 data-[disabled]:tw-figr-pointer-events-none data-[disabled]:tw-figr-opacity-50",
      className
    )}
    {...props}
  >
    <span className="tw-figr-absolute tw-figr-right-2 tw-figr-flex tw-figr-h-3.5 tw-figr-w-3.5 tw-figr-items-center tw-figr-justify-center">
      <SelectPrimitive.ItemIndicator>
        <Check className="tw-figr-h-4 tw-figr-w-4" />
      </SelectPrimitive.ItemIndicator>
    </span>
    <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
  </SelectPrimitive.Item>
));
SelectItem.displayName = SelectPrimitive.Item.displayName;

const SelectSeparator = React.forwardRef<
  React.ElementRef<typeof SelectPrimitive.Separator>,
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.Separator>
>(({ className, ...props }, ref) => (
  <SelectPrimitive.Separator
    ref={ref}
    className={cn(
      "-tw-figr-mx-1 tw-figr-my-1 tw-figr-h-px tw-figr-bg-neutral-100",
      className
    )}
    {...props}
  />
));
SelectSeparator.displayName = SelectPrimitive.Separator.displayName;

export {
  Select,
  SelectGroup,
  SelectValue,
  SelectTrigger,
  SelectContent,
  SelectLabel,
  SelectItem,
  SelectSeparator,
  SelectScrollUpButton,
  SelectScrollDownButton
};
