<?php
declare( strict_types=1 );

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * ╭────────────────────────╮
 * | Products loop selector |
 * ╰────────────────────────╯
 */
add_filter(
	'fibofilters/config/products_loop_selectors',
	function () {
		return [
			'.products.products-grid .row.grid',
			'.products.products-list .row.grid',
		];
	}
);

/**
 * ╭─────────────────────╮
 * | Product placeholder |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/request/html/product_placeholder',
	function () {
		ob_start();
		?>
		<div class="grid product fibofilters-product-placeholder">
			<div class="product-block grid">
				<img src="<?php echo esc_url( wc_placeholder_img_src() ); ?> " alt="Placeholder image"/>
			</div>
		</div>
		<?php
		return (string) ob_get_clean();
	}
);

/*
 * ╭────────╮
 * | Colors |
 * ╰────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {

		wp_add_inline_style(
			'fibofilters',
			'body {
				--fibofilters--colors--base: var( --tb-theme-color, var( --fibofilters--colors--base-org ) );
				--fibofilters--colors--link: var( --tb-theme-color, var( --fibofilters--colors--link-org ) );
			}'
		);
	}
);

/**
 * ╭────────────────────╮
 * | CSS customizations |
 * ╰────────────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		// Custom CSS.
		ob_start();
		?>
		<style>
			.tbay-pagination-load-more {
				display: none;
			}
			.tbay-filter .fibofilters-orderby {
				border: none;
				font-weight: 700;
				padding: 10px 4px;
			}
		</style>
		<?php
		$css = ob_get_clean();

		wp_add_inline_style(
			'fibofilters',
			str_replace( [ '<style>', '</style>' ], '', $css )
		);
	}
);

/*
 * ╭──────────────────────────────╮
 * | Show more products as button |
 * ╰──────────────────────────────╯
 */
add_filter( 'fibofilters/config/show_more_as_button', '__return_true' );

/**
 * ╭─────────────────────╮
 * | Pagination strategy |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/config/pagination_strategy',
	function ( $pagination_strategy ) {
		if ( ! function_exists( 'puca_tbay_get_config' ) ) {
			return $pagination_strategy;
		}

		$product_pagination_style = puca_tbay_get_config( 'product_pagination_style', 'loadmore' );

		if ( $product_pagination_style === 'number' ) {
			$pagination_strategy = 'pagination';
		} elseif ( $product_pagination_style === 'loadmore' ) {
			$pagination_strategy = 'show-more-button';
		}

		return $pagination_strategy;
	}
);

/**
 * ╭───────────────────╮
 * | Products per page |
 * ╰───────────────────╯
 */
add_filter(
	'fibofilters/config/products_per_page',
	function ( $products_per_page ): int {
		if ( function_exists( 'puca_tbay_get_config' ) ) {
			$number_products_per_page = intval( puca_tbay_get_config( 'number_products_per_page' ) );
			if ( $number_products_per_page > 0 ) {
				$products_per_page = $number_products_per_page;
			}
		}
		return $products_per_page;
	}
);

/**
 * ╭──────────────────╮
 * | Products per row |
 * ╰──────────────────╯
 */
add_filter(
	'fibofilters/config/products_per_row',
	function ( $products_per_page ): int {
		if ( function_exists( 'puca_tbay_get_config' ) ) {
			$product_columns = intval( puca_tbay_get_config( 'product_columns' ) );
			if ( $product_columns > 0 ) {
				$products_per_page = $product_columns;
			}
		}
		return $products_per_page;
	}
);

/*
 * ╭──────────────────────────╮
 * | Non-removable URL params |
 * ╰──────────────────────────╯
 */
add_filter(
	'fibofilters/config/non_removable_url_params',
	function ( $params ) {
		$params[] = 'display';

		return $params;
	}
);

add_action(
	'wp_enqueue_scripts',
	function () {
		ob_start();
		?>
		<script>
			if (typeof fiboFilters !== 'undefined') {
				if (typeof jQuery !== 'undefined') {
					<?php // Disable view mode buttons events from theme to allow full page reload. ?>
					jQuery(document).ready(() => {
						setTimeout(() => {
							jQuery('.display-mode button.grid, .display-mode button.list').off('click');
							jQuery('form.display-mode').attr('action', '');
						}, 300);
					});
				}
			}
		</script>
		<?php
		$this->enqueue_inline_script( (string) ob_get_clean(), [ 'jquery' ] );
	}
);
