<?php
defined('ABSPATH') || exit;

if (! function_exists('reeid_woocommerce_gettext_filter')) {
    function reeid_woocommerce_gettext_filter($translated, $text, $domain)
    {
        if ($domain !== 'woocommerce') {
            return $translated;
        }

        if (! empty($GLOBALS['reeid_in_gettext'])) {
            return $translated;
        }

        $GLOBALS['reeid_in_gettext'] = true;

        $lang = function_exists('reeid_current_lang') ? (string) reeid_current_lang() : '';
        if ($lang === '' || $lang === 'en') {
            unset($GLOBALS['reeid_in_gettext']);
            return $translated;
        }

        if (! function_exists('reeid_wc_get_static_string_map')) {
            unset($GLOBALS['reeid_in_gettext']);
            return $translated;
        }

        $map = reeid_wc_get_static_string_map($lang);
        if (! is_array($map) || empty($map)) {
            unset($GLOBALS['reeid_in_gettext']);
            return $translated;
        }

        $key = trim((string) $text);
        if ($key !== '' && isset($map[$key]) && $map[$key] !== '') {
            unset($GLOBALS['reeid_in_gettext']);
            return (string) $map[$key];
        }

        unset($GLOBALS['reeid_in_gettext']);
        return $translated;
    }

    add_filter('gettext', 'reeid_woocommerce_gettext_filter', 20, 3);
}

if (! function_exists('reeid_woocommerce_gettext_with_context_filter')) {
    function reeid_woocommerce_gettext_with_context_filter($translated, $text, $context, $domain)
    {
        if ($domain !== 'woocommerce') {
            return $translated;
        }

        if (! empty($GLOBALS['reeid_in_gettext'])) {
            return $translated;
        }

        $GLOBALS['reeid_in_gettext'] = true;

        $lang = function_exists('reeid_current_lang') ? (string) reeid_current_lang() : '';
        if ($lang === '' || $lang === 'en') {
            unset($GLOBALS['reeid_in_gettext']);
            return $translated;
        }

        $out = reeid_woocommerce_gettext_filter($translated, $text, $domain);

        unset($GLOBALS['reeid_in_gettext']);
        return $out;
    }

    add_filter('gettext_with_context', 'reeid_woocommerce_gettext_with_context_filter', 20, 4);
}

if (! function_exists('reeid_translate_add_to_cart_button')) {
    function reeid_translate_add_to_cart_button($text)
    {
        if (! empty($GLOBALS['reeid_in_gettext'])) {
            return $text;
        }

        $GLOBALS['reeid_in_gettext'] = true;

        $lang = function_exists('reeid_current_lang') ? (string) reeid_current_lang() : '';
        if ($lang === '' || $lang === 'en') {
            unset($GLOBALS['reeid_in_gettext']);
            return $text;
        }

        if (function_exists('reeid_wc_get_static_string_map')) {
            $map = reeid_wc_get_static_string_map($lang);
            if (is_array($map) && ! empty($map[$text])) {
                unset($GLOBALS['reeid_in_gettext']);
                return (string) $map[$text];
            }
        }

        unset($GLOBALS['reeid_in_gettext']);
        return $text;
    }

    add_filter('woocommerce_product_add_to_cart_text', 'reeid_translate_add_to_cart_button', 20);
    add_filter('woocommerce_product_single_add_to_cart_text', 'reeid_translate_add_to_cart_button', 20);
}