import React from 'react';

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

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

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

  const scheme = useAppSelector(schemeSelector);
  const url = useAppSelector(urlSelector);

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

export default Url;
