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 { minResolutionValueSelector, 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 MinResolutionValue({ disabled = false }: PropTypes) {
  const dispatch = useAppDispatch();
  const settings = useFormSettings();
  const size = settings.size as GeneralSizeProp;

  const scheme = useAppSelector(schemeSelector);
  const minResolutionValue = useAppSelector(minResolutionValueSelector);

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

export default MinResolutionValue;
