import React from 'react';

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

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

  const scheme = useAppSelector(schemeSelector);
  const maxStories = useAppSelector(maxStoriesSelector);

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

export default MaxStories;
