<?php
/**
 * Class EmailOrderDetailsPointInfo
 *
 * @package DhlPolska\Point
 */

namespace DhlPolska\Point;

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

/**
 * Render pickup point information in order emails.
 */
class EmailOrderDetailsPointInfo implements Hookable {
	use ParcelshopDataTrait;

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

	/**
	 * EmailOrderDetailsPointInfo constructor.
	 *
	 * @param Renderer $renderer .
	 */
	public function __construct( Renderer $renderer ) {
		$this->renderer = $renderer;
	}

	/**
	 * Hooks.
	 */
	public function hooks() {
		add_action( 'woocommerce_email_after_order_table', [ $this, 'render_point_info' ], 10, 4 );
	}

	/**
	 * @param WC_Order $order         .
	 * @param bool     $sent_to_admin .
	 * @param bool     $plain_text    .
	 * @param object   $email         .
	 *
	 * @return void
	 */
	public function render_point_info( $order, $sent_to_admin, $plain_text, $email ): void {
		if ( ! $order instanceof WC_Order ) { /* @phpstan-ignore-line */
			return;
		}

		$parcelshop_data = $this->get_parcelshop_data( $order );

		if ( '' === $parcelshop_data['id'] ) {
			return;
		}

		$this->renderer->output_render(
			'point/email-parcelshop',
			[
				'order'           => $order,
				'parcelshop_data' => $parcelshop_data,
				'sent_to_admin'   => (bool) $sent_to_admin,
				'plain_text'      => (bool) $plain_text,
				'email'           => $email,
			]
		);
	}
}
