<?php

add_action( 'wp_enqueue_scripts', 'wpcode_pixel_enqueue_scripts' );

/**
 * Enqueue scripts for the frontend.
 *
 * @return void
 */
function wpcode_pixel_enqueue_scripts() {

	$custom_events = wpcode()->settings->get_option( 'custom_events', array() );

	// If we have no custom events there is nothing to load.
	if ( empty( $custom_events ) ) {
		return;
	}

	// Enqueue Custom Events frontend script if we have any custom events.
	if ( ! empty( $custom_events ) ) {
		$custom_asset_file = WPCODE_PIXEL_PLUGIN_PATH . 'build/custom-event-tracking.asset.php';

		if ( ! file_exists( $custom_asset_file ) ) {
			return;
		}

		$asset = include_once $custom_asset_file;

		$custom_events_data = array(
			'events'   => $custom_events,
			'rest_url' => esc_url_raw( rest_url( 'wpcode-pixel/v1/track-event' ) ),
		);

		// Attach some simple contextual data that might be useful when evaluating triggers.
		global $wp;
		$current_url = home_url( $wp->request );
		if ( ! empty( $_SERVER['QUERY_STRING'] ) ) {
			$current_url .= '?' . $_SERVER['QUERY_STRING'];
		}

		$custom_events_data['context'] = array(
			'is_home'           => is_front_page(),
			'current_post_id'   => is_singular() ? get_queried_object_id() : 0,
			'current_post_type' => is_singular() ? get_post_type( get_queried_object_id() ) : '',
			'current_url'       => esc_url_raw( $current_url ),
		);

		// Let's see if any of the custom events has google assigned in the platforms array.
		$google_custom_events = array_filter(
			$custom_events,
			function ( $event ) {
				return ! empty( $event['platforms'] ) && in_array( 'google', $event['platforms'], true );
			}
		);
		if ( $google_custom_events ) {
			$google_auto_insert = array_filter(
				wpcode_pixel()->auto_insert->types,
				function ( $auto_insert ) {
					return 'google' === $auto_insert->type;
				}
			);
			if ( ! empty( $google_auto_insert ) ) {
				$google_auto_insert                         = array_values( $google_auto_insert );
				$custom_events_data['google_ads_ids']       = $google_auto_insert[0]->get_ads_ids();
				$custom_events_data['google_analytics_ids'] = $google_auto_insert[0]->get_analytics_ids();
				$custom_events_data['google_ads_id']        = $google_auto_insert[0]->get_ads_id();
				$custom_events_data['google_analytics_id']  = $google_auto_insert[0]->get_analytics_id();
			}
		}

		// Let's see if any of the custom events has facebook assigned in the platforms array.
		$facebook_custom_events = array_filter(
			$custom_events,
			function ( $event ) {
				return ! empty( $event['platforms'] ) && in_array( 'facebook', $event['platforms'], true );
			}
		);
		if ( $facebook_custom_events ) {
			$facebook_auto_insert = array_filter(
				wpcode_pixel()->auto_insert->types,
				function ( $auto_insert ) {
					return 'facebook' === $auto_insert->type;
				}
			);
			if ( ! empty( $facebook_auto_insert ) ) {
				$facebook_auto_insert                     = array_values( $facebook_auto_insert );
				$facebook_type                            = $facebook_auto_insert[0];
				$custom_events_data['facebook_pixel_ids'] = $facebook_type->get_pixel_ids();
				if ( method_exists( $facebook_type, 'has_api_token' ) && $facebook_type->has_api_token() ) {
					$custom_events_data['facebook_has_api_token'] = true;
				}
			}
		}

		$custom_events_data = apply_filters( 'wpcode_pixel_custom_events_data', $custom_events_data );

		wp_enqueue_script(
			'wpcode-pixel-custom-events-frontend',
			plugins_url( 'build/custom-event-tracking.js', WPCODE_PIXEL_FILE ),
			$asset['dependencies'],
			$asset['version'],
			true
		);
		wp_localize_script( 'wpcode-pixel-custom-events-frontend', 'wpcodeCustomEventData', $custom_events_data );
	}
}
