<?php

declare( strict_types=1 );

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

/**
 * ╭────────╮
 * | Colors |
 * ╰────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		wp_add_inline_style(
			'fibofilters',
			'body {
				--fibofilters--colors--base: var( --color-primary, var( --fibofilters--colors--base-org ) );
				--fibofilters--colors--link: var( --bs-link-color, var( --fibofilters--colors--link-org ) );
			}'
		);
	}
);

/**
 * ╭─────────────────────╮
 * | Pagination strategy |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/config/pagination_strategy',
	function ( $pagination_strategy ) {

		$pagination = get_theme_mod( 'partdo_paginate_type' );

		if ( $pagination === 'loadmore' ) {
			$pagination_strategy = 'show-more-button';
		} elseif ( $pagination === 'default' ) {
			$pagination_strategy = 'pagination';
		} elseif ( $pagination === 'infinite' ) {
			$pagination_strategy = 'infinite-scroll';
		}

		return $pagination_strategy;
	}
);

/**
 * ╭─────────────────╮
 * | Pagination args |
 * ╰─────────────────╯
 */
add_filter(
	'fibofilters/config/pagination_args',
	function ( $args ) {

		return wp_parse_args(
			[
				'prev_text' => '<i class="klbth-icon-left-open-big"></i>',
				'next_text' => '<i class="klbth-icon-right-open-big"></i>',
			],
			$args
		);
	}
);

/**
 * ╭───────────────────╮
 * | Products per page |
 * ╰───────────────────╯
 */
add_filter(
	'fibofilters/config/products_per_page',
	function ( $products_per_page ): int {

		$per_page = ! empty( $_GET['perpage'] ) && is_numeric( $_GET['perpage'] ) ? absint( $_GET['perpage'] ) : 0;
		if ( $per_page > 0 ) {
			$products_per_page = $per_page;
		}

		return $products_per_page;
	}
);

/**
 * ╭─────────────────────╮
 * | Product placeholder |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/request/html/product_placeholder',
	function () {
		?>
		<div class="fibofilters-product-placeholder column-item product type-product">
			<div class="product product-type-1 custom-hover">
				<div class="product-wrapper">
					<div class="product-content">
						<div class="thumbnail-wrapper entry-media">
							<img src="<?php echo esc_url( wc_placeholder_img_src() ); ?> " alt="Placeholder image"/>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php
		return (string) ob_get_clean();
	}
);

/**
 * ╭───────────────────╮
 * | Disable AJAX Shop |
 * ╰───────────────────╯
 * In Customizing -> Shop Settings -> General -> Ajax on Shop Page.
 * This feature prevents FiboFilters from functioning properly.
 */
add_filter( 'theme_mod_partdo_ajax_on_shop', '__return_false' );


/**
 * ╭─────────────────╮
 * | Product sorting |
 * ╰─────────────────╯
 */
add_filter( 'fibofilters/config/override_ordering_enabled', '__return_true' );

add_filter(
	'fibofilters/config/ordering_selectors',
	function () {
		return [ '.sorting-products .woocommerce-ordering' ];
	}
);

/**
 * ╭────────────╮
 * | Custom CSS |
 * ╰────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		$filter_location                            = $this->get_config_value( 'filter_location' );
		$hide_sidebar_button_tablet                 = $filter_location === 'above_products' || ( $filter_location === 'sidebar' && $this->get_config_value( 'tablet_filters_enabled' ) );
		$hide_sidebar_button_mobile                 = $filter_location === 'above_products' || ( $filter_location === 'sidebar' && $this->get_config_value( 'mobile_filters_enabled' ) );
		$hide_sidebar_wide_button                   = $filter_location === 'above_products';
		$hide_sidebar_wide_button_tablet_and_mobile = $filter_location === 'sidebar';
		$main_breakpoint                            = absint( $this->get_config_value( 'main_breakpoint' ) );
		$mobile_breakpoint                          = absint( $this->get_config_value( 'mobile_breakpoint' ) );

		// Custom CSS.
		ob_start();
		?>
		<style>
			<?php if ( $hide_sidebar_wide_button ) : ?>
			.content-wrapper .before-shop-loop .filter-wide-button {
				display: none;
			}

			<?php endif; ?>

			<?php if ( $hide_sidebar_wide_button_tablet_and_mobile ) : ?>
			@media (max-width: <?php echo $main_breakpoint; ?>px) {
				.content-wrapper .before-shop-loop .filter-wide-button {
					display: none;
				}
			}

			<?php endif; ?>

			<?php if ( $hide_sidebar_button_mobile ) : ?>
			@media (max-width: <?php echo $mobile_breakpoint + 1; ?>px) {
				.content-wrapper .before-shop-loop .filter-button {
					display: none;
				}
			}

			<?php endif; ?>

			<?php if ( $hide_sidebar_button_tablet ) : ?>
			@media (min-width: <?php echo $mobile_breakpoint + 1; ?>px) and (max-width: <?php echo $main_breakpoint; ?>px) {
				.content-wrapper .before-shop-loop .filter-button {
					display: none;
				}
			}

			<?php endif; ?>
		</style>
		<?php
		$css = ob_get_clean();

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

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

		return $params;
	}
);
