<?php
declare( strict_types=1 );

use FiboFilters\Config\Env;

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

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

		$base_color = '#1aada3';

		if ( function_exists( 'basel_get_opt' ) ) {
			$color = basel_get_opt( 'primary-color' );

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

		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( 'basel_get_opt' ) ) {
			$ppp = basel_get_opt( 'shop_per_page' );
			if ( ! empty( $ppp ) && is_numeric( $ppp ) ) {
				return intval( $ppp );
			}
		}

		return 12;
	}
);

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

		if ( function_exists( 'basel_get_page_layout' ) ) {
			$page_layout = basel_get_page_layout();

			if ( ! empty( $page_layout ) && is_string( $page_layout ) && $page_layout === 'sidebar-left' ) {
				return 'left';
			}
		}

		return 'right';
	}
);

/**
 * ╭─────────────────────╮
 * | Product placeholder |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/request/html/product_placeholder',
	function ( $placeholder ) {
		if (
			! function_exists( 'basel_get_opt' )
			|| ! function_exists( 'basel_loop_prop' )
			|| ! function_exists( 'basel_set_loop_prop' )
			|| ! function_exists( 'basel_get_wide_items_array' )
			|| ! function_exists( 'basel_get_grid_el_class' )
		) {
			return $placeholder;
		}

		$is_slider       = basel_loop_prop( 'is_slider' );
		$is_shortcode    = basel_loop_prop( 'is_shortcode' );
		$different_sizes = basel_loop_prop( 'products_different_sizes' );
		$hover           = basel_loop_prop( 'product_hover' );
		$current_view    = basel_loop_prop( 'products_view' );
		$shop_view       = basel_get_opt( 'shop_view' );

		basel_set_loop_prop( 'woocommerce_loop', (int) basel_loop_prop( 'woocommerce_loop' ) + 1 );
		$woocommerce_loop = basel_loop_prop( 'woocommerce_loop' );

		$classes = [ 'product-grid-item' ];

		if ( $shop_view === 'grid' || $shop_view === 'list' ) {
			$current_view = $shop_view;
		}

		if ( $is_slider ) {
			$current_view = 'grid';
		}

		if ( $is_shortcode ) {
			$current_view = basel_loop_prop( 'products_view' );
		}

		if ( $current_view === 'list' ) {
			$hover = 'list';
			basel_set_loop_prop( 'products_columns', 1 );
			$classes[] = 'product-list-item';
		} else {
			$classes[] = 'basel-hover-' . $hover;
		}

		$classes[] = 'product';

		$products_columns = (int) basel_loop_prop( 'products_columns' );

		if ( $different_sizes && in_array( $woocommerce_loop, basel_get_wide_items_array( $different_sizes ), true ) ) {
			basel_set_loop_prop( 'double_size', true );
		}

		$xs_columns = (int) basel_get_opt( 'products_columns_mobile' ) ? (int) basel_get_opt( 'products_columns_mobile' ) : 2;
		$xs_size    = 12 / $xs_columns;

		if ( $products_columns === 1 ) {
			$xs_size = 12;
		}

		if ( ! $is_slider ) {
			$classes[] = basel_get_grid_el_class( $woocommerce_loop, $products_columns, $different_sizes, $xs_size );
		} else {
			$classes[] = 'product-in-carousel';
		}

		$classes[] = 'fibofilters-product-placeholder';

		ob_start();
		?>
		<div <?php wc_product_class( $classes ); ?>>
			<div class="product-element-top">
				<img src="<?php echo esc_url( wc_placeholder_img_src() ); ?> " alt="Placeholder image"/>
			</div>
		</div>
		<?php
		return (string) ob_get_clean();
	}
);

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

		if ( function_exists( 'basel_get_opt' ) ) {
			$pagination = basel_get_opt( 'shop_pagination' );

			if ( $pagination === 'more-btn' ) {
				$pagination_strategy = 'show-more-button';
			} elseif ( $pagination === 'pagination' ) {
				$pagination_strategy = 'pagination';
			} elseif ( $pagination === 'infinit' ) {
				$pagination_strategy = 'infinite-scroll';
			}
		}

		return $pagination_strategy;
	}
);

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

		// Custom CSS.
		ob_start();
		?>
		<style>
			.fibofilters-show-more-button-container .fibofilters-button {
				border-color: #000;
				border-width: 2px;
				font-weight: 600;
				background-color: transparent;
				color: #000
			}

			.fibofilters-show-more-button-container .fibofilters-button:hover {
				background-color: #000;
				border-color: #000;
				color: #FFF
			}

			.basel-products-holder {
				display: flex;
				flex-wrap: wrap;
			}

			.woocommerce-ordering {
				padding-bottom: 20px;
			}

			.sidebar-widget li.fibofilters-item {
				margin: 0;
			}
		</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 ($) {
				$(document).ready(function () {
					$('.products-footer .basel-load-more').remove();
				});
			}(jQuery));
		</script>
		<?php
		$this->enqueue_inline_script( (string) ob_get_clean(), [ 'jquery' ] );
	}
);

/**
 * ╭───────────────────╮
 * | Disable AJAX shop |
 * ╰───────────────────╯
 * We had to disable the Basel AJAX shop feature because ot overrides FiboFilters AJAX.
 * The Basel theme set their settings on the hook "wp" with priority 5.
 */
add_action( 'wp', function () {
	if ( isset( $GLOBALS['xts_basel_options']['ajax_shop'] ) ) {
		$GLOBALS['xts_basel_options']['ajax_shop'] = '0';
	}
}, 6 );

/**
 * ╭──────────────────────────────────╮
 * | Remove <link> in single products |
 * ╰──────────────────────────────────╯
 * The Basel theme added <link> elements in the output of FiboFilters\Filtering\Request::get_single_products()
 * by calling basel_enqueue_inline_style() function.
 */
add_action( 'fibofilters/request/single_products/before_loop', function () {
	add_filter( 'woocommerce_is_rest_api_request', '__return_true' );
} );
add_action( 'fibofilters/request/single_products/after_loop', function () {
	add_filter( 'woocommerce_is_rest_api_request', '__return_false' );
} );
