<?php
/*
 * Software Name: Eorisis Setup
 * Software URI: https://eorisis.com/shop/software/joomla/setup
 * 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 eorisis_setup\framework\lib\core\model;
\defined('_JEXEC') or die;

use eorisis_setup\framework\lib\core\app\config as app_config;
use eorisis_setup\framework\lib\core\cms\language as cms_language;
use eorisis_setup\framework\lib\core\model\filesystem;

class language
{
	const DEFAULT_TAG = 'en-GB';
	private static $suffixes = [];
	private static $loaded = [];

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

	public function __construct() {}

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

	public static function load(string $name) // void
	{
		$base_path = JPATH_PLUGINS.'/installer/'.app_config::INSTALLER_PLUGIN_NAME.'/framework/language';

		// Load the default first
		$ini_path = $base_path.'/'.self::DEFAULT_TAG.'/'.$name.'.ini';
		$arr = filesystem::ini_to_array($ini_path);
		if ($arr)
		{
			self::add_array($arr);
		}

		$current_tag = cms_language::current_tag();
		if (self::DEFAULT_TAG == $current_tag)
		{
			return;
		}

		// Load additional language

		$ini_path = $base_path.'/'.$current_tag.'/'.$name.'.ini';
		$arr = filesystem::ini_to_array($ini_path);
		if ($arr)
		{
			self::add_array($arr);
		}
	}

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

	public static function jload(string $suffix, string $tag = 'en-GB')
	{
		$item = $tag.'.'.$suffix;
		if (\in_array($item, self::$suffixes))
		{
			return;
		}

		self::$suffixes[] = $item;
		cms_language::load(app_config::INSTALLER_PLUGIN_ELEMENT.'_'.$suffix, [
			'path' => JPATH_PLUGINS.'/installer/'.app_config::INSTALLER_PLUGIN_NAME.'/framework',
			'tag' => $tag
		]);
	}

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

	public static function text(string $key)
	{
		return isset(self::$loaded[$key]) ? self::$loaded[$key] : '';
	}

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

	private static function add_array(array $arr)
	{
		self::$loaded = \array_merge(self::$loaded, $arr);
	}
}
