import React from 'react';

import type { GeneralSizeProp } from '@/mui/types';

import { getInputPropsByName } from '@/modules/@common/Form/helpers';
import { isNumber } from '@/modules/@common/helpers/number';
import { bitrateSelector, schemeSelector } from '@/modules/feeds/Editor/redux/selectors';
import { removeErrorByPropAction, setAction } from '@/modules/feeds/Editor/redux/slices';

import { useFormSettings } from '@/modules/@common/Form';
import { useAppDispatch, useAppSelector } from '@/modules/@common/store/hooks';
import { NumberField } from '@/mui/components';

type PropTypes = {
  disabled?: boolean;
};

function Bitrate({ disabled = false }: PropTypes) {
  const dispatch = useAppDispatch();
  const settings = useFormSettings();
  const size = settings.size as GeneralSizeProp;

  const scheme = useAppSelector(schemeSelector);
  const bitrate = useAppSelector(bitrateSelector);

  return (
    <NumberField
      size={size}
      min={0}
      step={1}
      placeholder="Enter Bitrate"
      value={bitrate}
      disabled={disabled}
      onChange={(e) => {
        const value = parseInt(e.target.value, 10);
        dispatch(setAction({ bitrate: isNumber(value) ? value : ('' as unknown as number) }));
      }}
      {...getInputPropsByName(scheme, ['bitrate'])}
      onFocus={() => dispatch(removeErrorByPropAction(['bitrate']))}
      label=""
    />
  );
}

export default Bitrate;
