<?php
/*
 * Software Name: Eorisis Pixels
 * Software URI: https://eorisis.com/shop/software/joomla/pixels
 * Author: Eorisis
 * Author URI: https://eorisis.com
 * Copyright: Copyright (C) 2011-2025 EORISIS. All Rights Reserved.
 * License: GNU General Public License version 2; See https://eorisis.com/gpl
 * Terms of Service: https://eorisis.com/tos
 */
namespace com_eorisis_pixels\framework\lib\core\cms;
\defined('_JEXEC') or die;

use version_compare, array_replace;
use JLoader, EorisisFactory, EorisisLanguageHelper, EorisisText;
use com_eorisis_pixels\framework\lib\core\model\clean;

if (version_compare(JVERSION, '3.8', '>='))
{
	JLoader::registerAlias('EorisisText', '\\Joomla\\CMS\\Language\\Text');
	JLoader::registerAlias('EorisisFactory', '\\Joomla\\CMS\\Factory');
	JLoader::registerAlias('EorisisLanguageHelper', '\\Joomla\\CMS\\Language\\LanguageHelper');
}
else
{
	JLoader::registerAlias('EorisisText', 'JText');
	JLoader::registerAlias('EorisisFactory', 'JFactory');
	JLoader::registerAlias('EorisisLanguageHelper', 'JLanguageHelper');
}

class language
{
	public function __construct() {}

	// --------------------------------------------------

	public static function obj()
	{
		// Never make it static,
		// it changes on language switch and messes things up

		if (version_compare(JVERSION, 4, '>='))
		{
			return EorisisFactory::getApplication()->getLanguage();
		}

		return EorisisFactory::getLanguage();
	}

	// --------------------------------------------------

	public static function load(string $element, array $args = [])
	{
		$args = array_replace([
			'path' => JPATH_ADMINISTRATOR,
			'tag' => 'en-GB',
			'reload' => true
		], $args);

		$language = self::obj();
		$language->load($element, $args['path'], $args['tag'], $args['reload']);
		$language->load($element, $args['path'], null, $args['reload']);
	}

	// --------------------------------------------------

	public static function default_tag()
	{
		return self::obj()->getDefault();
	}

	// --------------------------------------------------

	public static function current_tag()
	{
		$val = self::obj()->getTag();
		return $val ? $val : self::default_tag();
	}

	// --------------------------------------------------

	public static function current_sef() : string
	{
		return clean::uri(self::content_language('sef'));
	}

	// --------------------------------------------------

	public static function content_language($val) : string
	{
		$languages = self::languages();
		$current_tag = self::current_tag();

		// Check for cases where Joomla languages are misconfigured
		// (i.e, language installed, no content language created,
		// and default language set to a non created content language)

		if (isset($languages[$current_tag]))
		{
			$tag = $current_tag;
		}
		else
		{
			$tag = self::default_tag();
		}

		return (string)$languages[$tag]->{$val};
	}

	// --------------------------------------------------

	public static function languages(string $key = 'lang_code')
	{
		return EorisisLanguageHelper::getLanguages($key);
	}

	// --------------------------------------------------

	public static function is_rtl()
	{
		return self::obj()->isRtl();
	}

	// --------------------------------------------------

	public static function text(string $key)
	{
		return EorisisText::_($key);
	}

	// --------------------------------------------------

	public static function sprintf(string $string, $arg1 = null, $arg2 = null)
	{
		return EorisisText::sprintf($string, $arg1, $arg2);
	}
}
