import * as React from "react";
import * as ScrollAreaPrimitive from "@radix-ui/react-scroll-area";

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

const ScrollArea = React.forwardRef<
  React.ElementRef<typeof ScrollAreaPrimitive.Root>,
  React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Root>
>(({ className, children, ...props }, ref) => (
  <ScrollAreaPrimitive.Root
    ref={ref}
    className={cn("relative overflow-hidden", className)}
    {...props}
  >
    <ScrollAreaPrimitive.Viewport className="tw-figr-h-full tw-figr-w-full tw-figr-rounded-b-s tw-figr-rounded-t-s tw-figr-border tw-figr-border-neutral-200 tw-figr-bg-base-white tw-figr-p-s tw-figr-font-secondary tw-figr-text-base-black">
      {children}
    </ScrollAreaPrimitive.Viewport>
    <ScrollBar />
    <ScrollAreaPrimitive.Corner />
  </ScrollAreaPrimitive.Root>
));
ScrollArea.displayName = ScrollAreaPrimitive.Root.displayName;

const ScrollBar = React.forwardRef<
  React.ElementRef<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>,
  React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>
>(({ className, orientation = "vertical", ...props }, ref) => (
  <ScrollAreaPrimitive.ScrollAreaScrollbar
    ref={ref}
    orientation={orientation}
    className={cn(
      "tw-figr-flex tw-figr-touch-none tw-figr-select-none tw-figr-transition-colors",
      orientation === "vertical" &&
        "tw-figr-h-full tw-figr-w-2.5 tw-figr-border-l tw-figr-border-l-transparent tw-figr-p-[1px]",
      orientation === "horizontal" &&
        "tw-figr-h-2.5 tw-figr-flex-col tw-figr-border-t tw-figr-border-t-transparent tw-figr-p-[1px]",
      className
    )}
    {...props}
  >
    <ScrollAreaPrimitive.ScrollAreaThumb className="tw-figr-relative tw-figr-flex-1 tw-figr-rounded-full tw-figr-bg-neutral-200" />
  </ScrollAreaPrimitive.ScrollAreaScrollbar>
));
ScrollBar.displayName = ScrollAreaPrimitive.ScrollAreaScrollbar.displayName;

export { ScrollArea, ScrollBar };
