<?php defined( 'ABSPATH' ) OR die( 'This script cannot be accessed directly.' );

/**
 * The Events Calendar Support
 *
 * @link https://docs.theeventscalendar.com/
 */

if ( ! class_exists( 'Tribe__Events__Query' ) ) {
	return;
}

if ( ! function_exists( 'us_enqueue_the_events_calendar_styles' ) ) {
	/**
	 * Enqueue css file
	 */
	function us_enqueue_the_events_calendar_styles() {
		if (
			defined( 'US_DEV' )
			OR ! us_get_option( 'optimize_assets' )
			OR usb_is_post_preview()
		) {
			global $us_template_directory_uri;
			$src = '/common/css/plugins/tribe-events' . ( ! defined( 'US_DEV' ) ? '.min' : '' ) . '.css';
			wp_register_style( 'us-tribe-events', $us_template_directory_uri . $src, array(), US_THEMEVERSION, 'all' );
			wp_enqueue_style( 'us-tribe-events' );
		}
	}
	add_action( 'wp_enqueue_scripts', 'us_enqueue_the_events_calendar_styles', 14 );
}

if ( ! function_exists( 'the_events_calendar_us_grid_listing_query_args' ) ) {
	add_filter( 'us_grid_listing_query_args', 'the_events_calendar_us_grid_listing_query_args', 501, 2 );
	/**
	 * Argument handler when requesting TEC events
	 *
	 * @link https://docs.theeventscalendar.com/reference/functions/tribe_get_events/
	 *
	 * @param array $query_args Array of query variables and their corresponding values
	 * @param array $defined_vars Array of all defined variables in request context
	 * @return array Returns a modified list of query arguments
	 */
	function the_events_calendar_us_grid_listing_query_args( $query_args, $defined_vars ) {
		// Force update of parameters generated by The Events Calendar tools
		if (
			! empty( $query_args['post_type'] )
			AND in_array( 'tribe_events', (array) $query_args['post_type'] )
			AND isset( $defined_vars['events_calendar_show_past'] )
			AND function_exists( 'tribe_get_events' )
		) {
			// Set how events are reflected
			$query_args['eventDisplay'] = (
				! empty( $defined_vars['events_calendar_show_past'] )
					? 'custom'
					: 'list'
			);

			// In case custom field is selected to order by, reflect it in the query args
			if ( isset( $query_args['meta_query']['_orderby_custom_'] ) ) {
				$query_args['meta_key'] = $query_args['orderby'];
				$query_args['orderby'] = array(
					'meta_value' => us_arr_path( $query_args, 'order', /* default */'ASC' ),
				);
			}

			// Replacing aliases with column names
			elseif ( isset( $query_args['orderby'] ) AND is_array( $query_args['orderby'] ) ) {
				$column_aliases = array(
					'date' => 'event_date',
					'title' => 'post_title',
				);
				foreach( $query_args['orderby'] as $param => $value ) {
					if ( isset( $column_aliases[ $param ] ) ) {
						$query_args['orderby'][ $column_aliases[ $param ] ] = $value;
						unset( $query_args['orderby'][ $param ] );
					}
				}
			}

			// Save sorting options as `tribe_get_events()` removes them
			$orderby = us_arr_path( $query_args, 'orderby', '' );

			// Get WP_Query object
			$wp_query = tribe_get_events( $query_args, /* return WP_Query */TRUE );
			if ( $wp_query instanceof WP_Query ) {
				$query_args = (array) $wp_query->query;
				if ( $orderby ) {
					$query_args['orderby'] = $orderby;
				}
			}
		}

		return $query_args;
	}
}

if ( ! function_exists( 'the_events_calendar_us_grid_filter_main_query_args' ) ) {
	add_filter( 'us_grid_filter_main_query_args', 'the_events_calendar_us_grid_filter_main_query_args', 501, 2 );
	/**
	 * Filter main query arguments for each filter element
	 *
	 * @link https://docs.theeventscalendar.com/reference/functions/tribe_get_events/
	 *
	 * @param array $query_args Array of query variables and their corresponding values
	 * @param array $grids_found An array of grids found on the page
	 * @return array Returns a modified list of query arguments
	 */
	function the_events_calendar_us_grid_filter_main_query_args( $query_args, $grids_found ) {
		if ( is_archive() ) {
			return $query_args; // for archive pages, return the result unchanged
		}
		// Include events whose date has expired
		foreach ( $grids_found as $grid_found ) {
			// Note: The presence of the variable `$events_calendar_show_past` is important for `get_defined_vars()`
			if ( $events_calendar_show_past = us_arr_path( $grid_found, 'events_calendar_show_past' ) ) {
				return the_events_calendar_us_grid_listing_query_args( $query_args, get_defined_vars() );
			}
		}
		return $query_args;
	}
}
