<?php
declare( strict_types=1 );

use FiboFilters\Config\Env;

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

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

		$base_color = '#0089F7';
		$link_color = '#006EDF';

		if ( function_exists( 'mfn_opts_get' ) ) {

			// Base color.
			$color = mfn_opts_get( 'color-theme' );
			if ( ! empty( $color ) && is_string( $color ) && substr( $color, 0, 1 ) === "#" ) {
				$base_color = $color;
			}

			// Link color.
			$link = mfn_opts_get( 'color-a', '#006EDF' );
			if ( ! empty( $link ) && is_string( $link ) && substr( $link, 0, 1 ) === "#" ) {
				$link_color = $link;
			}
		}

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


/**
 * ╭───────────────────╮
 * | Products per page |
 * ╰───────────────────╯
 */
add_filter(
	'fibofilters/config/products_per_page',
	function ( $products_per_page ): int {

		if ( function_exists( 'mfn_get_per_page' ) ) {
			$products_per_page = mfn_get_per_page();

			if ( empty( $products_per_page ) ) {
				$products_per_page = 12;
			}
		}

		return $products_per_page;
	}
);

/**
 * ╭────────────────────────╮
 * | Products loop selector |
 * ╰────────────────────────╯
 *
 * Necessarily, because post cards uses different CSS classes.
 */
add_filter(
	'fibofilters/config/products_loop_selectors',
	function ( $selectors ) {

		$selectors   = [];
		$selectors[] = 'ul.products';

		return $selectors;
	}
);

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

		if ( function_exists( 'mfn_opts_get' ) && mfn_opts_get( 'shop-infinite-load' ) === '1' ) {
			$pagination_strategy = 'infinite-scroll';
		}

		return $pagination_strategy;
	}
);

/**
 * ╭─────────────────────╮
 * | Pagination selector |
 * ╰─────────────────────╯
 */
add_filter( 'fibofilters/config/pagination_selectors', fn() => [ '.pager_wrapper .pager ' ] );

/**
 * ╭─────────────────╮
 * | Pagination args |
 * ╰─────────────────╯
 */
add_filter(
	'fibofilters/config/pagination_args',
	function ( $args ) {
		return wp_parse_args(
			[
				'type'          => 'plain',
				'wrapper_class' => 'pages',
			],
			$args
		);
	}
);

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

		ob_start();
		?>
		<script>
			(function ($) {
				$(document).ready(function () {
					const $bethemePagination = $('.pager_wrapper .pager');
					if ($bethemePagination.length > 0) {
						$bethemePagination.html('');
					}
				});
			}(jQuery));
		</script>
		<?php
		$this->enqueue_inline_script( (string) ob_get_clean(), [ 'jquery' ] );
	}
);

/**
 * ╭────────────────────────────────────╮
 * | Disable products per page selector |
 * ╰────────────────────────────────────╯
 */
add_filter( 'option_betheme', function ( $options ) {
	if ( is_array( $options ) && array_key_exists( 'shop-list-perpage', $options ) ) {
		$options['shop-list-perpage'] = '0';
	}

	return $options;
} );

