import $ from 'jquery';

export const keyUpQuantityHandler = ( event: JQuery.KeyUpEvent ) => {
	const target = $( event.currentTarget );
	const value = parseInt( target.val() );

	if ( isNaN( value ) ) {
        return;
    }

	const attributeMin = target.attr( 'min' );
	const attributeMax = target.attr( 'max' );

	const min = attributeMin ? parseInt( attributeMin ) : 1;
	const max = attributeMax ? parseInt( attributeMax ) : null;

	if ( value < min ) {
		target.val( min );
	} else if ( max && value > max ) {
		target.val( max );
	}
};
