import { type FC } from 'react';
import { Controller, useFormContext } from 'react-hook-form';

import { DesktopTimePicker } from '@material-hu/mui/x-date-pickers/DesktopTimePicker';

export type DateInputProps = {
  name: string;
  disabled: boolean;
  minutesStep?: number;
};

const TimeInput: FC<DateInputProps> = props => {
  const { name, disabled, minutesStep = 10 } = props;

  const { control } = useFormContext();

  return (
    <Controller
      name={name}
      control={control}
      render={({ field }) => (
        <DesktopTimePicker
          {...field}
          minutesStep={minutesStep}
          disabled={disabled}
          slotProps={{
            popper: {
              placement: 'auto-start',
            },
            textField: {
              disabled: true,
              onKeyDown: e => {
                e.preventDefault();
              },
            },
          }}
        />
      )}
    />
  );
};

export default TimeInput;
