<?php
/**
 * Core preparer for products response data for select2 fields.
 *
 * @package Ilabs\Firevert\Core\Api\Preparer
 */

declare( strict_types=1 );

namespace Ilabs\Firevert\Core\Api\Preparer;

/**
 * Class ProductsResponsePreparer
 */
final class ProductsResponsePreparer {

	/**
	 * Prepares the final frontend payload based on the products query result.
	 *
	 * @param array $products_data Products query result.
	 *
	 * @return array<string, mixed> Final response data structure for frontend.
	 */
	public static function prepare_response( array $products_data ): array {
		$results = array();

		foreach ( $products_data['products'] as $product ) {
			$results[] = array(
				'id'   => $product['id'],
				'name' => $product['name'],
			);
		}

		return array(
			'results'    => $results,
			'pagination' => array(
				'more'         => $products_data['has_more'],
				'total_pages'  => $products_data['total_pages'],
				'current_page' => $products_data['current_page'],
			),
		);
	}
}
