import React from 'react';

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

import { getInputPropsByName } from '@/modules/@common/Form/helpers';
import { schemeSelector, versionAttributeNameSelector } 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;
};

const MAX_LENGTH = 100;

function VersionAttributeName({ disabled = false }: PropTypes) {
  const dispatch = useAppDispatch();
  const { size } = useFormSettings();

  const scheme = useAppSelector(schemeSelector);
  const versionAttributeName = useAppSelector(versionAttributeNameSelector);

  return (
    <TextField
      placeholder="Enter Attribute Name"
      size={size as GeneralSizeProp}
      value={versionAttributeName}
      inputProps={{ maxLength: MAX_LENGTH }}
      disabled={disabled}
      onChange={(e) => dispatch(setAction({ versionAttributeName: e.target.value }))}
      {...getInputPropsByName(scheme, ['versionAttributeName'])}
      onFocus={() => dispatch(removeErrorByPropAction(['versionAttributeName']))}
      label=""
    />
  );
}

export default VersionAttributeName;
