'use client';

import { cn } from '@/lib/utils';

/** 40 x 22 switch with a smooth thumb. */
export function Switch({
  checked,
  onCheckedChange,
  disabled,
  id,
  label,
  labelledBy,
}: {
  checked: boolean;
  onCheckedChange: (checked: boolean) => void;
  disabled?: boolean;
  id?: string;
  label?: string;
  labelledBy?: string;
}) {
  return (
    <button
      id={id}
      type="button"
      role="switch"
      aria-checked={checked}
      aria-label={label}
      aria-labelledby={labelledBy}
      disabled={disabled}
      onClick={() => onCheckedChange(!checked)}
      className={cn(
        'relative inline-flex h-[22px] w-10 shrink-0 items-center rounded-full transition-colors duration-180 ease-brand',
        checked ? 'bg-primary' : 'bg-border-strong',
        disabled ? 'cursor-not-allowed opacity-60' : 'cursor-pointer',
      )}
    >
      <span
        aria-hidden="true"
        className={cn(
          'inline-block h-[18px] w-[18px] rounded-full bg-primary-foreground shadow-sm transition-transform duration-180 ease-brand',
          checked ? 'translate-x-[20px]' : 'translate-x-[2px]',
        )}
      />
    </button>
  );
}
