<?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( --fs-color-primary, var( --fibofilters--colors--base-org ) );
  --fibofilters--colors--link: var( --fs-experimental-link-color, var( --fibofilters--colors--link-org ) );
}
'
		);
	}
);

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

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

		return 12;
	}
);

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

/*
 * ╭────────────╮
 * | Pagination |
 * ╰────────────╯
 */
add_filter(
	'fibofilters/config/pagination_args',
	function ( $args ) {
		return wp_parse_args(
			[
				'prev_text' => '<i class="icon-angle-left"></i>',
				'next_text' => '<i class="icon-angle-right"></i>',
				'end_size'  => 3,
				'mid_size'  => 3,
			],
			$args
		);
	}
);

/*
 * ╭───────────────────╮
 * | Custom JS and CSS |
 * ╰───────────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {

		// Custom CSS.
		ob_start();
		?>
		<style>

			.col-inner ul li.fibofilters-item {
				margin-left: 0;
			}

			.sidebar-inner .fibofilters-vertical-filters-container {
				padding-right: 20px;
			}

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

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

		// Custom JS.
		ob_start();
		?>
		<script>
			(function ($) {
				if (typeof fiboFilters !== 'undefined' && typeof jQuery !== 'undefined') {
					function fibofiltersFlatsomeFix() {
						if (typeof Flatsome !== 'object') {
							return;
						}

						const $container = jQuery('.shop-container');
						jQuery('.shop-container .quick-view-added').removeClass('quick-view-added');
						Flatsome.attach('quick-view', $container);
						Flatsome.attach('lazy-load-images', $container)
						Flatsome.attach('quick-view', $container)
						Flatsome.attach('tooltips', $container)
						Flatsome.attach('add-qty', $container)
						Flatsome.attach('wishlist', $container)
						Flatsome.attach('equalize-box', $container)
					}

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

/*
 * ╭─────────────────────╮
 * | Flatsome Off-canvas |
 * ╰─────────────────────╯
 * Determine if the Flatsome "Filters" button that opens off-canvas, should be shown or not.
 */
add_action(
	'template_redirect',
	function () {
		$show_flatsome_off_canvas = true;
		$sidebar_position         = get_theme_mod( 'category_sidebar', 'left-sidebar' );
		$filter_location          = $this->get_config_value( 'filter_location' );
		$force_tablet_view        = $this->get_config_value( 'tablet_filters_enabled' );
		$force_mobile_view        = $this->get_config_value( 'mobile_filters_enabled' );

		// Case 1 - When filters are above products, always hide the Flatsome off-canvas
		if ( $filter_location === 'above_products' ) {
			$show_flatsome_off_canvas = false;
		}

		// Case 2 - When filters are displayed in sidebar
		if ( $filter_location === 'sidebar' ) {

			// Case 2a - We force horizontal filters on tablet or mobile filters and Off canvas is disabled on desktop in Flatsome theme
			if ( ( $force_tablet_view || $force_mobile_view ) && $sidebar_position !== 'off-canvas' ) {
				$show_flatsome_off_canvas = false;
			}
		}
		if ( ! $show_flatsome_off_canvas ) {
			remove_action( 'flatsome_category_title', 'flatsome_add_category_filter_button', 20 );
		}
	}
);

/*
 * ╭──────────────────────────╮
 * | Flatsome AJAX Pagination |
 * ╰──────────────────────────╯
 * This feature breaks FiboFilters in some places, e.g. the filters disappear after sorting products.
 * Since we are completely overwriting the pagination anyway, the Flatsome AJAX pagination feature might be disabled programmatically.
 */
add_filter( 'experimental_flatsome_pjax_enabled', '__return_false' );

