<?php
namespace ARCHM\Admin;

if ( ! defined('ABSPATH') ) {
    // phpcs:ignore Universal.PHP.RequireExitDieParentheses.Missing
    exit;
}

class Translations {
    /**
     * Initialize the class
     */
    public function __construct() {
        add_action('init', [ $this, 'load_textdomain' ]);
        add_action('admin_enqueue_scripts', [ $this, 'localize_scripts' ]);
    }

    /**
     * Load plugin text domain
     * Note: Since WordPress 4.6, translations are automatically loaded for plugins hosted on WordPress.org
     */
    public function load_textdomain() {
        // Load translations from plugin's languages directory
        $domain = 'archive-master';
        $plugin_rel_path = dirname(plugin_basename(ARCHM_PLUGIN_FILE)) . '/languages';

        // Register the textdomain path
        load_plugin_textdomain($domain, false, $plugin_rel_path);

        // Also try loading directly with absolute path (for WordPress 6.7+ compatibility)
        $locale = determine_locale();
        $mofile = ARCHM_PLUGIN_PATH . '/languages/' . $domain . '-' . $locale . '.mo';
        if ( file_exists($mofile) ) {
            load_textdomain($domain, $mofile);
        }
    }

    /**
     * Recursively translate nested arrays
     * This ensures that nested translation arrays are properly translated
     *
     * @param array  $array  Array to translate
     * @param string $domain Text domain
     * @return array Translated array
     */
    private function translate_nested_array( $array, $domain ) {
        foreach ( $array as $key => $value ) {
            if ( is_array($value) ) {
                // Recursively translate nested arrays
                $array[ $key ] = $this->translate_nested_array($value, $domain);
            } elseif ( is_string($value) ) {
                // If it's a string that looks like it should be translated (contains English text),
                // try to translate it. However, since __() was already called in the translations file,
                // this should already be translated. But we'll ensure it's using the current locale.
                // Actually, the value should already be translated by __() when the file was included.
                // So we don't need to do anything here.
                // No-op: String values are already translated when the translations file was included.
                continue;
            }
        }
        return $array;
    }

    /**
     * Localize scripts with translations
     */
    public function localize_scripts() {
        // Only load on our plugin's admin page
        $screen = get_current_screen();
        if ( ! $screen || 'toplevel_page_master-archives' !== $screen->id ) {
            return;
        }

        // Force reload textdomain to ensure translations are available
        $domain = 'archive-master';
        $locale = determine_locale();
        $mofile = ARCHM_PLUGIN_PATH . '/languages/' . $domain . '-' . $locale . '.mo';

        // Unload first to force reload
        unload_textdomain($domain);

        // Load with absolute path
        if ( file_exists($mofile) ) {
            load_textdomain($domain, $mofile);
        } else {
            // Fallback to relative path
            load_plugin_textdomain($domain, false, dirname(plugin_basename(ARCHM_PLUGIN_FILE)) . '/languages');
        }

        // Get translations array - __() will now use the loaded translations
        $translations_file = ARCHM_PLUGIN_PATH . '/languages/archive-master-translations.php';
        $translations = [];
        if ( file_exists($translations_file) ) {
            $translations = include $translations_file;
        }

        // Localize the script
        wp_localize_script(
            'wa-admin-js',
            'archmTranslations',
            $translations
        );
    }
}
