<?php
/**
 * WPML.
 *
 * @package xts
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Direct access not allowed.
}

if ( ! function_exists( 'xts_wpml_register_header_builder_strings' ) ) {
	/**
	 * Register header builder strings.
	 *
	 * @since 1.1.0
	 *
	 * @param string $file File.
	 */
	function xts_wpml_register_header_builder_strings( $file ) {
		global $wpdb;

		if ( is_string( $file ) && 'xts-' . XTS_THEME_SLUG === basename( dirname( $file ) ) && class_exists( 'WPML_Admin_Text_Configuration' ) ) {
			$admin_texts = array();
			$headers     = get_option( 'xts_saved_headers', array() );

			foreach ( $headers as $key => $header ) {
				$admin_texts[] = array(
					'value' => '',
					'attr'  => array( 'name' => 'xts_' . $key ),
					'key'   => array(
						array(
							'value' => '',
							'attr'  => array( 'name' => 'structure' ),
							'key'   => array(
								array(
									'value' => '',
									'attr'  => array( 'name' => 'content' ),
									'key'   => array(
										array(
											'value' => '',
											'attr'  => array( 'name' => '*' ),
											'key'   => array(
												array(
													'value' => '',
													'attr' => array( 'name' => 'content' ),
													'key'  => array(
														array(
															'value' => '',
															'attr'  => array( 'name' => '*' ),
															'key'   => array(
																array(
																	'value' => '',
																	'attr'  => array( 'name' => 'content' ),
																	'key'   => array(
																		array(
																			'value' => '',
																			'attr'  => array( 'name' => '*' ),
																			'key'   => array(
																				array(
																					'value' => '',
																					'attr'  => array( 'name' => 'params' ),
																					'key'   => array(
																						array(
																							'value' => '',
																							'attr'  => array( 'name' => 'content' ),
																							'key'   => array(
																								array(
																									'value' => '',
																									'attr'  => array( 'name' => 'value' ),
																									'key'   => array(),
																								),
																							),
																						),
																						array(
																							'value' => '',
																							'attr'  => array( 'name' => 'title' ),
																							'key'   => array(
																								array(
																									'value' => '',
																									'attr'  => array( 'name' => 'value' ),
																									'key'   => array(),
																								),
																							),
																						),
																						array(

																							'value' => '',

																							'attr'  => array( 'name' => 'button_text' ),

																							'key'   => array(

																								array(

																									'value' => '',

																									'attr'  => array( 'name' => 'value' ),

																									'key'   => array(),

																								),

																							),

																						),
																						array(
																							'value' => '',
																							'attr'  => array( 'name' => 'subtitle' ),
																							'key'   => array(
																								array(
																									'value' => '',
																									'attr'  => array( 'name' => 'value' ),
																									'key'   => array(),
																								),
																							),
																						),
																						array(
																							'value' => '',
																							'attr' => array( 'name' => 'image' ),
																							'key' => array(
																								array(
																									'value' => '',
																									'attr' => array( 'name' => 'value' ),
																									'key' => array(),
																								),
																							),
																						),
																					),
																				),
																			),
																		),
																	),
																),
															),
														),
													),
												),
											),
										),
									),
								),
							),
						),
					),
				);
			}

			$object = (object) array(
				'config'             => array(
					'wpml-config' => array(
						'admin-texts' => array(
							'value' => '',
							'key'   => $admin_texts,
						),
					),
				),
				'type'               => 'theme',
				'admin_text_context' => 'xts-header-builder',
			);

			$config       = new WPML_Admin_Text_Configuration( $object );
			$config_array = $config->get_config_array();

			if ( $config_array ) {
				$st_records          = new WPML_ST_Records( $wpdb );
				$import              = new WPML_Admin_Text_Import( $st_records, new WPML_WP_API() );
				$config_handler_hash = md5( serialize( 'xts' ) ); // phpcs:ignore
				$import->parse_config( $config_array, $config_handler_hash );
			}
		}
	}

	add_filter( 'wpml_parse_config_file', 'xts_wpml_register_header_builder_strings' );
}

if ( ! function_exists( 'xts_ajax_actions_wpml_compatibility' ) ) {
	/**
	 * AJAX actions for WPML.
	 *
	 * @since 1.0.0
	 *
	 * @param array $ajax_actions AJAX Actions.
	 *
	 * @return mixed
	 */
	function xts_ajax_actions_wpml_compatibility( $ajax_actions ) {
		$ajax_actions[] = 'xts_single_product_ajax_add_to_cart';
		$ajax_actions[] = 'xts_quick_view';
		$ajax_actions[] = 'xts_ajax_search';
		$ajax_actions[] = 'xts_load_html_dropdowns';
		$ajax_actions[] = 'xts_update_cart_item';
		$ajax_actions[] = 'xts_get_products_tab_element';
		$ajax_actions[] = 'xts_ajax_search';
		$ajax_actions[] = 'xts_get_product_element';

		return $ajax_actions;
	}

	add_filter( 'wcml_multi_currency_ajax_actions', 'xts_ajax_actions_wpml_compatibility', 10, 1 );
}

if ( class_exists( 'woocommerce_wpml' ) && ! function_exists( 'xts_wpml_shipping_progress_bar_amount' ) ) {
	function xts_wpml_shipping_progress_bar_amount( $limit ) {
		global $woocommerce_wpml;

		$multi_currency = $woocommerce_wpml->get_multi_currency();

		if ( ! empty( $multi_currency->prices ) && method_exists( $multi_currency->prices, 'convert_price_amount' ) ) {
			$limit = $multi_currency->prices->convert_price_amount( $limit );
		}

		return $limit;
	}

	add_filter( 'xts_shipping_progress_bar_amount', 'xts_wpml_shipping_progress_bar_amount' );
	add_filter( 'xts_pricing_amount_discounts_value', 'xts_wpml_shipping_progress_bar_amount' );
}

