<?php

namespace orderflow\Facebook;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}


class EventsManager {
    private static $_instance;
    public $facebookServerEvents = array();
	public $doingAMP = false;
    private $standardParams = array();
    private $staticEvents = array();
    private $dynamicEvents = array();
    private $triggerEvents = array();
    private $triggerEventTypes = array();
    private $uniqueId = array();

    public function __construct() {

		add_action( 'wp_enqueue_scripts', array( $this, 'enqueueScripts' ),10 );
        add_action( 'wp_enqueue_scripts', array( $this, 'setupEventsParams' ),14 );
        add_action( 'wp_enqueue_scripts', array( $this, 'outputData' ),15 );
		add_action( 'wp_footer', array( $this, 'outputNoScriptData' ), 10 );

        // Classic hook for checkout page
        add_filter( 'script_loader_tag', array( $this, 'add_data_attribute_to_script' ), 10, 3 );
	}

    public static function instance() {

        if ( is_null( self::$_instance ) ) {
            self::$_instance = new self();
        }

        return self::$_instance;


    }
    function add_data_attribute_to_script( $tag, $handle, $src ) {
        $array_scripts = array('of-js-cookie-pys', 'of-jquery-bind-first', 'of-js-tld', 'of-pys');
        if ( 'of-js-cookie-pys' === $handle && isCookiebotPluginActivated()) {
            $tag = str_replace( 'src=', 'data-cookieconsent="true" src=', $tag );
        }
        return $tag;
    }
	public function enqueueScripts() {

        wp_register_script( 'of-jquery-bind-first', ADS_PLUGIN_URL . '/dist/scripts/jquery.bind-first-0.2.3.min.js', array( 'jquery' ) );
        wp_enqueue_script( 'of-jquery-bind-first' );

        wp_register_script( 'of-js-cookie-pys', ADS_PLUGIN_URL . '/dist/scripts/js.cookie-2.1.3.min.js', array(), '2.1.3' );
        wp_register_script( 'of-js-tld', ADS_PLUGIN_URL . '/dist/scripts/tld.min.js', array( 'jquery' ), '2.3.1' );

        wp_enqueue_script( 'of-js-cookie-pys' );
        wp_enqueue_script( 'of-js-tld' );

        /*
        if ( \orderflow\Facebook\PYS()->getOption( 'compress_front_js' )){
            wp_enqueue_script( 'of-pys', ADS_PLUGIN_URL . '/dist/scripts/public.min.js',
                array( 'jquery','of-js-cookie-pys', 'of-jquery-bind-first','of-js-tld' ), ADS_VERSION );
        }
        else
        {

        }*/

        wp_enqueue_script( 'of-pys', ADS_PLUGIN_URL . '/dist/scripts/public.js',
            array( 'jquery','of-js-cookie-pys', 'of-jquery-bind-first','of-js-tld' ), time() );


	}

