<?php
defined( 'ABSPATH' ) || exit;

/*-------------------------------------------------------------------------------*/
/* [ Table of contents ]
/*-------------------------------------------------------------------------------*/

	# Archives
	# Entry
	# Cards

/*-------------------------------------------------------------------------------*/
/* [ Archives ]
/*-------------------------------------------------------------------------------*/

/**
 * Defines your default search results page style.
 */
function wpex_search_results_style() {
	$style = '';

	if ( ! wpex_search_entry_card_style() ) {
		$style = get_theme_mod( 'search_style', 'default' );
		if ( wp_validate_boolean( get_theme_mod( 'search_results_cpt_loops', true ) ) ) {
			$post_type = $_GET[ 'post_type' ] ?? $_GET[ 'post_types' ] ?? '';
			if ( $post_type && 'product' !== $post_type ) {
				if ( in_array( $post_type, wpex_theme_post_types(), true ) ) {
					$style = $post_type;
				} elseif ( 'post' === $post_type ) {
					$style = 'blog';
				}
			}
		}
	}

	$style = $style ?: 'default'; // Important: Can't be empty or it will look like the blog

	/**
	 * Filters the search results style.
	 *
	 * @param string $style
	 */
	$style = (string) apply_filters( 'wpex_search_results_style', $style );

	return $style;
}

/**
 * Returns the search loop top class.
 */
function wpex_search_loop_top_class() {
	$classes = [
		'wpex-clr',
	];

	if ( wpex_search_entry_card_style() ) {

		$classes[] = 'wpex-row';

		$grid_style = get_theme_mod( 'search_archive_grid_style', 'fit-rows' );

		if ( 'masonry' === $grid_style || 'no-margins' === $grid_style ) {
			$classes[] = 'wpex-masonry-grid';
			wpex_enqueue_isotope_scripts(); // This is a good spot to enqueue grid scripts
		}

		if ( $gap = get_theme_mod( 'search_archive_grid_gap' ) ) {
			$classes[] = wpex_gap_class( $gap );
		}

	}

	/**
	 * Filters the search loop top element classes.
	 *
	 * @param array $classes
	 */
	$classes = (array) apply_filters( 'wpex_search_loop_top_class', $classes );

	if ( $classes ) {
		echo 'class="' . esc_attr( implode( ' ', $classes ) ) . '"';
	}
}

/*-------------------------------------------------------------------------------*/
/* [ Entry ]
/*-------------------------------------------------------------------------------*/

/**
 * Returns search entry excerpt length.
 */
function wpex_search_entry_excerpt_length() {
	$length = get_theme_mod( 'search_entry_excerpt_length', '30' );

	/**
	 * Filters the seach entry excerpt length.
	 *
	 * @param string $length
	 */
	$length = apply_filters( 'wpex_search_entry_excerpt_length', $length );

	return $length;
}

/**
 * Returns search entry excerpt length.
 */
function wpex_search_archive_columns() {
	$columns = '1';

	if ( wpex_search_entry_card_style() ) {
		$columns = get_theme_mod( 'search_entry_columns', '2' );
	}

	/**
	 * Filters the search archive columns.
	 *
	 * @param string $columns
	 */
	$columns = apply_filters( 'wpex_search_archive_columns', $columns );

	return $columns;
}

/**
 * Search entry class.
 */
function wpex_search_entry_class() {
	$classes = [
		'search-entry',
	];

	$columns = absint( wpex_search_archive_columns() );

	if ( $columns > 1 || wpex_search_entry_card_style() ) {

		$col_class = wpex_row_column_width_class( $columns );

		if ( $col_class ) {
			$classes[] = 'col';
			$classes[] = $col_class;
		}

		$loop_counter = wpex_get_loop_counter();

		if ( $loop_counter ) {
			$classes[] = 'col-' . absint( $loop_counter );
		}

		$grid_style = get_theme_mod( 'search_archive_grid_style', 'fit-rows' );

		if ( 'masonry' === $grid_style || 'no-margins' === $grid_style ) {
			$classes[] = 'wpex-masonry-col';
		}

	}

	/**
	 * Filters the seach entry class.
	 *
	 * @param array $class
	 */
	$classes = (array) apply_filters( 'wpex_search_entry_class', $classes );

	post_class( $classes );

}

