<?php
/**
 * Configuration accessors for BW 2Checkout Pricing.
 *
 * All settings live in a single option array. The Secret Key additionally supports a
 * wp-config constant so it can be kept out of the database on hardened sites.
 *
 * @package BW_2Checkout_Pricing
 */

defined( 'ABSPATH' ) || exit;

/**
 * Default settings. Copernic-specific values are entered in the admin screen, not hardcoded.
 *
 * @return array<string,mixed>
 */
function bw_2checkout_pricing_default_settings() {
	return array(
		'merchant_code'     => '',
		'secret_key'        => '',
		'api_base'          => 'https://api.2checkout.com/rest/6.0/',
		'checkout_base'     => 'https://secure.2checkout.com/checkout/buy',
		'checkout_merchant' => '',
		'checkout_tpl'      => 'default',
		'test_mode'         => true,
		'cache_ttl'         => 3600,
		'default_currency'  => 'USD',
		'currencies'        => 'USD,EUR,GBP,CAD',
		// 'mock' = illustrative prices (no API needed). 'live' = pull from 2Checkout.
		'pricing_source'    => 'mock',
		// Products config: map of widget products. Empty falls back to the built-in default.
		'products'          => array(),
	);
}

/**
 * Full settings array, merged over defaults.
 *
 * @return array<string,mixed>
 */
function bw_2checkout_pricing_get_settings() {
	$stored = get_option( 'bw_2checkout_pricing_settings', array() );
	if ( ! is_array( $stored ) ) {
		$stored = array();
	}
	return wp_parse_args( $stored, bw_2checkout_pricing_default_settings() );
}

/**
 * Single setting value.
 *
 * @param string $key     Setting key.
 * @param mixed  $default Fallback if unset.
 * @return mixed
 */
function bw_2checkout_pricing_get_setting( $key, $default = null ) {
	$settings = bw_2checkout_pricing_get_settings();
	return array_key_exists( $key, $settings ) ? $settings[ $key ] : $default;
}

/**
 * Merchant code (constant wins over stored setting).
 *
 * @return string
 */
function bw_2checkout_pricing_get_merchant_code() {
	if ( defined( 'BW_2CHECKOUT_PRICING_MERCHANT_CODE' ) && BW_2CHECKOUT_PRICING_MERCHANT_CODE ) {
		return (string) BW_2CHECKOUT_PRICING_MERCHANT_CODE;
	}
	return (string) bw_2checkout_pricing_get_setting( 'merchant_code', '' );
}

/**
 * Secret key (constant wins over stored setting). Never echo this value.
 *
 * @return string
 */
function bw_2checkout_pricing_get_secret_key() {
	if ( defined( 'BW_2CHECKOUT_PRICING_SECRET_KEY' ) && BW_2CHECKOUT_PRICING_SECRET_KEY ) {
		return (string) BW_2CHECKOUT_PRICING_SECRET_KEY;
	}
	return (string) bw_2checkout_pricing_get_setting( 'secret_key', '' );
}

/**
 * Built-in default products config (used when no admin config is set) so the widget
 * works out of the box. Value codes are Copernic's per Phil's buy-link; verify in sandbox.
 *
 * @return array<string,mixed>
 */
function bw_2checkout_pricing_default_products() {
	return array(
		'cds' => array(
			'label'        => 'Copernic Desktop & Cloud Search',
			'product_code' => '3WL7JRHV69',
			'selectors'    => array(
				array(
					'key'    => 'edition',
					'group'  => 'EDITION_2',
					'label'  => 'Edition',
					'type'   => 'select',
					'values' => array(
						array( 'code' => 'basic', 'label' => 'Basic' ),
						array( 'code' => 'advanced', 'label' => 'Advanced' ),
						array( 'code' => 'elite', 'label' => 'Elite' ),
					),
				),
				array(
					'key'    => 'term',
					'group'  => 'TERM',
					'label'  => 'Term',
					'type'   => 'select',
					'values' => array(
						array( 'code' => '1Y', 'label' => '1 Year' ),
						array( 'code' => '2Y', 'label' => '2 Years' ),
						array( 'code' => '3Y', 'label' => '3 Years' ),
					),
				),
				array(
					'key'     => 'users',
					'group'   => 'users',
					'label'   => 'Users',
					'type'    => 'number',
					'min'     => 1,
					'default' => 1,
				),
			),
		),
	);
}

/**
 * Products config: admin-defined if present, otherwise the built-in default.
 *
 * @return array<string,mixed>
 */
function bw_2checkout_pricing_get_products() {
	$products = bw_2checkout_pricing_get_setting( 'products', array() );
	if ( is_array( $products ) && ! empty( $products ) ) {
		return $products;
	}
	return bw_2checkout_pricing_default_products();
}

/**
 * Translate a config-driven, user-facing label (product / selector / option labels).
 *
 * These live in the settings, not in code, so gettext/POT cannot cover them. On a WPML site we
 * register them with WPML String Translation and return the translated value; without WPML the
 * string is returned unchanged. No hard dependency on WPML.
 *
 * @param string $string The source (default-language) label.
 * @param string $name   A stable identifier for this string within the plugin's domain.
 * @return string
 */
function bw_2checkout_pricing_tr( $string, $name = '' ) {
	$string = (string) $string;
	if ( '' === $string ) {
		return $string;
	}
	$domain = 'BW 2Checkout Pricing';
	$name   = '' !== $name ? $name : $string;

	if ( has_filter( 'wpml_translate_single_string' ) ) {
		do_action( 'wpml_register_single_string', $domain, $name, $string );
		return (string) apply_filters( 'wpml_translate_single_string', $string, $domain, $name );
	}
	return $string;
}

/**
 * Currency symbol map for the big-number price display. Falls back to the ISO code.
 *
 * @return array<string,string>
 */
function bw_2checkout_pricing_currency_symbols() {
	return array(
		'USD' => '$',
		'CAD' => '$',
		'AUD' => '$',
		'NZD' => '$',
		'HKD' => '$',
		'SGD' => '$',
		'MXN' => '$',
		'EUR' => '€',
		'GBP' => '£',
		'JPY' => '¥',
		'CNY' => '¥',
		'INR' => '₹',
		'BRL' => 'R$',
		'ZAR' => 'R',
		'CHF' => 'CHF ',
	);
}

/**
 * @param string $currency ISO code.
 * @return string
 */
function bw_2checkout_pricing_currency_symbol( $currency ) {
	$currency = strtoupper( (string) $currency );
	$map      = bw_2checkout_pricing_currency_symbols();
	return isset( $map[ $currency ] ) ? $map[ $currency ] : $currency . ' ';
}

/**
 * Which side of the number the symbol sits on. Inferred from the currency; the suffix set below
 * covers the currencies that conventionally trail the symbol (kr, zł, Kč, lei, …). Ambiguous
 * cases (e.g. EUR, which is locale-dependent) default to the left.
 *
 * @param string $currency ISO code.
 * @return string 'left' | 'right'
 */
function bw_2checkout_pricing_symbol_side( $currency ) {
	$right = array( 'SEK', 'NOK', 'DKK', 'PLN', 'CZK', 'HUF', 'RON', 'RSD', 'RUB', 'UAH', 'BYN', 'MDL', 'VND', 'PYG' );
	return in_array( strtoupper( (string) $currency ), $right, true ) ? 'right' : 'left';
}
