<?php

namespace Cart\notify\Frontend;

use Cart\notify\WooCommerce\Integration;

defined( 'ABSPATH' ) || exit;


/**
 * Notification handler class
 */

class Notify {
	/**
	 * WooCommerce integration helper.
	 *
	 * @var Integration
	 */
	private $woocommerce;

	/**
	 * Initialize the class.
	 *
	 * @param Integration|null $woocommerce WooCommerce integration helper.
	 */
	function __construct( Integration $woocommerce = null ) {
		$this->woocommerce = $woocommerce ? $woocommerce : new Integration();

		add_action( 'wp_enqueue_scripts', array( $this, 'cns_scripts_add' ) );
	}

	/**
	 * Enqueue frontend notification assets.
	 *
	 * @return void
	 */
	public function cns_scripts_add() {
		if ( ! $this->woocommerce->is_available() ) {
			return;
		}

		$should_enqueue = apply_filters(
			'cart_notify_should_enqueue_assets',
			true,
			array(
				'is_product'   => function_exists( 'is_product' ) ? is_product() : false,
				'is_shop'      => function_exists( 'is_shop' ) ? is_shop() : false,
				'is_cart'      => function_exists( 'is_cart' ) ? is_cart() : false,
				'is_checkout'  => function_exists( 'is_checkout' ) ? is_checkout() : false,
				'is_woocommerce' => function_exists( 'is_woocommerce' ) ? is_woocommerce() : false,
			)
		);

		if ( ! $should_enqueue ) {
			return;
		}

		wp_enqueue_script( 'cartnotify-libs-script' );
		wp_enqueue_script( 'cartnotify-script' );
		wp_enqueue_style( 'cartnotify-libs-style' );
		wp_enqueue_style( 'cartnotify-style' );
	}
}