/**
 * Search entry inner class.
 */
function wpex_search_entry_inner_class() {
	$class = [
		'search-entry-inner',
		'wpex-flex',
		'wpex-last-mb-0',
	];

	/**
	 * Filters the seach entry inner class.
	 *
	 * @param array $class
	 */
	$class = (array) apply_filters( 'wpex_search_entry_inner_class', $class );

	if ( $class ) {
		echo 'class="' . esc_attr( implode( ' ', $class ) ) . '"';
	}
}

/**
 * Search entry content class.
 */
function wpex_search_entry_content_class() {
	$class = [
		'search-entry-text',
		'wpex-flex-grow',
		'wpex-last-mb-0',
	];

	/**
	 * Filters the seach entry content class.
	 *
	 * @param array $class
	 */
	$class = (array) apply_filters( 'wpex_search_entry_content_class', $class );

	if ( $class ) {
		echo 'class="' . esc_attr( implode( ' ', $class ) ) . '"';
	}
}

/**
 * Search entry header class.
 */
function wpex_search_entry_header_class() {
	$class = [
		'search-entry-header',
	];

	/**
	 * Filters the seach entry header class.
	 *
	 * @param array $class
	 */
	$class = (array) apply_filters( 'wpex_search_entry_header_class', $class );

	if ( $class ) {
		echo 'class="' . esc_attr( implode( ' ', $class ) ) . '"';
	}
}

/**
 * Search entry title class.
 */
function wpex_search_entry_title_class() {
	$class = [
		'search-entry-header-title',
		'entry-title',
		'wpex-text-lg',
		'wpex-m-0',
	];

	/**
	 * Filters the seach entry title class.
	 *
	 * @param array $class
	 */
	$class = (array) apply_filters( 'wpex_search_entry_title_class', $class );

	if ( $class ) {
		echo 'class="' . esc_attr( implode( ' ', $class ) ) . '"';
	}
}

/**
 * Search entry excerpt class.
 */
function wpex_search_entry_excerpt_class() {
	$class = [
		'search-entry-excerpt',
		'wpex-my-15',
		'wpex-last-mb-0',
		'wpex-clr',
	];

	/**
	 * Filters the seach entry excerpt class.
	 *
	 * @param array $class
	 */
	$class = (array) apply_filters( 'wpex_search_entry_excerpt_class', $class );

	if ( $class ) {
		echo 'class="' . esc_attr( implode( ' ', $class ) ) . '"';
	}
}

/**
 * Search Entry divider.
 */
function wpex_search_entry_divider() {
	$columns = wpex_get_grid_entry_columns();

	$divider = '<div class="search-entry-divider wpex-divider wpex-my-25"></div>';

	/**
	 * Filters the seach entry divider element.
	 *
	 * @param string $divider
	 */
	echo apply_filters( 'wpex_search_entry_divider', $divider );
}

/*-------------------------------------------------------------------------------*/
/* [ Cards ]
/*-------------------------------------------------------------------------------*/

/**
 * Search card entry style
 */
function wpex_search_entry_card_style() {
	$style = get_theme_mod( 'search_entry_card_style' );

	/**
	 * Filters the search entry card style.
	 *
	 * @param string $style
	 */
	$style = apply_filters( 'wpex_search_entry_card_style', $style );

	return $style;
}

/**
 * Search card entry.
 */
function wpex_search_entry_card() {
	$card_style = wpex_search_entry_card_style();

	if ( ! $card_style ) {
		return false;
	}

	$args = [
		'style'          => $card_style,
		'post_id'        => get_the_ID(),
		'thumbnail_size' => 'search_results', // @todo rename to 'search_entry'?
		'excerpt_length' => wpex_search_entry_excerpt_length(),
	];

	if ( $overlay_style = wpex_overlay_style() ) {
		$args['thumbnail_overlay_style'] = $overlay_style;
	}

	/**
	 * Filters the search entry card style args.
	 *
	 * @param array $args
	 */
	$args = (array) apply_filters( 'wpex_search_entry_card_args', $args );

	wpex_card( $args );

	return true;
}