import React from 'react';

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

import { getInputPropsByName } from '@/modules/@common/Form/helpers';
import { feedPrioritySelector, 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 { TextField } from '@/mui/components';

type PropTypes = {
  disabled?: boolean;
};

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

  const scheme = useAppSelector(schemeSelector);
  const feedPriority = useAppSelector(feedPrioritySelector);

  return (
    <TextField
      size={size}
      type="number"
      placeholder="Enter Feed Priority"
      value={feedPriority}
      disabled={disabled}
      {...getInputPropsByName(scheme, ['feedPriority'])}
      onFocus={() => dispatch(removeErrorByPropAction(['feedPriority']))}
      onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
        dispatch(setAction({ feedPriority: e.target.value as unknown as number }));
      }}
    />
  );
}

export default FeedPriority;
