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

  const scheme = useAppSelector(schemeSelector);
  const resolutionValue = useAppSelector(resolutionValueSelector);

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

export default ResolutionValue;
