<?php
namespace WPVOID\StockBeacon\Frontend;

use WPVOID\StockBeacon\Core\WaitlistManager;
use WPVOID\StockBeacon\Settings;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class FormRenderer {
	private WaitlistManager $manager;

	public function __construct( WaitlistManager $manager ) {
		$this->manager = $manager;
	}

	public function render( int $product_id, int $variation_id = 0 ): void {
		$settings = Settings::get();
		$count    = $this->manager->get_cached_count( $product_id );
		$email    = is_user_logged_in() ? wp_get_current_user()->user_email : '';
		$args     = array(
			'product_id'       => $product_id,
			'variation_id'     => $variation_id,
			'settings'         => $settings,
			'count'            => $count,
			'prefilled_email'  => $email,
		);

		$theme_template = locate_template(
			array(
				'stockbeacon-product-waitlists-for-woocommerce/form-out-of-stock.php',
				'stockbeacon-product-waitlists-for-woocommerce/notify-me-form.php',
			)
		);
		if ( '' !== $theme_template ) {
			extract( $args, EXTR_SKIP );
			include $theme_template;
			return;
		}

		wc_get_template(
			'frontend/notify-me-form.php',
			$args,
			'',
			STOCKBEACON_PATH . 'templates/'
		);
	}
}
