<?php
/**
 * Entry filtering settings Gravity Forms style.
 *
 * @package GravityKit\GravityView\Entry
 * @since 3.0.0
 */

namespace GravityKit\GravityView\Entry;

/**
 * Entry filtering settings Gravity Forms style.
 *
 * The good old $search_criteria in object form.
 *
 * @since 2.0
 * @since 3.0.0 Migrated to GravityKit\GravityView\Entry namespace.
 */
class EntryFilterGravityForms extends \GV\Entry_Filter {

	/**
	 * @var array $search_criteria The $search_criteria array used by Gravity Forms
	 */
	private $search_criteria = [];

	/**
	 * Create a filter from this criteria.
	 *
	 * @param array $search_criteria The Gravity Forms search criteria.
	 * @see GFAPI::search_entries
	 *
	 * @return \GV\GF_Entry_Filter The filter.
	 */
	public static function from_search_criteria( $search_criteria ) {
		$filter                  = new self();
		$filter->search_criteria = $search_criteria;
		return $filter;
	}

	/**
	 * Merge two search criteria arrays.
	 *
	 * If two values collide, $b always wins.
	 *
	 * @param array $a One Gravity Forms search criteria.
	 * @param array $a Another Gravity Forms search criteria.
	 *
	 * @see GFAPI::search_entries
	 *
	 * @return array Merged search criteria.
	 */
	public static function merge_search_criteria( $a, $b ) {
		$search_criteria = [];

		foreach ( [ 'field_filters', 'start_date', 'end_date', 'status' ] as $key ) {
			switch ( $key ) :
				case 'field_filters':
					$field_filters = array_merge( empty( $a[ $key ] ) ? [] : $a[ $key ], empty( $b[ $key ] ) ? [] : $b[ $key ] );
					if ( ! empty( $field_filters ) ) {
						$search_criteria[ $key ] = $field_filters;
					}

					if ( ! empty( $b[ $key ]['mode'] ) ) {
						$search_criteria[ $key ]['mode'] = $b[ $key ]['mode'];
					} elseif ( ! empty( $a[ $key ]['mode'] ) ) {
						$search_criteria[ $key ]['mode'] = $a[ $key ]['mode'];
					}
					break;
				case 'start_date':
				case 'end_date':
				case 'status':
					if ( isset( $b[ $key ] ) ) {
						$search_criteria[ $key ] = $b[ $key ];
					} elseif ( isset( $a[ $key ] ) ) {
						$search_criteria[ $key ] = $a[ $key ];
					}
					break;
			endswitch;
		}

		return $search_criteria;
	}

	/**
	 * Get the $search_criteria.
	 *
	 * @see GFAPI::search_entries
	 *
	 * @return array $search_criteria The Gravity Forms search criteria.
	 */
	public function as_search_criteria() {
		return $this->search_criteria;
	}
}
