import React from 'react';

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

import { getInputPropsByName } from '@/modules/@common/Form/helpers';
import { maxDurationSelector, 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 MaxDuration({ disabled = false }: PropTypes) {
  const dispatch = useAppDispatch();
  const settings = useFormSettings();
  const size = settings.size as GeneralSizeProp;

  const scheme = useAppSelector(schemeSelector);
  const maxDuration = useAppSelector(maxDurationSelector);

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

export default MaxDuration;
