import React from 'react';

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

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

const MAX_LENGTH = 200;

function YouTubeChannelId({ disabled = false, placeholder = 'Enter Youtube Channel ID / Playlist ID' }: PropTypes) {
  const dispatch = useAppDispatch();
  const { size } = useFormSettings();

  const scheme = useAppSelector(schemeSelector);
  const youTubeChannelId = useAppSelector(youTubeChannelIdSelector);

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

export default YouTubeChannelId;
