import $ from 'jquery';

export type OrderItems = Array<{
	product_id: string,
	product_src: string,
	product_name: string,
	product_sku: string,
	product_quantity: number
}>

export const renderOrderItems = ( returnStep: JQuery, orderItems: OrderItems ) => {
	if ( !returnStep.length ) {
		throw new Error( 'Return step is empty' );
	}

	if ( !orderItems.length ) {
		throw new Error( 'Order items are empty' );
	}

	const orderItemTemplate = window.wp.template( 'wetail-portal-order-item' );

	if ( orderItemTemplate ) {
		orderItems.forEach( item => {
			const orderItem = orderItemTemplate( {
				product_id: item.product_id,
				product_src: item.product_src,
				product_name: item.product_name,
				product_sku: item.product_sku,
				product_quantity: item.product_quantity,
			} );

			returnStep.find( $( '.order-items' ) ).append( orderItem );
		} );
	}
};
