<?php
declare( strict_types=1 );

use FiboFilters\Config\Env;

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

/*
 * ╭──────────────────────────────╮
 * | Colors && CSS customizations |
 * ╰──────────────────────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		wp_add_inline_style(
			'fibofilters',
			'
body {
  --fibofilters--colors--base: var( --nectar-accent-color, var( --fibofilters--colors--base-org ) );
  --fibofilters--colors--link: var( --nectar-accent-color, var( --fibofilters--colors--link-org ) );
}
.fibofilters-item--link .fibofilters-item__interactive{
	display: flex !important;
}
.fibofilters-show-more-button-container button.button.fibofilters-show-more{
    line-height: 24px;
    font-size: 14px;
}

.woocommerce #sidebar div ul li, .material.woocommerce #sidebar div ul li.fibofilters-item--button {
    padding: 3px 7px;
}

'
		);
	}
);

/**
 * ╭─────────────────────╮
 * | Off-canvas position |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/config/offcanvas_position',
	function ( $offcanvas_position ): string {
		if ( ! function_exists( 'get_nectar_theme_options' ) ) {
			return $offcanvas_position;
		}
		$options          = get_nectar_theme_options();
		$main_shop_layout = ! empty( $options['main_shop_layout'] ) ? $options['main_shop_layout'] : '';
		if ( $main_shop_layout === 'right-sidebar' ) {
			return 'right';
		} elseif ( $main_shop_layout === 'left-sidebar' ) {
			return 'left';
		}

		return 'right';
	}
);

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

		return ! empty( $options['woo-products-per-page'] ) && intval( $options['woo-products-per-page'] ) > 0 ? intval( $options['woo-products-per-page'] ) : 12;
	}
);

/**
 * ╭────────────────────────────────╮
 * | Reinit lazy load shop images   |
 * ╰────────────────────────────────╯
 *  Salient >> WooCommerce >> General >> Lazy Load Shop Images
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		if ( ! function_exists( 'get_nectar_theme_options' ) ) {
			return;
		}
		$options            = get_nectar_theme_options();
		$lazy_images_active = isset( $options['product_lazy_load_images'] ) && $options['product_lazy_load_images'] === '1';

		if ( ! $lazy_images_active ) {
			return;
		}
		ob_start();
		?>
		<script>
			if (typeof fiboFilters !== 'undefined') {
				function fibofiltersSalientLazyImagesFix() {
					if (typeof jQuery !== 'undefined') {
						jQuery(window).trigger('salient-lazyloading-image-reinit');
					}
				}

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