<?php


declare( strict_types=1 );

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

/**
 * ╭────────╮
 * | Colors |
 * ╰────────╯
 * In this theme, there are no explicit options to override the Base Color and Link Color.
 * Therefore, we only replace the default values to ensure the initial color scheme matches the overall design.
 * However, we leave the option to customize colors in FiboFilters.
 */
add_filter(
	'fibofilters/settings/section=appearance',
	function ( $settings ) {
		global $nm_theme_options;

		// Theme options -> Styling -> Font Color - Highlighted Text.
		if ( ! empty( $nm_theme_options['highlight_color'] ) ) {
			$color                    = $nm_theme_options['highlight_color'];
			$settings[100]['default'] = $color; // Base color.
			$settings[200]['default'] = $color; // Link color.
		}

		return $settings;
	}
);

/**
 * ╭───────────────────╮
 * | Products per page |
 * ╰───────────────────╯
 * Theme options -> Shop -> Catalog -> Products per Page
 */
add_filter(
	'fibofilters/config/products_per_page',
	function (): int {
		global $nm_theme_options;

		$per_page = 16;
		if ( function_exists( 'nm_woocommerce_products_per_page' ) ) {
			$cols = intval( isset( $nm_theme_options['shop_columns'] ) ?? 4 );
			$cols = $cols > 0 ? $cols : 4;

			$per_page = nm_woocommerce_products_per_page( $cols );
		}

		return $per_page;
	}
);

/**
 * ╭──────────────────╮
 * | Products per row |
 * ╰──────────────────╯
 * Theme options -> Shop -> Catalog -> Columns
 */
add_filter(
	'fibofilters/config/products_per_row',
	function (): int {
		global $nm_theme_options;

		return isset( $nm_theme_options['shop_columns'] ) && intval( $nm_theme_options['shop_columns'] ) > 0 ? intval( $nm_theme_options['shop_columns'] ) : wc_get_default_products_per_row();
	}
);

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

		$pagination = $nm_theme_options['shop_infinite_load'];

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

		return $pagination_strategy;
	}
);

/**
 * ╭────────────╮
 * | Pagination |
 * ╰────────────╯
 */
add_filter(
	'fibofilters/config/pagination_args',
	function ( $args ) {
		return wp_parse_args(
			[
				'prev_text' => '<i class="nm-font nm-font-angle-thin-left"></i>',
				'next_text' => '<i class="nm-font nm-font-angle-thin-right"></i>',
			],
			$args
		);
	}
);

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

/**
 * ╭─────────────────────╮
 * | Off-canvas position |
 * ╰─────────────────────╯
 * Theme Options -> Shop Filters -> Sidebar Position
 */
add_filter(
	'fibofilters/config/offcanvas_position',
	function (): string {
		global $nm_theme_options;

		$layout = 'left';
		if ( ! empty( $nm_theme_options['shop_filters_sidebar_position'] ) ) {
			$layout = $nm_theme_options['shop_filters_sidebar_position'];
		}

		return $layout;
	}
);

/**
 * ╭───────────────────────╮
 * | Disables AJAX filters |
 * ╰───────────────────────╯
 * Disables filters provided by the theme.
 * Theme Options -> Shop Filters -> AJAX
 */
add_filter(
	'option_nm_theme_options',
	function ( $option ) {
		if ( isset( $option['shop_filters_enable_ajax'] ) ) {
			$option['shop_filters_enable_ajax'] = '0';
		}

		return $option;
	}
);

/**
 * ╭───────────────────────────────────╮
 * | Block the theme's infinite scroll |
 * ╰───────────────────────────────────╯
 * This prevents the theme's infinite scroll from triggering alongside the custom implementation.
 */
add_action(
	'woocommerce_before_template_part',
	function ( $name ) {
		global $nm_theme_options;

		if ( $name === 'loop/pagination.php' ) {
			// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
			$nm_theme_options['shop_infinite_load'] = '0';
		}
	}
);

/**
 * ╭────────────╮
 * | Custom CSS |
 * ╰────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		$filter_location            = $this->get_config_value( 'filter_location' );
		$hide_sidebar_button_tablet = $filter_location === 'sidebar' && $this->get_config_value( 'tablet_filters_enabled' );
		$hide_sidebar_button_mobile = $filter_location === 'sidebar' && $this->get_config_value( 'mobile_filters_enabled' );
		$main_breakpoint            = absint( $this->get_config_value( 'main_breakpoint' ) );
		$mobile_breakpoint          = absint( $this->get_config_value( 'mobile_breakpoint' ) );

		// Custom CSS.
		ob_start();
		?>
		<style>
			<?php if ( $hide_sidebar_button_mobile ) : ?>
			@media (max-width: <?php echo $mobile_breakpoint + 1; ?>px) {
				li.nm-shop-sidebar-btn-wrap[data-panel="sidebar"] {
					display: none !important;
				}
			}

			<?php endif; ?>

			<?php if ( $hide_sidebar_button_tablet ) : ?>
			@media (min-width: <?php echo $mobile_breakpoint + 1; ?>px) and (max-width: <?php echo $main_breakpoint; ?>px) {
				li.nm-shop-sidebar-btn-wrap[data-panel="sidebar"] {
					display: none !important;
				}
			}

			<?php endif; ?>

			@media (max-width: 991px) {
				#nm-shop-sidebar .widget.fibofilters-filters-widget .nm-shop-widget-col:last-child {
					height: auto;
				}
			}

			.fibofilters-block ul.fibofilters-block__items-list li {
				margin-bottom: 0;
			}
		</style>
		<?php
		$css = ob_get_clean();

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