<?php


declare( strict_types=1 );

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

/**
 * ╭────────╮
 * | Colors |
 * ╰────────╯
 * In the enfold theme, the link color is also the primary color.
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		wp_add_inline_style(
			'fibofilters',
			'body {
				--fibofilters--colors--base: var( --enfold-main-color-primary, var( --fibofilters--colors--base-org ) );
 				--fibofilters--colors--link: var( --enfold-main-color-primary, var( --fibofilters--colors--link-org ) );
			}'
		);
	}
);

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

		return $per_page > 0 ? $per_page : 15;
	}
);


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

/**
 * ╭─────────────────────╮
 * | Off-canvas position |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/config/offcanvas_position',
	function (): string {
		$layout = 'right';
		if ( function_exists( 'avia_get_option' ) && avia_get_option( 'archive_layout' ) === 'sidebar_left' ) {
			$layout = 'left';
		}

		return $layout;
	}
);

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

add_filter(
	'fibofilters/config/pagination_args',
	function ( $args ) {

		return wp_parse_args(
			[
				'prev_text' => '‹',
				'next_text' => '›',
				'mid_size'  => 3,
				'end_size'  => 1,
				'type'      => 'plain',
			],
			$args
		);
	}
);


/**
 * ╭─────────────────╮
 * | Product sorting |
 * ╰─────────────────╯
 */
add_filter( 'fibofilters/config/override_ordering_enabled', '__return_true' );
add_filter( 'fibofilters/config/ordering_selectors', fn() => [ '.avia-product-sorting' ] );

/**
 * ╭────────────╮
 * | Custom CSS |
 * ╰────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		$pagination = $this->get_config_value( 'pagination_strategy' );

		// Custom CSS.
		ob_start();
		?>
		<style>
			.main_color .fibofilters-applied-filters strong {
				color: var(--enfold-main-color-color);
			}

			#top .widget ul.fibofilters-block__items-list {
				float: none;
			}

			.avia-product-sorting .sort-param-count {
				display: none;
			}

			#top .main_color select.fibofilters-orderby {
				color: var(--enfold-main-color-heading);
				max-width: 190px;
			}

			#top .fibofilters-block input[type=search] {
				margin: 0;
			}

			#top .fibofilters-block input[type=number] {
				margin-bottom: 0;
			}

			.sidebar .fibofilters-filters-widget {
				padding: 0 7px;
			}

			.pagination span.page-numbers.dots {
				float: left;
				height: 35px;
				width: 35px;
				line-height: 35px;
				text-align: center;
				padding: 0;
				border-radius: 100px;
				margin-right: 3px;
				box-shadow: 0 0 1px 0 rgba(0, 0, 0, .2);
				background: var(--enfold-main-color-bg2);
			}

			<?php if( $pagination !== 'pagination' ): ?>
			nav.pagination {
				display: none;
			}

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

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