<?php


declare( strict_types=1 );

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

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

		$base_color = '#f76b6a';

		if ( ! empty( $nasa_opt['color_primary'] ) && is_string( $nasa_opt['color_primary'] ) ) {
			$base_color = $nasa_opt['color_primary'];
		}

		wp_add_inline_style(
			'fibofilters',
			"body {
				--fibofilters--colors--base: $base_color;
			}"
		);
	}
);

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

		if ( function_exists( 'elessi_loop_shop_per_page' ) ) {

			return elessi_loop_shop_per_page( null );
		}

		return 16;
	}
);

/**
 * ╭────────────────────────╮
 * | Products loop selector |
 * ╰────────────────────────╯
 */
add_filter(
	'fibofilters/config/products_loop_selectors',
	function ( $selectors ) {

		$selectors[] = '.nasa-content-page-products ul.products';

		return $selectors;
	}
);

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

/**
 * ╭────────────────────────────────────╮
 * | Force enabling "Disable Ajax Shop" |
 * ╰────────────────────────────────────╯
 *
 * FiboFilters does not work correctly when shop pages are loaded via AJAX.
 * A page reload is required.
 */
add_filter( 'nasa_theme_options', function ( $options ) {

	if ( isset( $options['disable_ajax_product'] )
		 && $options['disable_ajax_product'] === '0' ) {
		$options['disable_ajax_product'] = '1';
	}

	return $options;
} );

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

		$pagination_style = $nasa_opt['pagination_style'] ?? 'style-2';

		switch ( $pagination_style ) {
			case 'style-1':
			case 'style-2':
				$pagination_strategy = 'pagination';
				break;
			case 'load-more':
				$pagination_strategy = 'show-more-button';
				break;
			case 'infinite':
				$pagination_strategy = 'infinite-scroll';
				break;
		}

		return $pagination_strategy;
	}
);

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

/**
 * ╭────────────╮
 * | Custom CSS |
 * ╰────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		$pagination                 = $this->get_config_value( 'pagination_strategy' );
		$filter_location            = $this->get_config_value( 'filter_location' );
		$hide_sidebar_button_tablet = $filter_location === 'sidebar' && $this->get_config_value( 'tablet_filters_enabled' );
		$hide_sidebar_button_mobile = $filter_location === 'sidebar' && $this->get_config_value( 'mobile_filters_enabled' );
		$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_button_mobile ): ?>
			@media (max-width: <?php echo $mobile_breakpoint + 1; ?>px) {
				body ul.sort-bar .li-toggle-sidebar,
				.nasa-bot-item.nasa-bot-item-sidebar {
					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) {
				body ul.sort-bar .li-toggle-sidebar {
					display: none;
				}
			}

			<?php endif; ?>

			.widget li.fibofilters-item {
				padding-left: 0;
				margin-bottom: 5px;
			}

			.widget li.fibofilters-item::before {
				display: none;
			}

			.fibofilters-orderby {
				padding-right: 28px;
			}

			<?php if( $pagination !== 'pagination' ): ?>
			.nasa-paginations-warp {
				display: none;
			}

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

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

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

		ob_start();
		?>
		<script>
			(function ($) {
				if (typeof fiboFilters !== 'undefined') {
					function fibofiltersElessiFix() {
						$('body').trigger('nasa_after_loaded_ajax_complete', [false, false]);
					}

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

