import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';

export function YearOfBirth({
	hasError = false,
	customErrorMessage = '',
	isDisabled = false,
	isRequired = true,
	value = '',
	prompt = '',
	fieldId = 'yearOfBirthField',
	inputId = 'yearOfBirth',
	inputName = 'yearOfBirth',
	fieldLabel = 'Year of birth',
	dataTrackable = 'year-of-birth',
}) {
	const inputWrapperClassNames = classNames([
		'o-forms-input',
		'o-forms-input--text',
		{ 'o-forms-input--invalid': hasError },
	]);

	const fieldTitleClassName = classNames([
		'o-forms-title',
		{ 'o-forms-field--optional': !isRequired },
	]);

	return (
		<label
			id={fieldId}
			htmlFor={inputId}
			className="o-forms-field ncf__validation-error"
			data-validate="required,number"
		>
			<span className={fieldTitleClassName}>
				<span className="o-forms-title__main">{fieldLabel}</span>
				<span className="o-forms-title__prompt">{prompt}</span>
			</span>
			<span className={inputWrapperClassNames}>
				<input
					type="text"
					id={inputId}
					name={inputName}
					placeholder="YYYY"
					autoComplete="bday-year"
					minLength="4"
					maxLength="4"
					pattern="\d{4}"
					data-trackable={dataTrackable}
					aria-required={isRequired}
					required={isRequired}
					disabled={isDisabled}
					defaultValue={value}
					inputmode="numeric"
				/>
				<span className="o-forms-input__error">
					{customErrorMessage || 'Please enter a valid year of birth'}
				</span>
			</span>
		</label>
	);
}

YearOfBirth.propTypes = {
	hasError: PropTypes.bool,
	customErrorMessage: PropTypes.string,
	isDisabled: PropTypes.bool,
	isRequired: PropTypes.bool,
	value: PropTypes.string,
	prompt: PropTypes.string,
	fieldId: PropTypes.string,
	inputId: PropTypes.string,
	inputName: PropTypes.string,
	fieldLabel: PropTypes.string,
	dataTrackable: PropTypes.string,
};