if ( ! function_exists( 'xts_add_translated_product_to_waitlist' ) ) {
	/**
	 * When a user adds a translated product to the waitlist, the ID of the translated product is recorded in the database instead of the original.
	 * Therefore, when the stock status of the original changes, we need to search the database for not only this ID but also all translation IDs.
	 *
	 * @param array $waitlists Queue for sending notifications about product restocks.
	 * @param int   $product_id ID of the product whose stock status has just changed.
	 *
	 * @return array
	 */
	function xts_add_translated_product_to_waitlist( $waitlists, $product_id ) {
		$db_storage   = XTS\Modules\Waitlist\DB_Storage::get_instance();
		$current_lang = apply_filters( 'wpml_current_language', null );
		$active_langs = apply_filters( 'wpml_active_languages', null );

		if ( ! $current_lang || ! $active_langs ) {
			return $waitlists;
		}

		foreach ( $active_langs as $lang ) {
			if ( $current_lang === $lang['language_code'] ) {
				continue;
			}

			$translated_product_id = apply_filters( 'wpml_object_id', $product_id, 'product', false, $lang['language_code'] );
			$product               = wc_get_product( $translated_product_id );

			if ( ! $product instanceof WC_Product ) {
				continue;
			}

			$waitlists = array_merge( $waitlists, $db_storage->get_subscriptions_by_product( $product ) );
		}

		return $waitlists;
	}

	add_filter( 'xts_waitlists_instock_list', 'xts_add_translated_product_to_waitlist', 10, 2 );
}


if ( ! function_exists( 'xts_add_translated_product_to_waitlist' ) ) {
	/**
	 * When a user adds a translated product to the waitlist, the ID of the translated product is recorded in the database instead of the original.
	 * Therefore, when the stock status of the original changes, we need to search the database for not only this ID but also all translation IDs.
	 *
	 * @param array $waitlists Queue for sending notifications about product restocks.
	 * @param int   $product_id ID of the product whose stock status has just changed.
	 *
	 * @return array
	 */
	function xts_add_translated_product_to_waitlist( $waitlists, $product_id ) {
		$db_storage   = XTS\Modules\Waitlist\DB_Storage::get_instance();
		$current_lang = apply_filters( 'wpml_current_language', null );
		$active_langs = apply_filters( 'wpml_active_languages', null );

		foreach ( $active_langs as $lang ) {
			if ( $current_lang === $lang['language_code'] ) {
				continue;
			}

			$translated_product_id = apply_filters( 'wpml_object_id', $product_id, 'product', false, $lang['language_code'] );
			$product               = wc_get_product( $translated_product_id );

			if ( ! $product instanceof WC_Product ) {
				continue;
			}

			$waitlists = array_merge( $waitlists, $db_storage->get_subscriptions_by_product( $product ) );
		}

		return $waitlists;
	}

	add_filter( 'xts_waitlists_instock_list', 'xts_add_translated_product_to_waitlist', 10, 2 );
}

if ( ! function_exists( 'xts_add_wcml_emails_options_to_translate' ) ) {
	/**
	 * Add emails for WooCommerce Multilingual & Multicurrency plugin translate.
	 *
	 * @param array $email_options List of email options name.
	 *
	 * @return array
	 */
	function xts_add_wcml_emails_options_to_translate( $email_options ) {
		// Waitlist.
		$email_options[] = 'woocommerce_xts_waitlist_confirm_subscription_email_settings';
		$email_options[] = 'woocommerce_xts_waitlist_in_stock_settings';
		$email_options[] = 'woocommerce_xts_waitlist_subscribe_email_settings';
		// Abandoned cart.
		$email_options[] = 'woocommerce_xts_abandoned_cart_email_settings';

		return $email_options;
	}

	add_filter( 'wcml_emails_options_to_translate', 'xts_add_wcml_emails_options_to_translate' );
}

if ( ! function_exists( 'xts_change_wcml_emails_section_name_prefix' ) ) {
	/**
	 * Remove wcml emails section name prefix for emails.
	 *
	 * @param string $prefix Wcml emails section name prefix.
	 * @param string $email_option Email options name.
	 *
	 * @return string
	 */
	function xts_change_wcml_emails_section_name_prefix( $prefix, $email_option ) {
		$waitlist_options = array(
			// Waitlist.
			'woocommerce_xts_waitlist_confirm_subscription_email_settings',
			'woocommerce_xts_waitlist_in_stock_settings',
			'woocommerce_xts_waitlist_subscribe_email_settings',
			// Abandoned cart.
			'woocommerce_xts_abandoned_cart_email_settings',
		);

		if ( in_array( $email_option, $waitlist_options, true ) ) {
			return '';
		}

		return $prefix;
	}

	add_filter( 'wcml_emails_section_name_prefix', 'xts_change_wcml_emails_section_name_prefix', 10, 2 );
}

if ( ! function_exists( 'xts_add_wcml_emails_text_keys_to_translate' ) ) {
	/**
	 * Add custom emals options keys to translate.
	 *
	 * @param array $text_keys Option name.
	 *
	 * @return array
	 */
	function xts_add_wcml_emails_text_keys_to_translate( $text_keys ) {
		$text_keys[] = 'content_html';
		$text_keys[] = 'content_text';

		return $text_keys;
	}

	add_filter( 'wcml_emails_text_keys_to_translate', 'xts_add_wcml_emails_text_keys_to_translate' );
}
