import React from 'react';

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

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

const MAX_LENGTH = 100;

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

  const scheme = useAppSelector(schemeSelector);
  const description = useAppSelector(descriptionSelector);

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

export default DisplayName;
