<?php
declare( strict_types=1 );

namespace FiboFilters\Integrations\Plugins;

use FiboFilters\Struct\QueryContext;

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

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound

/**
 * Class AeliaCurrencySwitcherForWooCommerce
 *
 * Plugin Name: Aelia Currency Switcher for WooCommerce
 * Plugin URI: https://aelia.co/shop/currency-switcher-woocommerce/
 * Tested versions: 5.2.4.241202
 */
class AeliaCurrencySwitcherForWooCommerce extends AbstractPlugin {
	public function init(): void {
		add_filter( 'fibofilters/config/multicurrency', [ $this, 'multicurrency_data' ] );
		add_filter( 'fibofilters/indexer/filters_source_query/data/unparsed', [ $this, 'add_prices_in_other_currencies' ] );
		add_filter( 'fibofilters/request/get_user_query', [ $this, 'adjust_user_query' ] );
	}

	public function multicurrency_data( array $multicurrency ): array {
		$language = $this->multilingual->is_multilingual() ? $this->multilingual->get_current_language() : '';
		$ctx      = new QueryContext( $language, 'main', false );
		$sources  = $this->sources->get_all_sources( $ctx );

		$multicurrency['baseCurrency'] = apply_filters( 'wc_aelia_cs_base_currency', '' );
		/**
		 * Price sources are the sources that contain prices in other currencies.
		 * e.g.
		 * [
		 *  'price_EUR' => 1,
		 *  'price_GBP' => 2,
		 * ]
		 */
		$multicurrency['priceSources'] = array_filter(
			$sources,
			function ( $value, $key ) {
				return preg_match( '/^price_[A-Z]{3}$/', $key );
			},
			ARRAY_FILTER_USE_BOTH
		);

		return $multicurrency;
	}

	/**
	 * Add prices in other currencies to the raw data in SourceQuery.
	 *
	 * @param array $rows Unparsed data.
	 */
	public function add_prices_in_other_currencies( array $rows ): array {
		if ( empty( $rows ) ) {
			return $rows;
		}

		$base_currency = apply_filters( 'wc_aelia_cs_base_currency', '' );
		$currencies    = apply_filters( 'wc_aelia_cs_enabled_currencies', [] );
		$currencies    = array_diff( $currencies, [ $base_currency ] );

		foreach ( $rows as $i => $row ) {
			if ( isset( $row['price'] ) ) {
				foreach ( $currencies as $currency ) {
					$rows[ $i ][ 'price_' . $currency ] = (string) apply_filters( 'wc_aelia_cs_get_product_price', $row['price'], $row['ID'], $currency );
				}
			}
		}

		return $rows;
	}

	/**
	 * Adjust the user query if price is used as a source.
	 *
	 * @param array $user_query User query.
	 */
	public function adjust_user_query( array $user_query ): array {
		if ( empty( $user_query ) ) {
			return $user_query;
		}

		$current_currency = get_woocommerce_currency();
		$base_currency    = apply_filters( 'wc_aelia_cs_base_currency', '' );

		// If the current currency is not the base currency, we need to adjust the source key for the price.
		if ( $current_currency !== $base_currency ) {
			foreach ( $user_query as $i => $query ) {
				if ( isset( $query['source'] ) && $query['source'] === 'price' ) {
					// Append the current currency to the source key.
					$user_query[ $i ]['source'] = $query['source'] . '_' . $current_currency;
				}
			}
		}

		return $user_query;
	}
}
