import classNames from 'classnames'
import { HTMLInputTypeAttribute, HTMLProps, ReactNode } from 'react'
import Field from '../field'
import './style.scss'

interface SelectBaseProps {
	isInvalid?: boolean
	isLoading?: boolean
	required?: boolean
	className?: string
	field: HTMLProps<HTMLSelectElement>
	children: ReactNode
}

function SelectBase({
	isInvalid,
	isLoading,
	className,
	field,
	children,
	...rest
}: SelectBaseProps) {
	return (
		<select
			className={classNames(
				'hds-form-select',
				'hds-typography-body-200',
				'hds-font-weight-regular',
				{
					['hds-form-text-input--is-invalid']: isInvalid,
				},
				className
			)}
			{...field}
			{...rest}
		>
			{children}
		</select>
	)
}

interface SelectFieldProps {
	isInvalid?: boolean
	isLoading?: boolean
	isRequired?: boolean
	isOptional?: boolean
	id: string
	label?: ReactNode
	helperText?: ReactNode
	error?: ReactNode
	field: HTMLProps<HTMLSelectElement>
	children: ReactNode
}

export default function SelectField({
	isInvalid,
	isLoading,
	isRequired,
	isOptional,
	id,
	label,
	helperText,
	error,
	field,
	children,
	...rest
}: SelectFieldProps) {
	return (
		<Field
			label={label}
			helperText={helperText}
			error={error}
			isRequired={isRequired}
			isOptional={isOptional}
			id={id}
			layout="vertical"
		>
			<SelectBase
				isInvalid={isInvalid}
				required={isRequired}
				field={field}
				{...rest}
			>
				{children}
			</SelectBase>
		</Field>
	)
}
