'use client';

import * as React from 'react';
import { Switch as SwitchPrimitive } from 'radix-ui';

import { cn } from '@spoon/ui';

const Switch = ({
  className,
  size = 'default',
  ...props
}: React.ComponentProps<typeof SwitchPrimitive.Root> & {
  size?: 'sm' | 'default';
}) => (
  <SwitchPrimitive.Root
    data-slot='switch'
    data-size={size}
    className={cn(
      'data-[state=checked]:bg-primary data-[state=unchecked]:bg-input focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive dark:aria-invalid:border-destructive/50 dark:data-[state=unchecked]:bg-input/80 peer group/switch relative inline-flex shrink-0 items-center rounded-full border border-transparent transition-all outline-none after:absolute after:-inset-x-3 after:-inset-y-2 focus-visible:ring-3 aria-invalid:ring-3 data-disabled:cursor-not-allowed data-disabled:opacity-50 data-[size=default]:h-[18.4px] data-[size=default]:w-[32px] data-[size=sm]:h-[14px] data-[size=sm]:w-[24px]',
      className,
    )}
    {...props}
  >
    <SwitchPrimitive.Thumb
      data-slot='switch-thumb'
      className='bg-background dark:group-data-[state=unchecked]/switch:bg-foreground dark:group-data-[state=checked]/switch:bg-primary-foreground pointer-events-none block rounded-full ring-0 transition-transform group-data-[size=default]/switch:size-4 group-data-[size=sm]/switch:size-3 group-data-[size=default]/switch:group-data-[state=checked]/switch:translate-x-[calc(100%-2px)] group-data-[size=sm]/switch:group-data-[state=checked]/switch:translate-x-[calc(100%-2px)] group-data-[size=default]/switch:group-data-[state=unchecked]/switch:translate-x-0 group-data-[size=sm]/switch:group-data-[state=unchecked]/switch:translate-x-0'
    />
  </SwitchPrimitive.Root>
);

export { Switch };
