<?php
declare( strict_types=1 );

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

add_action(
	'wp_enqueue_scripts',
	function () {
		if ( ! function_exists( 'et_get_option' ) ) {
			return;
		}
		$color_scheme = et_get_option( 'color_schemes', 'none' );
		$main_color   = '#2ea3f2';

		switch ( $color_scheme ) {
			case 'pink':
				$main_color = '#c37cc6';
				break;
			case 'green':
				$main_color = '#7cc68d';
				break;
			case 'red':
				$main_color = '#cd5c5c';
				break;
			case 'orange':
				$main_color = '#edb059';
				break;
		}

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

/**
 * ╭────────────────────╮
 * | CSS customizations |
 * ╰────────────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		ob_start();
		?>
		<style>
			.et_pb_widget ul li.fibofilters-item {
				margin-bottom: 0 !important;
			}
            div.fibofilters-ordering {
                display: flex;
                justify-content: flex-end;
            }
            @media (max-width: 480px) {
                div.fibofilters-ordering {
                    justify-content: start;
                    margin-bottom: 16px;
                    width: 100%;
                }
            }
		</style>
		<?php
		$css = ob_get_clean();

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

/**
 * Global Divi option:
 * Divi ➝  Theme Options ➝ General ➝ Number of Products displayed on WooCommerce archive pages.
 */
add_filter(
	'fibofilters/config/products_per_page',
	function (): int {
		if ( function_exists( 'et_get_option' ) ) {
			$ppp = (int) et_get_option( 'divi_woocommerce_archive_num_posts', '9' );
			if ( ! empty( $ppp ) ) {
				return $ppp;
			}
		}

		return 9;
	}
);


/**
 * Add filters placeholders specific for Divi Builder,
 * because 'woocommerce_before_shop_loop' and woocommerce_after_shop_loop hooks don't work there.
 */

// woocommerce_shortcode_{$this->type}_loop_no_results

add_filter(
	'fibofilters/before_enqueue_front_assets',
	function ( $output ) {

		if ( empty( $output['context']['show_filters'] ) || ! function_exists( 'et_theme_builder_get_template_layouts' ) ) {
			return;
		}

		$layouts = et_theme_builder_get_template_layouts();

		// Break early if Divi doesn't use builder for body
		if ( empty( $layouts ) || ! $layouts['et_body_layout']['override'] ) {
			return;
		}

		add_action(
			'woocommerce_shortcode_before_products_loop',
			function () {
				global $woocommerce_loop;

				/**
				 * If an attribute "is_paginated" isn't set to true,
				 *  then WooCommerce doesn't call standards hooks like "woocommerce_before_shop_loop"
				 *  We have to use alternative hooks inside the shortcode [products]
				 *  like "woocommerce_shortcode_before_products_loop"
				 */
				if ( isset( $woocommerce_loop ) && ! $woocommerce_loop['is_paginated'] ) {
					do_action( 'fibofilters/front/display_horizontal_filters_placeholder' );
					do_action( 'fibofilters/front/display_mobile_filters_placeholder' );
					do_action( 'fibofilters/front/display_applied_filters_placeholder' );
				}
			}
		);

		add_action(
			'woocommerce_shortcode_after_products_loop',
			function () {
				global $woocommerce_loop;

				/**
				 * If an attribute "is_paginated" isn't set to true,
				 *  then WooCommerce doesn't call standards hooks like "woocommerce_after_shop_loop"
				 *  We have to use alternative hooks inside the shortcode [products]
				 *  like "woocommerce_shortcode_after_products_loop"
				 */
				if ( isset( $woocommerce_loop ) && ! $woocommerce_loop['is_paginated'] ) {
					do_action( 'fibofilters/front/display_load_more_button_placeholder' );
				}
			}
		);
	}
);

/**
 * ╭──────────────────────────────────────────────╮
 * | Add custom class to Divi Builder Shop module |
 * ╰──────────────────────────────────────────────╯
 * Filter: "et_builder_{$module_name}_classes"
 */
add_filter(
	'et_builder_shop_classes',
	function ( $classname ) {
		$classname[] = 'fibofilters-divi-shop-module';

		return $classname;
	}
);

/**
 * ╭───────────────────────────────╮
 * | Adjust product loop selectors |
 * ╰───────────────────────────────╯
 */
add_filter(
	'fibofilters/config/products_loop_selectors',
	function ( $selectors ) {
		$selectors[] = '.fibofilters-divi-shop-module ul.products';
		$selectors[] = 'ul.products';

		return $selectors;
	},
	15
);