	public function outputData() {
		$data = array(
            'staticEvents'          => $this->staticEvents,
            'dynamicEvents'         => $this->dynamicEvents,
            'triggerEvents'         => $this->triggerEvents,
            'triggerEventTypes'     => $this->triggerEventTypes,
        );
		// collect options for configured pixel
		foreach ( \orderflow\Facebook\PYS()->getRegisteredPixels() as $pixel ) {
			/** @var Pixel|Settings $pixel */

		    if ( $pixel->configured() ) {
			    $data[ $pixel->getSlug() ] = $pixel->getPixelOptions();
		    }

		}

		$pys_analytics_storage_mode = has_filter( 'of_pys_analytics_storage_mode' );
		$pys_ad_storage_mode = has_filter( 'of_pys_ad_storage_mode' );
		$pys_ad_user_data_mode = has_filter( 'of_pys_ad_user_data_mode' );
		$pys_ad_personalization_mode = has_filter( 'of_pys_ad_personalization_mode' );
		$google_consent_mode = ( has_filter( 'cm_google_consent_mode' ) || $pys_analytics_storage_mode || $pys_ad_storage_mode || $pys_ad_user_data_mode || $pys_ad_personalization_mode ) ? true : \orderflow\Facebook\PYS()->getOption( 'google_consent_mode' );

		$options = array(
			'debug'                            => \orderflow\Facebook\PYS()->getOption( 'debug_enabled' ),
			'siteUrl'                          => site_url(),
			'ajaxUrl'                          => admin_url( 'admin-ajax.php' ),
			'ajax_event'                       => wp_create_nonce( 'ajax-event-nonce' ),
			'enable_remove_download_url_param' => \orderflow\Facebook\PYS()->getOption( 'enable_remove_download_url_param' ),
			'cookie_duration'                  => \orderflow\Facebook\PYS()->getOption( 'cookie_duration' ),
			'last_visit_duration'              => \orderflow\Facebook\PYS()->getOption( 'last_visit_duration' ),
			'enable_success_send_form'         => \orderflow\Facebook\PYS()->getOption( 'enable_success_send_form' ),
			//'ajaxForServerEvent'               => \orderflow\Facebook\PYS()->getOption( 'server_event_use_ajax' ),
            //"ajaxForServerStaticEvent"         => \orderflow\Facebook\PYS()->getOption( 'server_static_event_use_ajax' ),
            'ajaxForServerEvent'               => \orderflow\Facebook\PYS()->getOption( 'server_event_use_ajax' ) ?? true,
            "ajaxForServerStaticEvent"         => \orderflow\Facebook\PYS()->getOption( 'server_static_event_use_ajax' ) ?? true,
            "useSendBeacon"                     => \orderflow\Facebook\PYS()->getOption( 'use_send_beacon' ),
			"send_external_id"                 => \orderflow\Facebook\PYS()->getOption( 'send_external_id' ),
			"external_id_expire"               => \orderflow\Facebook\PYS()->getOption( 'external_id_expire' ),
            "track_cookie_for_subdomains"      => \orderflow\Facebook\PYS()->getOption( 'track_cookie_for_subdomains' ),
			"google_consent_mode"              => $google_consent_mode
		);

		$options[ 'gdpr' ] = array(
			'ajax_enabled'               => \orderflow\Facebook\PYS()->getOption( 'gdpr_ajax_enabled' ),
			'all_disabled_by_api'        => apply_filters( 'of_pys_disable_by_gdpr', false ),
			'facebook_disabled_by_api'   => apply_filters( 'of_pys_disable_facebook_by_gdpr', false ),
			'analytics_disabled_by_api'  => apply_filters( 'of_pys_disable_analytics_by_gdpr', false ),
			'google_ads_disabled_by_api' => apply_filters( 'of_pys_disable_google_ads_by_gdpr', false ),
			'pinterest_disabled_by_api'  => apply_filters( 'of_pys_disable_pinterest_by_gdpr', false ),
			'bing_disabled_by_api'       => apply_filters( 'of_pys_disable_bing_by_gdpr', false ),

			'externalID_disabled_by_api' => apply_filters( 'of_pys_disable_externalID_by_gdpr', false ),

			'facebook_prior_consent_enabled'   => \orderflow\Facebook\PYS()->getOption( 'gdpr_facebook_prior_consent_enabled' ),
			'analytics_prior_consent_enabled'  => \orderflow\Facebook\PYS()->getOption( 'gdpr_analytics_prior_consent_enabled' ),
			'google_ads_prior_consent_enabled' => \orderflow\Facebook\PYS()->getOption( 'gdpr_google_ads_prior_consent_enabled' ),
			'pinterest_prior_consent_enabled'  => \orderflow\Facebook\PYS()->getOption( 'gdpr_pinterest_prior_consent_enabled' ),
			'bing_prior_consent_enabled'       => \orderflow\Facebook\PYS()->getOption( 'gdpr_bing_prior_consent_enabled' ),


			'cookiebot_integration_enabled'          => isCookiebotPluginActivated() && \orderflow\Facebook\PYS()->getOption( 'gdpr_cookiebot_integration_enabled' ),
			'cookiebot_facebook_consent_category'    => \orderflow\Facebook\PYS()->getOption( 'gdpr_cookiebot_facebook_consent_category' ),
			'cookiebot_analytics_consent_category'   => \orderflow\Facebook\PYS()->getOption( 'gdpr_cookiebot_analytics_consent_category' ),
			'cookiebot_tiktok_consent_category'      => \orderflow\Facebook\PYS()->getOption( 'gdpr_cookiebot_tiktok_consent_category' ),
			'cookiebot_google_ads_consent_category'  => \orderflow\Facebook\PYS()->getOption( 'gdpr_cookiebot_google_ads_consent_category' ),
			'cookiebot_pinterest_consent_category'   => \orderflow\Facebook\PYS()->getOption( 'gdpr_cookiebot_pinterest_consent_category' ),
			'cookiebot_bing_consent_category'        => \orderflow\Facebook\PYS()->getOption( 'gdpr_cookiebot_bing_consent_category' ),
			'consent_magic_integration_enabled'      => isConsentMagicPluginActivated() && \orderflow\Facebook\PYS()->getOption( 'consent_magic_integration_enabled' ),
			'real_cookie_banner_integration_enabled' => isRealCookieBannerPluginActivated() && \orderflow\Facebook\PYS()->getOption( 'gdpr_real_cookie_banner_integration_enabled' ),
			'cookie_notice_integration_enabled'      => isCookieNoticePluginActivated() && \orderflow\Facebook\PYS()->getOption( 'gdpr_cookie_notice_integration_enabled' ),
			'cookie_law_info_integration_enabled'    => isCookieLawInfoPluginActivated() && \orderflow\Facebook\PYS()->getOption( 'gdpr_cookie_law_info_integration_enabled' ),
		);

		$options[ 'gdpr' ] = array_merge( $options[ 'gdpr' ], apply_filters( 'cm_google_consent_mode', array(
			'analytics_storage'  => array(
				'enabled' => $google_consent_mode,
				'value'   => 'granted',
			),
			'ad_storage'         => array(
				'enabled' => $google_consent_mode,
				'value'   => 'granted',
			),
			'ad_user_data'       => array(
				'enabled' => $google_consent_mode,
				'value'   => 'granted',
			),
			'ad_personalization' => array(
				'enabled' => $google_consent_mode,
				'value'   => 'granted',
			),
		) ) );

		$options[ 'gdpr' ][ 'analytics_storage' ][ 'filter' ] = $pys_analytics_storage_mode;
		$options[ 'gdpr' ][ 'ad_storage' ][ 'filter' ] = $pys_ad_storage_mode;
		$options[ 'gdpr' ][ 'ad_user_data' ][ 'filter' ] = $pys_ad_user_data_mode;
		$options[ 'gdpr' ][ 'ad_personalization' ][ 'filter' ] = $pys_ad_personalization_mode;
		$options[ 'gdpr' ][ 'analytics_storage' ][ 'value' ] = $pys_analytics_storage_mode ? ( apply_filters( 'of_pys_analytics_storage_mode', true ) ? 'granted' : 'denied' ) : $options[ 'gdpr' ][ 'analytics_storage' ][ 'value' ];
		$options[ 'gdpr' ][ 'ad_storage' ][ 'value' ] = $pys_ad_storage_mode ? ( apply_filters( 'of_pys_ad_storage_mode', true ) ? 'granted' : 'denied' ) : $options[ 'gdpr' ][ 'ad_storage' ][ 'value' ];
		$options[ 'gdpr' ][ 'ad_user_data' ][ 'value' ] = $pys_ad_user_data_mode ? ( apply_filters( 'of_pys_ad_user_data_mode', true ) ? 'granted' : 'denied' ) : $options[ 'gdpr' ][ 'ad_user_data' ][ 'value' ];
		$options[ 'gdpr' ][ 'ad_personalization' ][ 'value' ] = $pys_ad_personalization_mode ? ( apply_filters( 'of_pys_ad_personalization_mode', true ) ? 'granted' : 'denied' ) : $options[ 'gdpr' ][ 'ad_personalization' ][ 'value' ];

		$options[ 'cookie' ] = array(
			'disabled_all_cookie'                => apply_filters( 'of_pys_disable_all_cookie', false ),
			'disabled_start_session_cookie'      => apply_filters( 'of_pys_disabled_start_session_cookie', false ),
			'disabled_advanced_form_data_cookie' => apply_filters( 'of_pys_disable_advanced_form_data_cookie', false ) || apply_filters( 'of_pys_disable_advance_data_cookie', false ),
			'disabled_landing_page_cookie'       => apply_filters( 'of_pys_disable_landing_page_cookie', false ),
			'disabled_first_visit_cookie'        => apply_filters( 'of_pys_disable_first_visit_cookie', false ),
			'disabled_trafficsource_cookie'      => apply_filters( 'of_pys_disable_trafficsource_cookie', false ),
			'disabled_utmTerms_cookie'           => apply_filters( 'of_pys_disable_utmTerms_cookie', false ),
			'disabled_utmId_cookie'              => apply_filters( 'of_pys_disable_utmId_cookie', false ),
		);

		$options[ 'tracking_analytics' ] = array(
			"TrafficSource"  => getTrafficSource(),
			"TrafficLanding" => $_COOKIE[ 'of_pys_landing_page' ] ?? $_SESSION[ 'LandingPage' ] ?? 'undefined',
			"TrafficUtms"    => getUtms(),
			"TrafficUtmsId"  => getUtmsId(),
		);

        // GATags removed - only Facebook pixel needed
        /**
         * @var EventsFactory[] $eventsFactory
         */
        $eventsFactory = apply_filters("of_pys_event_factory",[]);
        foreach ($eventsFactory as $factory) {
            $opt =  $factory->getOptions();
            if(!empty($opt)) {
                $slug = method_exists($factory, 'getSlug') ? $factory::getSlug() : 'unknown';
                $options[$slug] = $factory->getOptions();
            }
        }
        $options['cache_bypass'] = time();

		$data = array_merge( $data, $options );

		wp_localize_script( 'of-pys', 'ofPysOptions', $data );

	}
	
