<?php

namespace Clonable\Models;

use Clonable\Helpers\Locales;

class MultiCurrency {

	const PAGE = "multi-currency";

	const CURCY_SUPPORT_SETTING = "clonable_enable_curcy_plugin_support";
	const AELIA_SUPPORT_SETTING = "clonable_aelia_currency_support";

	const MULTI_CURRENCY_BY_CLONE = "clonable_multi_currency_by_clone";

	const MULTI_CURRENCY_FORCE_PER_CLONE = "clonable_multi_currency_force_per_clone";

	/**
	 * The name and value for the multi-currency by clone field
	 * @deprecated this value has been renamed to MULTI_CURRENCY_BY_CLONE
	 */
	const _OLD_CURCY_CURRENCY_OVERRIDES = "clonable_curcy_currency_override";

	public static $fields = [
		self::CURCY_SUPPORT_SETTING => [
			"render" => "clonable_enable_curcy_plugin_support_field",
			"name" => "Enable CURCY support",
			"setting" => self::CURCY_SUPPORT_SETTING,
		],
		self::AELIA_SUPPORT_SETTING => [
			"render" => "clonable_enable_aelia_plugin_support_field",
			"name" => "Enable Aelia support",
			"setting" => self::AELIA_SUPPORT_SETTING,
		],
		self::MULTI_CURRENCY_FORCE_PER_CLONE => [
			"render" => "clonable_currency_force_per_clone",
			"name" => "Force currency per clone",
			"setting" => self::MULTI_CURRENCY_FORCE_PER_CLONE,
			"description" => "When checked, this will force the multi currency that you have chosen on the clone. Even if the user has selected another one.",
			"default" => 'on' // the default value for the field
		],
		self::MULTI_CURRENCY_BY_CLONE => [
			"render" => "clonable_currency_per_country_field",
			"name" => "Currency per clone",
			"setting" => self::MULTI_CURRENCY_BY_CLONE,
			"description" => "Override the default currencies that are assigned to a Clonable language."
		],
	];

	/**
	 * Checks for backwards compatibility based on the provided field name.
	 *
	 * @param string $field_name The field name to check for backwards compatibility.
	 *
	 * @return string Returns the old currency override constant if applicable, otherwise returns the provided field name.
	 */
	public static function backwards_compatibility($field_name): string {
		if (get_option(self::MULTI_CURRENCY_BY_CLONE) !== false) {
			return $field_name; // once this value is set, we can ignore the other one
		}

		if ($field_name == self::MULTI_CURRENCY_BY_CLONE) {
			$old_value = get_option(self::_OLD_CURCY_CURRENCY_OVERRIDES);
			if ($old_value !== false) {
				return self::_OLD_CURCY_CURRENCY_OVERRIDES;
			}
		}
		return $field_name;
	}

	public static function get_currency_name(string $code) {
		$currencies = get_woocommerce_currencies();
		return $currencies[$code] ?? $code;
	}

	/**
	 * Get the default values that are used in the MultiCurrency module.
	 * These by default map the target region of a clone to the local currency.
	 *
	 * @return array
	 */
	public static function get_default_currencies(): array {
		$response = get_option("clonable_site");
		if (empty($response)) {
			return array();
		}

		$site         = new Site($response);
		$currency_map = [];
		foreach ($site->get_clones() as $clone) {
			$split_locale   = explode("_" ,$clone['lang_code']);
			$currency_map[] = [
				'id'             => $clone['id'] ,
				'target_region'  => mb_strtolower(end($split_locale)) ,
				'display_region' => 'Clone: ' . Locales::get_display_region($clone['lang_code']) ,
				'currency'       => \Clonable\Helpers\MultiCurrency::get_currency_by_region(end($split_locale)) ,
			];
		}

		return $currency_map;
	}
}