<?php
declare( strict_types=1 );

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

/*
 * ╭─────────────────────╮
 * | Pagination selector |
 * ╰─────────────────────╯
 */
add_filter( 'fibofilters/config/pagination_selectors', fn() => [ '.page-numbers' ] );

/*
 * ╭────────────────────────────────╮
 * | Hide pagination from the theme |
 * ╰────────────────────────────────╯
 */
add_action(
	'template_redirect',
	function () {
		$context    = apply_filters( 'fibofilters/request/get_context', [] );
		$pagination = $this->get_config_value( 'pagination_strategy' );
		if ( isset( $context['show_filters'] ) && $context['show_filters'] && $pagination !== 'pagination' ) {
			remove_action( 'footer_action_bar', 'atomion_get_posts_pagination' );
		}

		$filter_location = $this->get_config_value( 'filter_location' );
		if ( $filter_location === 'sidebar' ) {
			/** @var \FiboFilters\Filtering\Front $front */
			$front = FiboFilters\DI\ContainerWrapper::get_instance()->get( \FiboFilters\Filtering\Front::class );
			remove_action( 'woocommerce_after_shop_loop', [ $front, 'display_load_more_button_placeholder' ], 5 );
			add_action( 'woocommerce_after_main_content', [ $front, 'display_load_more_button_placeholder' ], 15 );
		}
	},
	PHP_INT_MAX - 5
);

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

/**
 * ╭────────────────────╮
 * | CSS customizations |
 * ╰────────────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		// Custom CSS.
		ob_start();
		?>
		<style>
			@media (max-width: 768px) {
				.fibofilters-horizontal-filters-container {
					order: 4;
				}
				.fibofilters-mobile-filters-container {
					order: 4;
				}
				.fibofilters-applied-filters-container {
					order: 4;
				}
				.fibofilters-show-previous-button-container {
					order: 4;
				}
				.fibofilters-show-more-button-container {
					order: 6;
				}
				#shop-sidebar {
					display: none;
				}
			}
		</style>
		<?php
		$css = ob_get_clean();

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