"use client";
import type { VariantProps } from "@karma/cva";
import { cva, cx } from "@karma/cva";
import * as SliderPrimitive from "@radix-ui/react-slider";
import * as React from "react";

const sliderTrackVariants = cva({
  base: " rounded-full",
  variants: {
    variant: {
      accent: "bg-accent",
      muted: "bg-muted",
    },
  },
  defaultVariants: {
    variant: "accent",
  },
});

export type SliderProps = React.ComponentPropsWithoutRef<
  typeof SliderPrimitive.Root
> &
  VariantProps<typeof sliderTrackVariants>;

export const Slider = React.forwardRef<
  React.ElementRef<typeof SliderPrimitive.Root>,
  SliderProps
>(({ className, variant, ...props }, ref) => (
  <SliderPrimitive.Root
    ref={ref}
    className={cx(
      "relative flex w-full touch-none select-none items-center",
      className,
    )}
    {...props}
  >
    <SliderPrimitive.Track className="relative h-2 w-full grow overflow-hidden rounded-full bg-foreground">
      <SliderPrimitive.Range
        className={cx(sliderTrackVariants({ variant }), "absolute h-full")}
      />
    </SliderPrimitive.Track>
    <SliderPrimitive.Thumb className="focus-visible:ring-ring block h-5 w-5 rounded-full border-2 border-primary bg-background ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50" />
  </SliderPrimitive.Root>
));

Slider.displayName = SliderPrimitive.Root.displayName;
