<?php
/**
 * OrderDetailsTrackingInfo class.
 *
 * @package DhlPolska\Shipment
 */

namespace DhlPolska\Shipment;

use DhlPolskaVendor\WPDesk\PluginBuilder\Plugin\Hookable;
use DhlPolskaVendor\WPDesk\View\Renderer\Renderer;
use WC_Order;

/**
 * Render shipment tracking information in customer order details.
 */
class OrderDetailsTrackingInfo implements Hookable {

	/**
	 * @var Renderer
	 */
	private Renderer $renderer;

	/**
	 * @var ShipmentTrackingDataProvider
	 */
	private ShipmentTrackingDataProvider $tracking_data_provider;

	public function __construct( Renderer $renderer, ?ShipmentTrackingDataProvider $tracking_data_provider = null ) {
		$this->renderer               = $renderer;
		$this->tracking_data_provider = $tracking_data_provider ?? new ShipmentTrackingDataProvider();
	}

	/**
	 * @return void
	 */
	public function hooks() {
		add_action( 'woocommerce_order_details_after_order_table', [ $this, 'render_tracking_info' ] );
	}

	/**
	 * @param WC_Order $order .
	 *
	 * @return void
	 */
	public function render_tracking_info( $order ): void {
		if ( ! $order instanceof WC_Order ) { /* @phpstan-ignore-line */
			return;
		}

		$tracking_data = $this->tracking_data_provider->get_tracking_data( $order );
		if ( [] === $tracking_data ) {
			return;
		}

		$this->renderer->output_render(
			'shipment/order-details-tracking',
			[
				'order'         => $order,
				'tracking_data' => $tracking_data,
			]
		);
	}
}
