import $ from 'jquery';

export type DataItems = Array<{
	quantity: number;
	product_id: string;
}>

export const getOrderItems = ( items:JQuery<HTMLElement> ): DataItems => {
	const order_items: DataItems = [];

	items.each( function() {
		const orderItem = $( this );
		const checkbox = orderItem.find( $( 'input[type="checkbox"]' ) );

		if ( checkbox.prop( 'checked' ) ) {
			const quantityElement = orderItem.find( $( '.quantity input[type="number"]' ) );
			const quantity = quantityElement.val() as string;
			const product_id = orderItem.data( 'product_id' );

			order_items.push( {
				product_id,
				quantity: parseInt( quantity ),
			} );
		}
	} );

	return order_items;
}
