<?php
/**
 * WooCommerce coupon editor translations for ZERTI admin locales.
 */

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

/**
 * Map WordPress locale to wc-coupon-i18n-strings.php locale keys.
 */
function zerti_wc_coupon_i18n_locale_key(?string $locale = null): string
{
    $locale = $locale ?? (function_exists('zerti_get_effective_locale') ? zerti_get_effective_locale() : determine_locale());

    return $locale === 'fa_IR' ? 'fa' : $locale;
}

/**
 * @return array<string, array<string, string>>
 */
function zerti_wc_coupon_i18n_string_tables(): array
{
    static $tables = null;
    if ($tables !== null) {
        return $tables;
    }

    $labels = __DIR__ . '/wc-coupon-i18n-strings.php';
    $descriptions = __DIR__ . '/wc-coupon-i18n-descriptions.php';

    $tables = [];
    foreach ([$labels, $descriptions] as $path) {
        if (!is_readable($path)) {
            continue;
        }
        /** @var array<string, array<string, string>> $chunk */
        $chunk = require $path;
        if (is_array($chunk)) {
            $tables = array_merge($tables, $chunk);
        }
    }

    return $tables;
}

/**
 * English msgid => translated string for the active ZERTI admin locale.
 *
 * @return array<string, string>
 */
function zerti_get_wc_coupon_i18n_map(?string $locale = null): array
{
    static $cache = [];

    $locale_key = zerti_wc_coupon_i18n_locale_key($locale);
    if (isset($cache[$locale_key])) {
        return $cache[$locale_key];
    }

    $map = [];
    foreach (zerti_wc_coupon_i18n_string_tables() as $english => $per_locale) {
        if (!is_array($per_locale)) {
            continue;
        }
        $english = trim($english);
        $translated = trim((string) ($per_locale[$locale_key] ?? ''));
        if ($english !== '' && $translated !== '' && $translated !== $english) {
            $map[$english] = $translated;
        }
    }

    if (function_exists('zerti_get_locale_override_map')) {
        foreach (zerti_get_locale_override_map() as $english => $translated) {
            if (!isset($map[$english]) && $translated !== '' && $translated !== $english) {
                $map[$english] = $translated;
            }
        }
    }

    if (isset($map['Search for a product&hellip;'])) {
        $map['Search for a product…'] = $map['Search for a product&hellip;'];
    }
    if (isset($map['Searching&hellip;'])) {
        $map['Searching…'] = $map['Searching&hellip;'];
    }
    if (isset($map['Loading more results&hellip;'])) {
        $map['Loading more results…'] = $map['Loading more results&hellip;'];
    }

    $cache[$locale_key] = $map;

    return $map;
}

/**
 * @return array<string, array<string, string>>
 */
function zerti_get_wc_coupon_i18n_context_tables(): array
{
    static $tables = null;
    if ($tables !== null) {
        return $tables;
    }

    $path = __DIR__ . '/wc-coupon-i18n-select-context.php';
    if (!is_readable($path)) {
        $tables = [];

        return $tables;
    }

    /** @var array<string, array<string, array<string, string>>> $tables */
    $tables = require $path;

    return is_array($tables) ? $tables : [];
}

/**
 * Translate a WooCommerce string with context for the coupon editor.
 */
function zerti_get_wc_coupon_i18n_context_translation(string $context, string $text, ?string $locale = null): ?string
{
    $context = trim($context);
    $text = trim($text);
    if ($context === '' || $text === '') {
        return null;
    }

    $locale_key = zerti_wc_coupon_i18n_locale_key($locale);
    $tables = zerti_get_wc_coupon_i18n_context_tables();
    if (!isset($tables[$context][$text][$locale_key])) {
        return null;
    }

    $translated = trim((string) $tables[$context][$text][$locale_key]);

    return ($translated !== '' && $translated !== $text) ? $translated : null;
}

/**
 * Params for wc-enhanced-select on the coupon editor screen.
 *
 * @return array<string, mixed>
 */
function zerti_wc_coupon_enhanced_select_params(): array
{
    return [
        'i18n_no_matches' => _x('No matches found', 'enhanced select', 'zerti'),
        'i18n_ajax_error' => _x('Loading failed', 'enhanced select', 'zerti'),
        'i18n_input_too_short_1' => _x('Please enter 1 or more characters', 'enhanced select', 'zerti'),
        /* translators: %qty%: minimum number of characters */
        'i18n_input_too_short_n' => _x('Please enter %qty% or more characters', 'enhanced select', 'zerti'),
        'i18n_input_too_long_1' => _x('Please delete 1 character', 'enhanced select', 'zerti'),
        /* translators: %qty%: number of characters to delete */
        'i18n_input_too_long_n' => _x('Please delete %qty% characters', 'enhanced select', 'zerti'),
        'i18n_selection_too_long_1' => _x('You can only select 1 item', 'enhanced select', 'zerti'),
        /* translators: %qty%: maximum number of selectable items */
        'i18n_selection_too_long_n' => _x('You can only select %qty% items', 'enhanced select', 'zerti'),
        'i18n_load_more' => _x('Loading more results&hellip;', 'enhanced select', 'zerti'),
        'i18n_searching' => _x('Searching&hellip;', 'enhanced select', 'zerti'),
        'ajax_url' => admin_url('admin-ajax.php'),
        'search_products_nonce' => wp_create_nonce('search-products'),
        'search_customers_nonce' => wp_create_nonce('search-customers'),
        'search_categories_nonce' => wp_create_nonce('search-categories'),
        'search_taxonomy_terms_nonce' => wp_create_nonce('search-taxonomy-terms'),
        'search_product_attributes_nonce' => wp_create_nonce('search-product-attributes'),
        'search_pages_nonce' => wp_create_nonce('search-pages'),
        'search_order_metakeys_nonce' => wp_create_nonce('search-order-metakeys'),
    ];
}
