import { Switch } from '@mantine/core';
import { useOptionsContext } from './Options.context';

export function OptionSwitch({ label, description, error, path, ...rest }) {
  const { getOptions, updateOptions } = useOptionsContext();

  const handleEnabledChange = value => {
    updateOptions(path, value);
  };

  return (
    <Switch
      size="xs"
      checked={getOptions(path)}
      onChange={event => handleEnabledChange(event.currentTarget.checked)}
      label={label}
      description={description}
      error={error}
      labelPosition="right"
      {...rest}
    />
  );
}
