<?php

defined('ABSPATH') || exit;
if (defined('WCML_VERSION')) {
    function rocket_wcml_use_cookie_storage()
    {
        return 'cookie';
    }
    function rocket_wcml_add_dynamic_cookies($cookies)
    {
        $cookies[] = 'wcml_client_currency';
        $cookies[] = 'wcml_client_currency_language';
        $cookies[] = 'wcml_client_country';
        return $cookies;
    }
    add_filter('rocket_cache_dynamic_cookies', 'rocket_wcml_add_dynamic_cookies');
    function rocket_wcml_add_mandatory_cookies($cookies)
    {
        if (apply_filters('wcml_geolocation_is_used', false)) {
            $cookies[] = 'wcml_client_country';
        }
        return $cookies;
    }
    add_filter('rocket_cache_mandatory_cookies', 'rocket_wcml_add_mandatory_cookies');
    function rocket_wcml_reset_settings($option, $old_data, $data)
    {
        $keys_to_check = ['enable_multi_currency', 'currency_mode', 'default_currencies'];
        $check_key = function ($result, $key) use($old_data, $data) {
            $has_value_changed = function ($key) use($old_data, $data) {
                $get_value = function ($key, $data) {
                    return isset($data[$key]) ? $data[$key] : null;
                };
                return $get_value($key, $old_data) !== $get_value($key, $data);
            };
            return $result || $has_value_changed($key);
        };
        if ('_wcml_settings' === $option && array_reduce($keys_to_check, $check_key, false)) {
            flush_rocket_htaccess();
            rocket_generate_config_file();
        }
    }
    add_action('updated_option', 'rocket_wcml_reset_settings', 10, 3);
    function rocket_wcml_activate()
    {
        add_filter('rocket_htaccess_mod_rewrite', '__return_false', 64);
        add_filter('rocket_cache_dynamic_cookies', 'rocket_wcml_add_dynamic_cookies');
        add_filter('rocket_cache_mandatory_cookies', 'rocket_wcml_add_mandatory_cookies');
        flush_rocket_htaccess();
        rocket_generate_config_file();
    }
    add_action('woocommerce-multilingual/wpml-woocommerce.php', 'rocket_wcml_activate', 11);
    function rocket_wcml_deactivate()
    {
        remove_filter('rocket_htaccess_mod_rewrite', '__return_false', 64);
        remove_filter('rocket_cache_dynamic_cookies', 'rocket_wcml_add_dynamic_cookies');
        remove_filter('rocket_cache_mandatory_cookies', 'rocket_wcml_add_mandatory_cookies');
        flush_rocket_htaccess();
        rocket_generate_config_file();
    }
    add_action('woocommerce-multilingual/wpml-woocommerce.php', 'rocket_wcml_deactivate', 11);
    add_filter('rocket_htaccess_mod_rewrite', '__return_false', 64);
}