import React from 'react';

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

import { getInputPropsByName } from '@/modules/@common/Form/helpers';
import { nameSelector, 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 Name({ disabled = false }: PropTypes) {
  const dispatch = useAppDispatch();
  const { size } = useFormSettings();

  const scheme = useAppSelector(schemeSelector);
  const name = useAppSelector(nameSelector);

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

export default Name;
