<?php

namespace Sibs\Stargate\Core;

use Sibs\Stargate\Config\SibsConfig;
use Sibs\Stargate\Constants\Languages;

defined('ABSPATH') || exit;

class SibsFunctions
{
    /**
     * Translate.
     *
     * @param string $string
     * @param string $textdomain
     * @param string $locale
     *
     * @return string
     */
    public static function __2($string, $textdomain, $locale)
    {
        global $l10n;

        if (isset($l10n[$textdomain])) {
            $backup = $l10n[$textdomain];
        }

        $mo_file = constant('SIBS_PLUGIN_PATH').'sibs-woo-stg/languages/sibs-woo-stg'."-{$locale}.mo";
        load_textdomain($textdomain, $mo_file, $locale);
        $translation = __($string, $textdomain);

        if (isset($backup)) {
            $l10n[$textdomain] = $backup;
        }

        return $translation;
    }

    /**
     * SIBS Name Format.
     *
     * @param string $name
     *
     * @return string
     */
    public static function sibs_name_format($name)
    {
        $name_lower = strtolower($name);

        return str_replace('_', '-', $name_lower);
    }

    /**
     * SIBS Undercore Name Format.
     *
     * @param string $name
     *
     * @return string
     */
    public static function sibs_underscore_name_format($name)
    {
        $name_lower = strtolower($name);

        return str_replace('-', '_', $name_lower);
    }

    /**
     * SIBS Set Number Format.
     *
     * @param mixed $number
     *
     * @return string
     */
    public static function sibs_set_number_format($number)
    {
        $locale_info = localeconv();
        $decimal_pointer = $locale_info['decimal_point'];

        $float_number = floatval(str_replace($decimal_pointer, '.', $number));

        return number_format($float_number, 2, '.', '');
    }

    /**
     * SIBS Get Plugin URL.
     *
     * @return string
     */
    public static function sibs_get_plugin_url()
    {
        return untrailingslashit(constant('SIBS_PLUGIN_URL'));
    }

    /**
     * SIBS Is Billing Country Accepted.
     *
     * @return bool
     */
    public static function sibs_is_billing_country_accepted()
    {
        $customer = WC()->customer;

        $obj_array = (array) $customer;
        $changes = $obj_array["\0*\0".'changes'];
        $country = $changes['shipping']['country'];

        if (is_null($country)) {
            $country = $customer->country;
        }

        if (in_array($country, Languages::acceptedCountries())) {
            return true;
        }

        return false;
    }

    /**
     * Undocumented function.
     */
    public static function get_language()
    {
        return SibsConfig::getLanguageDefault();
    }

    /**
     * Echo Translate.
     *
     * @param string $string
     * @param string $textdomain
     * @param string $locale
     *
     * @return string
     */
    public static function _e2($string, $textdomain, $locale)
    {
        echo self::__2($string, $textdomain, $locale);
    }
}
