import { __ } from '@wordpress/i18n';
import { colord } from 'colord';
import { InputWithSlider } from './input-with-slider';
import type { RgbInputProps } from './types';

export const RgbInput = ( { color, onChange, enableAlpha }: RgbInputProps ) => {
	const { r, g, b, a } = color.toRgb();

	return (
		<>
			<InputWithSlider
				min={ 0 }
				max={ 255 }
				label={ __( 'Red' ) }
				abbreviation="R"
				value={ r }
				onChange={ ( nextR: number ) =>
					onChange( colord( { r: nextR, g, b, a } ) )
				}
			/>
			<InputWithSlider
				min={ 0 }
				max={ 255 }
				label={ __( 'Green' ) }
				abbreviation="G"
				value={ g }
				onChange={ ( nextG: number ) =>
					onChange( colord( { r, g: nextG, b, a } ) )
				}
			/>
			<InputWithSlider
				min={ 0 }
				max={ 255 }
				label={ __( 'Blue' ) }
				abbreviation="B"
				value={ b }
				onChange={ ( nextB: number ) =>
					onChange( colord( { r, g, b: nextB, a } ) )
				}
			/>
			{ enableAlpha && (
				<InputWithSlider
					min={ 0 }
					max={ 100 }
					label={ __( 'Alpha (RGB A value)' ) }
					abbreviation="A"
					value={ Math.trunc( a * 100 ) }
					onChange={ ( nextA: number ) =>
						onChange(
							colord( {
								r,
								g,
								b,
								a: nextA / 100,
							} )
						)
					}
				/>
			) }
		</>
	);
};
