<?php
declare( strict_types=1 );

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

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

		$base_color = '#fe4f00';

		if ( function_exists( 'freestyle_edge_options' ) ) {
			$color = freestyle_edge_options()->getOptionValue( 'first_color' );
			if ( ! empty( $color ) && is_string( $color ) ) {
				$base_color = $color;
			}
		}

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

/*
 * ╭─────────────────────╮
 * | Off-canvas position |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/config/offcanvas_position',
	function (): string {

		$sidebar_position = 'right';

		if ( function_exists( 'freestyle_edge_sidebar_layout' ) ) {
			$layout = freestyle_edge_sidebar_layout();

			if ( ! empty( $layout ) && strpos( $layout, 'left' ) !== false ) {
				$sidebar_position = 'left';
			}
		}

		return $sidebar_position;
	}
);

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

		return $products_per_page;
	}
);

/*
 * ╭────────────╮
 * | Pagination |
 * ╰────────────╯
 */
add_filter(
	'fibofilters/config/pagination_args',
	function ( $args ) {
		return wp_parse_args(
			[
				'prev_text' => is_rtl() ? '&gt;' : '&lt;',
				'next_text' => is_rtl() ? '&lt;' : '&gt;',
				'type'      => 'plain',
			],
			$args
		);
	}
);

add_action(
	'wp_enqueue_scripts',
	function () {
		// Custom CSS.
		ob_start();
		?>
		<style>
			.fibofilters-show-more {
				color: #fff;
				background-color: var(--fibofilters--colors--base);
				padding: 11px 36px;
				text-transform: uppercase;
				font-weight: bold;
				transition: transform .15s cubic-bezier(.25, .46, .45, .94), background-color .15s ease-out;
			}

			.fibofilters-show-more:hover {
				background-color: #303030;
			}

			.edgtf-woocommerce-page .product img:not(.avatar), .woocommerce .product img:not(.avatar) {
				box-sizing: border-box;
			}
		</style>
		<?php
		$css = ob_get_clean();

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