<?php
declare( strict_types=1 );

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

add_action(
	'wp_enqueue_scripts',
	function () {
		$base_color = get_theme_mod( 'ocean_primary_color', '#13aff0' );
		$link_color = get_theme_mod( 'ocean_links_color', '#333333' );
		wp_add_inline_style(
			'fibofilters',
			"body {
				--fibofilters--colors--base: $base_color;
				--fibofilters--colors--link: $link_color;
			}"
		);
	}
);

add_filter(
	'fibofilters/config/offcanvas_position',
	function (): string {
		$sidebar_position = get_theme_mod( 'ocean_woo_shop_layout', 'left-sidebar' );
		if ( $sidebar_position === 'right-sidebar' ) {
			return 'right';
		} elseif ( $sidebar_position === 'left-sidebar' ) {
			return 'left';
		}

		return 'right';
	}
);

/**
 * ╭───────────────────╮
 * | Products per page |
 * ╰───────────────────╯
 */
add_filter(
	'fibofilters/config/products_per_page',
	function (): int {
		return max( (int) get_theme_mod( 'ocean_woo_shop_posts_per_page', 12 ), 1 );
	}
);

/**
 * ╭─────────────────────╮
 * | Product placeholder |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/request/html/product_placeholder',
	function () {
		global $woocommerce_loop;
		$cols_class = 'span_1_of_3';
		if ( function_exists( 'oceanwp_grid_class' )
			&& ! empty( $woocommerce_loop['columns'] ) ) {
			$cols_class = oceanwp_grid_class( $woocommerce_loop['columns'] );
		}
		ob_start();
		?>
		<li class="product fibofilters-product-placeholder col <?php echo esc_attr( $cols_class ); ?>">
			<div class="product-inner clr">
				<ul class="woo-entry-inner clr">
					<li class="image-wrap">
						<div class="woo-entry-image clr">
							<img src="<?php echo esc_url( wc_placeholder_img_src() ); ?> "
								class="woo-entry-image-main"
								alt="Placeholder image"/>
						</div>
					</li>
				</ul>
			</div>
		</li>
		<?php
		return (string) ob_get_clean();
	}
);

add_action( 'fibofilters/request/single_products/start_loop', '__return_true' );

/**
 * Hide WooCommerce products per page. It means get_template_part( 'woocommerce/result-count' );
 * because we don't support it
 */
add_filter( 'theme_mod_ocean_woo_shop_result_count', '__return_false' );

/**
 * ╭─────────────────────────────────────────────────────────────╮
 * | OceanWP uses the slug "search" so it needs to be restricted |
 * ╰─────────────────────────────────────────────────────────────╯
 */
add_filter(
	'fibofilters/prohibited_url_slugs',
	function ( $slugs ) {
		$slugs[] = 'search';

		return $slugs;
	}
);