	public function outputNoScriptData() {
        if(!apply_filters( 'of_pys_disable_by_gdpr', false)) {
            foreach (\orderflow\Facebook\PYS()->getRegisteredPixels() as $pixel) {
                /** @var Pixel|Settings $pixel */
                if (!apply_filters('of_pys_disable_' . $pixel->getSlug() . '_by_gdpr', false)) {
                    $pixel->outputNoScriptEvents();
                }
            }
        }
    }






    public function setupEventsParams() {
        $this->standardParams = getStandardParams();
        $this->facebookServerEvents = array();


        /**
         * @var EventsFactory[] $eventsFactory
         **/
        $eventsFactory = apply_filters("of_pys_event_factory",[]);

        foreach ($eventsFactory as $factory) {
            if(!$factory->isEnabled()) {
                continue;
            }
            $events = $factory->generateEvents();
            $this->addEvents($events,$factory->getSlug());
        }

		// initial event
		$initEvent = new SingleEvent('init_event',EventTypes::$STATIC,'');
		
		if(get_post_type() == "post" && !is_archive()) {
			global $post;
			$catIds = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'names' ) );
			$initEvent->addParams([
				'post_category' => implode(", ",$catIds)
			]);
		}

		$registeredPixels = \orderflow\Facebook\PYS()->getRegisteredPixels();
		
		foreach ( $registeredPixels as $pixel ) {
			$events = $pixel->generateEvents( $initEvent );
			
			foreach ($events as $event) {
				$event->addParams($this->standardParams);
				$this->addStaticEvent( $event,$pixel,"" );
			}
		}

        if(\orderflow\Facebook\EventsWoo::instance()->isEnabled()){
            // AddToCart on button and Affiliate
            if ( \orderflow\Facebook\Facebook::instance()->getOption('woo_add_to_cart_catch_method') == "add_cart_js"
                    && \orderflow\Facebook\Facebook::instance()->getOption( 'of_woo_add_to_cart_enabled')
                    && \orderflow\Facebook\Facebook::instance()->getOption( 'of_woo_add_to_cart_on_button_click' )
            ) {
                add_action( 'woocommerce_after_shop_loop_item', array( $this, 'setupWooLoopProductData' ) );
                add_action( 'woocommerce_after_add_to_cart_button', '\orderflow\Facebook\EventsManager::setupWooSingleProductData' );
                add_filter( 'woocommerce_blocks_product_grid_item_html', array( $this, 'setupWooBlocksProductData' ), 10, 3 );
                add_filter('jet-woo-builder/elementor-views/frontend/archive-item-content', array( $this, 'setupWooBlocksProductData' ),10, 3);
            }
        }

        if(!\orderflow\Facebook\PYS()->is_user_agent_bot()){

            // hide by rahim disbale server side
            /*
            if( count($this->facebookServerEvents ) > 0
                && Facebook()->enabled()
                && Facebook()->isServerApiEnabled()
                && !\orderflow\Facebook\PYS()->isCachePreload()
                && \orderflow\Facebook\Consent::instance()->checkConsent( 'facebook' )
            ) {
                FacebookServer()->sendEventsAsync( $this->facebookServerEvents );
                $this->facebookServerEvents = array();
            }
*/
            // Pinterest and Bing removed - only Facebook pixel needed
        }


        // remove new user mark
        if($user_id = get_current_user_id()) {
            if ( get_user_meta( $user_id, 'of_pys_complete_registration', true ) ) {
                delete_user_meta( $user_id, 'of_pys_complete_registration' );
            }
        }
	}

	public function getStaticEvents( $context ) {
	    return isset( $this->staticEvents[ $context ] ) ? $this->staticEvents[ $context ] : array();
    }


    function addEvents($pixelEvents,$slug) {
        foreach ($pixelEvents as $pixelSlug => $events) {
            // Check if pixel is registered, skip if not found
            $registeredPixels = \orderflow\Facebook\PYS()->getRegisteredPixels();
            if ( ! isset( $registeredPixels[ $pixelSlug ] ) ) {
                continue;
            }
            $pixel = $registeredPixels[ $pixelSlug ];
            foreach ($events as $event) {
                // add standard params
                if(!isset($this->uniqueId[$event->getId()])) {
                    $this->uniqueId[$event->getId()] = EventIdGenerator::guidv4();
                }
                $event->addPayload(['eventID'=>$this->uniqueId[$event->getId()]]);

                $event->addParams($this->standardParams);
                //save different types of events
                if($event->getType() == EventTypes::$STATIC) {
                    $this->addStaticEvent( $event,$pixel,$slug );
                } elseif($event->getType() == EventTypes::$TRIGGER) {
                    $this->addTriggerEvent($event,$pixel,$slug);
                } else {
                    $this->addDynamicEvent($event,$pixel,$slug);
                }
            }

        }
    }



    function addDynamicEvent($event,$pixel,$slug) {
        $eventData = $event->getData();
        $eventData = $this::filterEventParams($eventData,$slug,['event_id'=>$event->getId(),'pixel'=>$pixel->getSlug()]);

        if($event->getId() == 'woo_remove_from_cart')  {
            $this->dynamicEvents[ $event->getId() ][ $event->args['key'] ][ $pixel->getSlug() ] = $eventData;
        } else  {
            $this->dynamicEvents[ $event->getId() ][ $pixel->getSlug() ] = $eventData;
        }
    }

    function addTriggerEvent($event,$pixel,$slug) {

        $eventData = $event->getData();
        $eventData = $this->filterEventParams($eventData,$slug,['event_id'=>$event->getId(),'pixel'=>$pixel->getSlug()]);
        //save static event data
        if($event->getId() == "custom_event") {
            $eventId = $event->args->getPostId();
        } else {
            $eventId = $event->getId();
        }
        $this->triggerEvents[ $eventId ][ $pixel->getSlug() ] = $eventData;
        if ( !empty( $event->args ) &&  $event->getCategory() === 'custom') {
            $this->triggerEventTypes = array_replace_recursive( $this->triggerEventTypes, $event->args->__get( 'triggerEventTypes' ) );
        }

    }

    /**
     * Create stack event, they fire when page loaded
     * @param Event $event
     */
    function addStaticEvent($event,$pixel,$slug) {
        $event_getId = $event->getId() == 'custom_event' ? $event->getPayloadValue('custom_event_post_id') : $event->getId();

        if(!isset($this->uniqueId[$event_getId])) {
            $this->uniqueId[$event_getId] = EventIdGenerator::guidv4();
        }

        $event->addPayload(['eventID'=>$this->uniqueId[$event_getId]]);

        $eventData = $event->getData();
        $eventData = $this::filterEventParams($eventData,$slug,['event_id'=>$event->getId(),'pixel'=>$pixel->getSlug()]);
        // send only for FB Server events
        if(Facebook()->enabled() && $pixel->getSlug() == "orderflow_facebook" &&
            ($event->getId() == "woo_complete_registration") &&
            Facebook()->isServerApiEnabled() &&
            Facebook()->getOption("of_woo_complete_registration_send_from_server") &&
            !$this->isGdprPluginEnabled() )
        {
            if($eventData['delay'] == 0 && !\orderflow\Facebook\PYS()->getOption( "server_static_event_use_ajax" )) {
                $this->facebookServerEvents[] = $event;
            }
            return;
        }

        //save static event data
        $this->staticEvents[ $pixel->getSlug() ][ $event->getId() ][] = $eventData;
        // fire fb server api event

        // hide by rahim disbale server side request
        /*
        if( $eventData['delay'] == 0 && (!\orderflow\Facebook\PYS()->getOption( "server_static_event_use_ajax" ) || !\orderflow\Facebook\PYS()->getOption('server_event_use_ajax'))) {
            if($pixel->getSlug() === "facebook" && Facebook()->enabled() && Facebook()->isServerApiEnabled()) {
                $this->facebookServerEvents[] = $event;
            }
            // Pinterest removed - only Facebook pixel needed
        } */

    }

    static function filterEventParams($data,$slug,$context = null)
    {

        if(!\orderflow\Facebook\PYS()->getOption('enable_content_name_param')) {
            unset($data['params']['content_name']);
        }

        if(!\orderflow\Facebook\PYS()->getOption('enable_page_title_param')) {
            unset($data['params']['page_title']);
        }
        if(!\orderflow\Facebook\PYS()->getOption('enable_post_category_param')) {
            unset($data['params']['post_category']);
        }

        // EventsWoo and EventsEdd filter params removed - only Facebook pixel needed

        if(isset($context) && isset($context['pixel']) && $context['pixel'] === 'orderflow_facebook' && Facebook()->getOption('of_enabled_medical')) {
            foreach (Facebook()->getOption('do_not_track_medical_param') as $param) {
                unset($data['params'][$param]);
            }
        }

        return $data;
    }



    function isGdprPluginEnabled() {
        return apply_filters( 'of_pys_disable_by_gdpr', false ) ||
            apply_filters( 'of_pys_disable_facebook_by_gdpr', false ) ||
            (isCookiebotPluginActivated() && \orderflow\Facebook\PYS()->getOption('gdpr_cookiebot_integration_enabled')) ||
            (isConsentMagicPluginActivated() && \orderflow\Facebook\PYS()->getOption('consent_magic_integration_enabled')) ||
            (isRealCookieBannerPluginActivated() && \orderflow\Facebook\PYS()->getOption('gdpr_real_cookie_banner_integration_enabled')) ||
            (isCookieNoticePluginActivated() && \orderflow\Facebook\PYS()->getOption('gdpr_cookie_notice_integration_enabled')) ||
            (isCookieLawInfoPluginActivated() && \orderflow\Facebook\PYS()->getOption('gdpr_cookie_law_info_integration_enabled'));
    }


    // WooCommerce methods removed - only Facebook pixel needed
    // WooCommerce events will be handled by Facebook class directly if needed

    // EDD methods removed - only Facebook pixel needed
    static function isTrackExternalId(){
        return \orderflow\Facebook\PYS()->getOption("send_external_id") && !apply_filters( 'of_pys_disable_externalID_by_gdpr', false ) && !apply_filters( 'of_pys_disable_all_cookie', false );
    }

    public function setupWooLoopProductData()
    {
        global $product;

        $this->setupWooProductData($product);
    }

    public function setupWooBlocksProductData($html, $data, $product)
    {
        $this->setupWooProductData($product);
        return $html;
    }

    public function setupWooProductData($product) {

        if (  !is_a($product,"WC_Product")
            || wooProductIsType( $product, 'variable' )
            || wooProductIsType( $product, 'grouped' )
        ) {
            return; // skip variable products
        }

        $product_id = $product->get_id();

        $params = array();
        $event = new SingleEvent('woo_add_to_cart_on_button_click',EventTypes::$STATIC,'woo');
        $event->args = ['productId' => $product_id,'quantity' => 1];

        foreach ( PYS()->getRegisteredPixels() as $pixel ) {
            /** @var Pixel|Settings $pixel */

            $events = $pixel->generateEvents( $event );
            foreach ($events as $event) {
                // prepare event data
                $eventData = $event->getData();
                $eventData = EventsManager::filterEventParams($eventData,"woo",['event_id'=>$event->getId(),'pixel'=>$pixel->getSlug()]);

                $params[$pixel->getSlug()] = $eventData; // replace data!!(now use only one event)
            }
        }

        if ( empty( $params ) ) {
            return;
        }

        $params = json_encode( $params );

        ?>

        <script type="application/javascript" style="display:none">
            /* <![CDATA[ */
            window.ofPysWooProductData = window.ofPysWooProductData || [];
            window.ofPysWooProductData[ <?php echo $product_id; ?> ] = <?php echo $params; ?>;
            /* ]]> */
        </script>

        <?php

    }

    public static function setupWooSingleProductData() {
        global $product;

        if ( ! is_object( $product)) $product = wc_get_product( get_the_ID() );

        if(!$product || !is_a($product,"WC_Product") ) return;

        // Skip external products (affiliate products)
        if ( wooProductIsType( $product, 'external' ) ) {
            return;
        }

        $eventType = 'woo_add_to_cart_on_button_click';
        $product_id = $product->get_id();

        // main product id
        $product_ids[] = $product_id;

        // variations ids
        if ( wooProductIsType( $product, 'variable' ) ) {
            $product_ids = array_merge($product_ids, $product->get_children());
        }

        $params = array();

        foreach ( $product_ids as $product_id ) {

            foreach ( PYS()->getRegisteredPixels() as $pixel ) {
                /** @var Pixel|Settings $pixel */
                $initEvent = new SingleEvent($eventType,EventTypes::$STATIC,"woo");
                $initEvent->args = ['productId' => $product_id,'quantity' => 1];
                $events = [];
                if(method_exists($pixel,'generateEvents')) {
                    add_filter('of_pys_conditional_post_id', function($id) use ($product_id) { return $product_id; });
                    $events =  $pixel->generateEvents( $initEvent );
                    remove_all_filters('of_pys_conditional_post_id',10);
                } else {
                    if( $pixel->addParamsToEvent( $initEvent )) {
                        $events[] = $initEvent;
                    }
                }

                if(count($events) == 0) continue;
                foreach ($events as $event) {
                    // prepare event data
                    $eventData = $event->getData();
                    $eventData = EventsManager::filterEventParams($eventData,"woo",['event_id'=>$event->getId(),'pixel'=>$pixel->getSlug()]);

                    $params[ $product_id ][ $pixel->getSlug() ] = $eventData; // replace (use only one event for product)
                }

            }

        }

        if ( empty( $params ) ) {
            return;
        }

        ?>

        <script type="application/javascript" style="display:none">
            /* <![CDATA[ */
            window.ofPysWooProductData = window.ofPysWooProductData || [];
            <?php foreach ( $params as $product_id => $product_data ) : ?>
            window.ofPysWooProductData[<?php echo $product_id; ?>] = <?php echo json_encode( $product_data ); ?>;
            <?php endforeach; ?>
            /* ]]> */
        </script>

        <?php

    }
}