<?php
/**
 * FoodOrd Cart Upsells
 *
 * @package FoodOrd
 * @since 1.3.36
 */

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

/**
 * Renders upsell suggestions inside the FoodOrd cart sidebar.
 */
class Foodord_Card_Upsells {

	/**
	 * Upsell helper.
	 *
	 * @var Foodord_Product_Modal_Upsells|null
	 */
	private $upsell_helper = null;

	/**
	 * Constructor.
	 */
	public function __construct() {
		if ( class_exists( 'Foodord_Product_Modal_Upsells' ) ) {
			$this->upsell_helper = new Foodord_Product_Modal_Upsells();
		}
	}

	/**
	 * Render cart upsell HTML.
	 *
	 * @return string
	 */
	public function get_markup_for_cart(): string {
		if ( 'yes' !== get_option( 'foodord_enable_modal_upsells', 'no' ) ) {
			return '';
		}

		if ( ! $this->upsell_helper || ! function_exists( 'WC' ) || ! WC()->cart ) {
			return '';
		}

		$foodord_card_upsells = $this->upsell_helper->get_products_for_cart( WC()->cart->get_cart() );
		if ( empty( $foodord_card_upsells ) ) {
			return '';
		}

		$foodord_card_upsells_template = FOODORD_DIR . 'templates/frontend/card-upsells.php';
		if ( ! file_exists( $foodord_card_upsells_template ) ) {
			return '';
		}

		$foodord_upsell_helper = $this->upsell_helper;

		ob_start();
		include $foodord_card_upsells_template;

		return trim( (string) ob_get_clean() );
	}
}
