import React from 'react';

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

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

type PropTypes = {
  disabled?: boolean;
};

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

  const scheme = useAppSelector(schemeSelector);
  const videoDuration = useAppSelector(videoDurationSelector);

  return (
    <NumberField
      min={1}
      max={7200}
      step={1}
      placeholder="Video Duration"
      size={size as GeneralSizeProp}
      value={videoDuration}
      disabled={disabled}
      onChange={(e) => {
        dispatch(setAction({ videoDuration: parseInt(e.target.value, 10) || ('' as unknown as number) }));
      }}
      {...getInputPropsByName(scheme, ['videoDuration'])}
      onFocus={() => dispatch(removeErrorByPropAction(['videoDuration']))}
    />
  );
}

export default VideoDuration;
