<?php
/**
 * WPML Support class.
 *
 * This class handles the integration with WPML for the plugin.
 *
 * @package SaveToWishlist\Classes\Integrations
 * @since   1.0.2
 */
namespace SaveToWishlist\Classes\Integrations;

use SaveToWishlist\Abstracts\Singleton;
use SaveToWishlist\Helpers\Helper_Functions;

/**
 * Class WPML_Support
 *
 * This class handles the integration with WPML for the plugin.
 */
class WPML_Support extends Singleton {
    /**
     * Helper functions instance.
     *
     * @var Helper_Functions
     */
    private $_helper_functions;

    /**
     * Constructor.
     *
     * @since 1.0.2
     * @access public
     */
    public function __construct() {
        $this->_helper_functions = Helper_Functions::instance();
    }

    /**
     * Execute WPML support.
     *
     * @since 1.0.2
     * @access public
     */
    public function run() {
        // Check if WPML is already loaded (it fires on plugins_loaded priority 1,
        // but this plugin boots on setup_theme priority 100).
        if ( did_action( 'wpml_loaded' ) ) {
            // WPML is already loaded, but delay initialization until WordPress is ready
            // to avoid textdomain loading issues.
            add_action( 'init', array( $this, 'wpml_loaded' ), 20 );
        } else {
            // Add hooks when WPML plugin is loaded (fallback for edge cases).
            add_action( 'wpml_loaded', array( $this, 'wpml_loaded' ), 110 );
        }

        // Apply translation to setting strings when the option is updated.
        add_action( 'update_option_stwlite_settings_wishlist_button_label', array( $this, 'register_translatable_strings' ) );
        add_action( 'update_option_stwlite_settings_wishlist_button_cart_label', array( $this, 'register_translatable_strings' ) );
        add_action( 'update_option_stwlite_settings_wishlist_title', array( $this, 'register_translatable_strings' ) );
        add_action( 'update_option_stwlite_settings_wishlist_description', array( $this, 'register_translatable_strings' ) );
    }

    /**
     * Check if the WPML requirements are installed.
     *
     * @since 1.0.2
     * @access private
     * @return bool
     */
    private function _is_wpml_requirements_installed() {
        return $this->_helper_functions->is_wp_plugin_installed( plugin_basename( 'sitepress-multilingual-cms/sitepress.php' ) )
        && $this->_helper_functions->is_wp_plugin_installed( plugin_basename( 'woocommerce-multilingual/wpml-woocommerce.php' ) )
        && \function_exists( 'icl_st_init' ) // string translation plugin.
        && \function_exists( 'wpml_tm_load_element_translations' ); // translation management plugin.
    }

    /**
     * Apply translation to setting strings.
     *
     * @since 1.0.2
     * @access public
     */
    public function wpml_loaded() {
        // Double-check WPML is actually available before proceeding.
        if ( ! $this->_is_wpml_requirements_installed() ) {
            return;
        }

        // Ensure WPML string translation functions are available.
        if ( ! function_exists( 'wpml_register_string' ) && ! has_action( 'wpml_register_string' ) ) {
            return;
        }

        // Register translatable strings.
        $this->register_translatable_strings();

        // Apply translation to setting strings.
        add_filter( 'stwlite_string_option', array( $this, 'apply_translation_setting_strings' ), 10, 2 );
    }

    /**
     * Register translatable strings.
     *
     * @since 1.0.2
     * @access public
     */
    public function register_translatable_strings() {
        if ( ! $this->_is_wpml_requirements_installed() ) {
            return;
        }

        // Register translatable strings.
        $package = array(
            'kind'  => 'STWLite Settings',
            'name'  => 'stwlite_settings',
            'title' => 'STWLite Settings',
        );

        $translatable_strings = apply_filters(
            'stwlite_wpml_translate_setting_options',
            array(
                array(
                    'value' => get_option( 'stwlite_settings_wishlist_button_label' ),
                    'name'  => 'stwlite_settings_wishlist_button_label',
                    'label' => __( 'Wishlist Button Label', 'saveto-wishlist-lite-for-woocommerce' ),
                    'type'  => 'LINE',
                ),
                array(
                    'value' => get_option( 'stwlite_settings_wishlist_button_cart_label' ),
                    'name'  => 'stwlite_settings_wishlist_button_cart_label',
                    'label' => __( 'Wishlist Button Cart Label', 'saveto-wishlist-lite-for-woocommerce' ),
                    'type'  => 'LINE',
                ),
                array(
                    'value' => get_option( 'stwlite_settings_wishlist_title' ),
                    'name'  => 'stwlite_settings_wishlist_title',
                    'label' => __( 'Wishlist Page Title', 'saveto-wishlist-lite-for-woocommerce' ),
                    'type'  => 'LINE',
                ),
                array(
                    'value' => get_option( 'stwlite_settings_wishlist_description' ),
                    'name'  => 'stwlite_settings_wishlist_description',
                    'label' => __( 'Wishlist Page Description', 'saveto-wishlist-lite-for-woocommerce' ),
                    'type'  => 'AREA',
                ),
            )
        );

        // register translations for fields.
        foreach ( $translatable_strings as $translatable_string ) {
            if ( $translatable_string['value'] ) {
                do_action( 'wpml_register_string', $translatable_string['value'], $translatable_string['name'], $package, $translatable_string['label'], $translatable_string['type'] );
            }
        }
    }

    /**
     * Apply translation to setting strings.
     *
     * @since 1.0.2
     * @access public
     *
     * @param string $value Setting value.
     * @param string $option Option name.
     * @return string Filtered setting value.
     */
    public function apply_translation_setting_strings( $value, $option ) {
        $package = array(
            'kind'  => 'STWLite Settings',
            'name'  => 'stwlite_settings',
            'title' => 'STWLite Settings',
        );

        return apply_filters( 'wpml_translate_string', $value, $option, $package );
    }
}
