import { clsx } from 'clsx';

export interface RadioButtonProps<T extends string | number = string> {
  id?: string;
  name: string;
  checked?: boolean;
  onChange?: (value: T) => void;
  disabled?: boolean;
  value?: T;
  readOnly?: boolean;
}

export default function RadioButton<T extends string | number = ''>({
  id,
  value = '' as T,
  name,
  checked,
  onChange,
  disabled,
  readOnly,
}: RadioButtonProps<T>) {
  return (
    <>
      <input
        type="radio"
        className="sr-only"
        id={id}
        value={value}
        name={name}
        checked={checked}
        disabled={disabled || readOnly}
        onChange={() => {
          if (!checked) {
            onChange?.(value);
          }
        }}
      />
      <span className={clsx('tw-radio-button', { checked, disabled: disabled || readOnly })}>
        <span className="tw-radio-check" />
      </span>
    </>
  );
}
