import * as React from 'react'
import * as SwitchPrimitive from '@radix-ui/react-switch'
import { cn } from '../lib/cn'

export interface SwitchProps
  extends React.ComponentPropsWithoutRef<typeof SwitchPrimitive.Root> {}

export const Switch = React.forwardRef<
  React.ElementRef<typeof SwitchPrimitive.Root>,
  SwitchProps
>(({ className, ...props }, ref) => (
  <SwitchPrimitive.Root
    ref={ref}
    className={cn(
      'peer inline-flex h-4 w-8 shrink-0 cursor-pointer items-center rounded border border-control-border bg-hover',
      'transition-opacity duration-150 motion-reduce:transition-none',
      'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-focus-ring',
      'disabled:opacity-50 disabled:pointer-events-none',
      'data-[state=checked]:bg-accent data-[state=checked]:border-accent-border',
      className,
    )}
    {...props}
  >
    <SwitchPrimitive.Thumb
      className={cn(
        'pointer-events-none block h-4 w-4 rounded bg-surface',
        'transition-transform duration-150 motion-reduce:transition-none',
        'data-[state=unchecked]:translate-x-0 data-[state=checked]:translate-x-4',
        'rtl:data-[state=checked]:-translate-x-4',
      )}
    />
  </SwitchPrimitive.Root>
))
Switch.displayName = 'Switch'
