<?php
declare( strict_types=1 );

use FiboFilters\Config\Env;

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

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

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

/*
 * ╭─────────────────────╮
 * | Off-canvas position |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/config/offcanvas_position',
	function (): string {

		$default          = 'right';
		$sidebar_position = get_theme_mod( 'ajaxfilter_panel_pos', $default );

		if ( $sidebar_position === 'right' ) {
			return 'right';
		} elseif ( $sidebar_position === 'left' ) {
			return 'left';
		}

		return in_array( $sidebar_position, [ 'right', 'left' ] ) ? $sidebar_position : $default;
	}
);

/*
 * ╭────────────────────────────────────────────╮
 * | Disable an option "Animate In (on scroll)" |
 * ╰────────────────────────────────────────────╯
 *  This option broke our layout grid. You can find it in:
 *  Customizer -> WooCommerce ➝ Catalog Settings ➝ Product Layout & Components
 */
add_filter( 'theme_mod_loop_animate_in', '__return_false' );

/*
 * ╭─────────────────────────────────────╮
 * | Disable an option "Enable Switcher" |
 * ╰─────────────────────────────────────╯
 *  This option doesn't work, because FiboFilters has used only "Load more button" so far
 *  Customizer -> WooCommerce ➝ Catalog Settings ➝ Grid Components -> Products per page Switcher
 */
add_filter( 'theme_mod_loop_switcher_ppp', '__return_false' );


add_action(
	'fibofilters/request/single_products/before_loop',
	function () {
		ob_start();
		// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
		do_action( 'reycore/woocommerce/loop/before_grid' );
		ob_get_clean();
	}
);

add_action(
	'wp_enqueue_scripts',
	function () {
		// Custom CSS.
		ob_start();
		?>
		<style>
			.fibofilters-show-more-button-container + .rey-ajaxLoadMore {
				display: none;
			}

			.fibofilters-show-more-button-container {
				padding-top: 70px;
			}

			.fibofilters-product-placeholder::before {
				height: 4px !important;
				width: 50% !important;
				display: block !important;
			}

			.fibofilters-product-placeholder .rey-productThumbnail {
				display: flex;
			}
		</style>
		<?php
		$css = ob_get_clean();

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

		// Custom JS.
		ob_start();
		?>
		<script>
			if (typeof fiboFilters !== 'undefined') {
				function fibofiltersReyFix() {
					var gridSelectors = [
						'.rey-siteMain ul.products',
						'.elementor-widget-loop-grid ul.products',
						'.elementor-widget-woocommerce-products ul.products',
					];

					setTimeout(function () {
						document.querySelectorAll(gridSelectors.join(',')).forEach(grid => {
							if (rey) {
								rey.hooks.doAction('product/loaded', grid.querySelectorAll('li.product:not(.--animated-in)'));
							}
						});
					}, 350);
				}

				fiboFilters.hooks.addAction('fiboFilters.renderer.products_loaded', 'fibofilters', fibofiltersReyFix);
				fiboFilters.hooks.addAction('fiboFilters.renderer.product_placeholders_overwritten', 'fibofilters', fibofiltersReyFix);
			}
		</script>
		<?php
		$this->enqueue_inline_script( (string) ob_get_clean() );
	}
);

add_filter(
	'fibofilters/config/pagination_args',
	function ( $args ) {
		if ( ! function_exists( 'reycore__arrowSvg' ) ) {
			return $args;
		}
		return wp_parse_args(
			[
				'prev_text' => reycore__arrowSvg(
					[
						'right'      => false,
						// phpcs:ignore WordPress.WP.I18n.MissingArgDomain
						'attributes' => sprintf( 'title="%s"', __( '&laquo; Previous' ) ),
						'title'      => false,
					]
				),
				'next_text' => reycore__arrowSvg(
					[
						// phpcs:ignore WordPress.WP.I18n.MissingArgDomain
						'attributes' => sprintf( 'title="%s"', __( 'Next &raquo;' ) ),
						'title'      => false,
					]
				),
				'mid_size'  => 3,
				'end_size'  => 1,
				'type'      => 'plain',
			],
			$args
		);
	}
);

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

/**
 * ╭────────────────────────────────────╮
 * | Override AJAX load more pagination |
 * ╰────────────────────────────────────╯
 * This allow us to hide load more button or infinite scroll trigger.
 */
if ( ! function_exists( 'reycore__ajax_load_more_pagination' ) ) {
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
	function reycore__ajax_load_more_pagination() {}
}

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

		if ( $loop_pagination === 'paged' ) {
			$pagination_strategy = 'pagination';
		} elseif ( $loop_pagination === 'load-more' ) {
			$pagination_strategy = 'show-more-button';
		} elseif ( $loop_pagination === 'infinite' ) {
			$pagination_strategy = 'infinite-scroll';
		}

		return $pagination_strategy;
	}
);
