<?php
declare( strict_types=1 );

use FiboFilters\Config\Env;

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

/*
 * ╭─────────────────────────────────────────────────────────────────────────────────╮
 * | Disable option: WooCommerce (Shop) >> Shop >> Shop Page Filters >> Ajax Filters |
 * ╰─────────────────────────────────────────────────────────────────────────────────╯
 * When this option is enabled, there is a conflict with our filters.
 */
add_filter( 'theme_mod_ajax_product_filter', '__return_false' );

/*
 * ╭──────────────────╮
 * | Filters position |
 * ╰──────────────────╯
 * Change position of filter placeholders from `woocommerce_before_shop_loop` to `etheme_before_product_loop_start`.
 */
add_filter( 'fibofilters/front/hook_into_woocommerce', '__return_false' );

add_action(
	'fibofilters/initialized',
	function () {
		do_action( 'fibofilters/add_action', 'etheme_before_product_loop_start', 'filtering.front', 'display_horizontal_filters_placeholder', 20 );
		do_action( 'fibofilters/add_action', 'etheme_before_product_loop_start', 'filtering.front', 'display_mobile_filters_placeholder', 30 );
		do_action( 'fibofilters/add_action', 'etheme_before_product_loop_start', 'filtering.front', 'display_applied_filters_placeholder', 40 );

		add_filter( 'fibofilters/config/early_products_caching', '__return_true' );
	}
);

/*
 * ╭────────────╮
 * | Breakpoint |
 * ╰────────────╯
 */
add_filter(
	'fibofilters/config/main_breakpoint',
	function (): int {
		return (int) get_theme_mod( 'mobile_header_start_from', 992 );
	}
);

/*
 * ╭─────────────────────╮
 * | Product placeholder |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/request/html/product_placeholder',
	function () {
		$per_row = $this->get_config_value( 'per_row' );
		ob_start();
		$classes = function_exists( 'etheme_get_product_class' ) ? etheme_get_product_class( $per_row ) : '';
		?>
		<div class="product fibofilters-product-placeholder <?php echo esc_attr( $classes ); ?>">
			<div class="content-product ">
				<div class="product-content-image ">
					<img src="<?php echo esc_url( wc_placeholder_img_src() ); ?> " alt="Placeholder image"/>
				</div>
			</div>
		</div>
		<?php
		return (string) ob_get_clean();
	}
);

/*
 * ╭─────────────────────╮
 * | Off-canvas position |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/config/offcanvas_position',
	function (): string {
		if ( ! function_exists( 'etheme_get_option' ) ) {
			return 'left';
		}
		$grid_sidebar = etheme_get_option( 'grid_sidebar', 'left' );

		if ( $grid_sidebar === 'right' ) {
			return 'right';
		}

		return 'left';
	}
);

/*
 * ╭───────────────────╮
 * | Products per page |
 * ╰───────────────────╯
 */
add_filter(
	'fibofilters/config/products_per_page',
	function ( $per_page ): int {
		if ( ! function_exists( 'etheme_get_option' ) ) {
			return (int) $per_page;
		}

		// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound, WordPress.Security.NonceVerification.Recommended
		$result = isset( $_REQUEST['et_per_page'] ) && intval( $_REQUEST['et_per_page'] ) > 0 ? intval( $_REQUEST['et_per_page'] ) : apply_filters( 'etheme_products_per_page', etheme_get_option( 'products_per_page', 12 ) );

		return (int) $result;
	}
);

/*
 * ╭──────────────────────────────────────╮
 * | Override "per_row" on category pages |
 * ╰──────────────────────────────────────╯
 */
add_filter(
	'et_option_category_page_columns',
	function () {
		return 'inherit';
	}
);

add_action(
	'wp_enqueue_scripts',
	function () {
		wp_add_inline_style(
			'fibofilters',
			'
.sidebar-widget .fibofilters-block--category li a.fibofilters-item__interactive {
	padding: 0
}
.fibofilters-item--link {
    display: flex !important;
}
.fibofilters-item {
	width: auto !important;
}
.fibofilters-item--checkbox, .fibofilters-item--radio, .fibofilters-item--button {
    display: block !important;
}
.fibofilters-item--button {
	width: auto !important;
}
.fibofilters-item--more-less {
	display: block !important;
}
body {
  --fibofilters--colors--base: var( --et_active-color, var( --fibofilters--colors--base-org ) );
  --fibofilters--colors--link: var( --et_active-color, var( --fibofilters--colors--link-org ) );
}
'
		);
	}
);

add_action(
	'wp_enqueue_scripts',
	function () {
		ob_start();
		?>
		<script>
			if (typeof fiboFilters !== 'undefined') {
				function fibofiltersXstoreFix() {
					if (typeof etTheme !== 'undefined' && typeof etTheme.contentProdImages === 'function') {
						etTheme.contentProdImages();
						maybeTriggerLazyLoadFix()
					}
				}

				function maybeTriggerLazyLoadFix() {
					const needsFix = document.querySelector('[data-ff-post-id] img.lazyload:not(.et-lazy-loaded)') !== null;

					if (needsFix && typeof window.etTheme?.global_image_lazy === 'function') {
						window.etTheme.global_image_lazy();
					}
				}

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