<?php
declare( strict_types=1 );

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

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

/**
 * ╭────────────╮
 * | Custom CSS |
 * ╰────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		wp_add_inline_style(
			'fibofilters',
			'.fibofilters-product-placeholder div {
				display: flex;
    			padding-bottom: 40px;
			}
			.fibofilters-item {
    			padding: 0 0 .5em !important;
    			line-height: 1em !important;
			}
			.fibofilters-input__label  {
				color: #999;
			}'
		);
	}
);

/**
 * ╭───────────────────╮
 * | Products per page |
 * ╰───────────────────╯
 */
add_filter(
	'fibofilters/config/products_per_page',
	function (): int {
		$cols = get_option( 'woocommerce_catalog_columns' );
		$rows = get_option( 'woocommerce_catalog_rows' );

		if ( ! empty( $cols ) && intval( $cols ) > 0 && ! empty( $rows ) && intval( $rows ) > 0 ) {
			return intval( $cols ) * intval( $rows );
		}

		return 12;
	}
);

/**
 * ╭────────────────────────────────╮
 * | "Show more product" as button  |
 * ╰────────────────────────────────╯
 */
add_filter( 'fibofilters/config/show_more_as_button', '__return_true' );

/**
 * ╭─────────────────────╮
 * | Pagination strategy |
 * ╰─────────────────────╯
 * Hestia Pro -> Customize -> Shop Settings -> Shop -> Pagination
 */
add_filter(
	'fibofilters/config/pagination_strategy',
	function ( $pagination_strategy ) {
		$product_pagination_options = get_theme_mod( 'hestia_shop_pagination_type', 'number' );

		if ( empty( $product_pagination_options ) ) {
			return $pagination_strategy;
		}

		if ( $product_pagination_options === 'number' ) {
			$pagination_strategy = 'pagination';
		} elseif ( $product_pagination_options === 'infinite' ) {
			$pagination_strategy = 'infinite-scroll';
		}

		return $pagination_strategy;
	}
);

/*
 * ╭─────────────────────────────────────────╮
 * | Disable infinite scroll from Hestia Pro |
 * ╰─────────────────────────────────────────╯
 * Determine if the Hestia "Filters" button that opens off-canvas, should be shown or not.
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		wp_deregister_script( 'hestia-woo-infinite-scroll' );
	},
	20
);

/*
 * ╭───────────────────╮
 * | Hestia Off-canvas |
 * ╰───────────────────╯
 * Determine if the Hestia "Filters" button that opens off-canvas, should be shown or not.
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		// phpcs:ignore
		$filter_location            = $this->get_config_value( 'filter_location' );
		// phpcs:ignore
		$hide_sidebar_button_tablet = $filter_location === 'sidebar' && $this->get_config_value( 'tablet_filters_enabled' );
		// phpcs:ignore
		$hide_sidebar_button_mobile = $filter_location === 'sidebar' && $this->get_config_value( 'mobile_filters_enabled' );
		// phpcs:ignore
		$main_breakpoint            = absint( $this->get_config_value( 'main_breakpoint' ) );
		// phpcs:ignore
		$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 esc_attr( $mobile_breakpoint + 1 ); ?>px) {
				.hestia-sidebar-open.btn {
					display: none;
				}
			}

			<?php endif; ?>

			<?php if ( $hide_sidebar_button_tablet ) : ?>
			@media (min-width: <?php echo esc_attr( $mobile_breakpoint + 1 ); ?>px) and (max-width: <?php echo esc_attr( $main_breakpoint ); ?>px) {
				.hestia-sidebar-open.btn{
					display: none;
				}
			}

			<?php endif; ?>

		</style>
		<?php
		$css = ob_get_clean();

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