<?php

namespace Sibs2;

class sibsFunctions
{
    public function __construct()
    {
        // code...
    }

     public static function sibs_name_format($name)
     {
         $name_lower = strtolower($name);

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

    public static function sibs_underscore_name_format($name)
    {
        $name_lower = strtolower($name);

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

  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, '.', '');
  }

  public static function sibs_get_plugin_url()
  {
      return untrailingslashit(plugins_url('/', SIBS_PLUGIN_FILE));
  }

  public static function sibs_get_shop_language()
  {
      return strtolower(substr(get_bloginfo('language'), 0, 2));
  }

    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, sibsConstants::SIBS_ACCEPTED_COUNTRIES)) {
            return true;
        }

        return false;
    }

  public static function get_language()
  {
      $site_language = self::sibs_get_shop_language();

      if (in_array($site_language, sibsConstants::SIBS_ACCEPTED_LANGUAGES)) {
          return $site_language;
      }

      return sibsConstants::SIBS_DEFAULT_LANGUAGE;
  }

    public static function get_environment()
    {
        try {
            $json_file = file_get_contents(plugin_dir_path(__DIR__) . '/environment.json');
        } catch (Exception $e) {
            wc_get_logger()->error("Caught exception: {$e->getMessage()}", sibsConstants::SIBS_LOG);
        }

        $environment_file = json_decode($json_file, true);

        return $environment_file;
    }

  public static function __2($string, $textdomain, $locale)
  {
      global $l10n;

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

      $mo_file = plugin_dir_path(__DIR__) . sibsConstants::SIBS_TRANSLATE . '/languages/' . sibsConstants::SIBS_TRANSLATE . "-{$locale}.mo";
      load_textdomain($textdomain, $mo_file, $locale);
      $translation = __($string, $textdomain);

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

      return $translation;
  }

    public static function _e2($string, $textdomain, $locale)
    {
        echo self::__2($string, $textdomain, $locale);
    }
}
