<?php
/**
 * Public controller.
 *
 * @package CartNinjaRecovery
 */

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

class CARTNINJA_Public {

	public function enqueue_assets() {
		if ( ! $this->should_enqueue_assets() ) {
			return;
		}

		wp_enqueue_style( 'cartninja-public', CARTNINJA_PLUGIN_URL . 'public/css/public.css', array(), CARTNINJA_VERSION );
		wp_enqueue_script( 'cartninja-public', CARTNINJA_PLUGIN_URL . 'public/js/public.js', array( 'jquery' ), CARTNINJA_VERSION, true );
		wp_localize_script(
			'cartninja-public',
			'cartninjaPublic',
			array(
				'ajaxUrl' => admin_url( 'admin-ajax.php' ),
				'nonce'   => wp_create_nonce( 'cartninja_public_nonce' ),
			)
		);
	}

	private function should_enqueue_assets(): bool {
		$is_checkout = function_exists( 'is_checkout' ) && is_checkout() && ( ! function_exists( 'is_order_received_page' ) || ! is_order_received_page() );

		if ( $is_checkout ) {
			return true;
		}

		if ( ! function_exists( 'cartninja_is_premium_unlocked' ) || ! cartninja_is_premium_unlocked() ) {
			return false;
		}

		$settings = get_option( 'cartninja_settings', array() );
		$popup    = isset( $settings['popup'] ) && is_array( $settings['popup'] ) ? $settings['popup'] : array();

		if ( 'yes' !== ( isset( $popup['enable_popup'] ) ? $popup['enable_popup'] : 'no' ) || 'yes' !== ( isset( $popup['enable_add_to_cart_popup'] ) ? $popup['enable_add_to_cart_popup'] : 'no' ) ) {
			return false;
		}

		return ( function_exists( 'is_product' ) && is_product() )
			|| ( function_exists( 'is_shop' ) && is_shop() )
			|| ( function_exists( 'is_product_category' ) && is_product_category() )
			|| ( function_exists( 'is_product_tag' ) && is_product_tag() )
			|| ( function_exists( 'is_cart' ) && is_cart() );
	}
}
