import React from 'react';

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

import { getInputPropsByName } from '@/modules/@common/Form/helpers';
import { schemeSelector, userSelector } 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;
  placeholder?: string;
  label?: string;
};

const NAME_MAX_LENGTH = 255;

function User({ disabled = false, placeholder = 'Enter Username', label = '' }: PropTypes) {
  const dispatch = useAppDispatch();
  const { size } = useFormSettings();

  const scheme = useAppSelector(schemeSelector);
  const user = useAppSelector(userSelector);

  return (
    <TextField
      size={size as GeneralSizeProp}
      placeholder={placeholder}
      value={user}
      disabled={disabled}
      {...getInputPropsByName(scheme, ['user'])}
      label={label}
      inputProps={{ maxLength: NAME_MAX_LENGTH }}
      name="external_u"
      autoComplete="off"
      onChange={(e) => dispatch(setAction({ user: e.target.value }))}
      onFocus={() => dispatch(removeErrorByPropAction(['user']))}
    />
  );
}

export default User;
