<?php
declare( strict_types=1 );

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

/**
 * ╭───────────────────╮
 * | Products per page |
 * ╰───────────────────╯
 */
add_filter(
	'fibofilters/config/products_per_page',
	function (): int {
		return function_exists( 'armania_loop_shop_per_page' ) ? intval( armania_loop_shop_per_page() ) : 12;
	}
);

/**
 * ╭──────────────────╮
 * | Products per row |
 * ╰──────────────────╯
 */
add_filter(
	'fibofilters/config/products_per_row',
	function (): int {
		return function_exists( 'armania_get_option' ) ? intval( armania_get_option( 'product_loop_columns', 4 ) ) : 4;
	}
);

/**
 * ╭────────────────────────────╮
 * | Products fetching strategy |
 * ╰────────────────────────────╯
 */
add_filter( 'fibofilters/config/products_fetching_strategy', fn() => 'page' );

/*
 * ╭────────╮
 * | Colors |
 * ╰────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		wp_add_inline_style(
			'fibofilters',
			'body {
 --fibofilters--colors--base: var( --e-main-color, var( --fibofilters--colors--base-org ) );
 --fibofilters--colors--link: var( --e-main-color-2, var( --fibofilters--colors--link-org ) );
 }'
		);
	}
);

/**
 * ╭────────────────────╮
 * | CSS customizations |
 * ╰────────────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		// Custom CSS.
		ob_start();
		?>
		<style>
			.shop-control .display-mode-control.mode-number {
				display: none !important;
			}
		</style>
		<?php
		$css = ob_get_clean();

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

/*
 * ╭───────────────────────╮
 * | Products selector fix |
 * ╰───────────────────────╯
 */
add_filter(
	'fibofilters/config/ignored_classes_in_css_selectors',
	function ( $classes ) {
		$classes[] = 'columns-1';
		$classes[] = 'columns-2';
		$classes[] = 'columns-3';
		$classes[] = 'columns-4';
		$classes[] = 'columns-5';
		$classes[] = 'columns-6';

		return $classes;
	}
);

/**
 * ╭─────────────────────╮
 * | Pagination strategy |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/config/pagination_strategy',
	function ( $pagination_strategy ) {
		if ( function_exists( 'armania_get_option' ) ) {
			$pagination = armania_get_option( 'woocommerce_pagination', 'pagination' );

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

		return $pagination_strategy;
	}
);


/**
 * ╭───────────────────────────────╮
 * | Custom pagination for Armania |
 * ╰───────────────────────────────╯
 * We override the pagination function to disable infinite scroll and load more buttons for Armania.
 * Our plugin will handle the pagination instead.
 */
if ( ! function_exists( 'ovic_custom_pagination' ) ) {
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
	function ovic_custom_pagination() {
		echo '<div class="woocommerce-pagination pagination-nav">';
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo paginate_links();
		echo '</div>';
	}
}

/**
 * ╭───────────────────────────────────╮
 * | Overriding the offcanvas position |
 * ╰───────────────────────────────────╯
 */
add_filter(
	'fibofilters/config/offcanvas_position',
	function ( $theme_position ) {
		if ( function_exists( 'armania_get_option' ) ) {
			$theme_position = armania_get_option( 'sidebar_shop_layout', 'left' );

			if ( in_array( $theme_position, [ 'left', 'right' ], true ) ) {
				return $theme_position;
			}
		}

		return $theme_position;
	}
);

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