<?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, in_array;
use EorisisFactory, EorisisHTMLHelper, JLoader, JResponse;
use com_eorisis_pixels\framework\lib\core\cms\application;

if (version_compare(JVERSION, '3.8', '>='))
{
	JLoader::registerAlias('EorisisFactory', '\\Joomla\\CMS\\Factory');
	JLoader::registerAlias('EorisisHTMLHelper', '\\Joomla\\CMS\\HTML\\HTMLHelper');
}
else
{
	JLoader::registerAlias('EorisisFactory', 'JFactory');
	JLoader::registerAlias('EorisisHTMLHelper', 'JHtml');
}

class document
{
	public function __construct() {}

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

	public static function get()
	{
//		if (version_compare(JVERSION, 4, '>='))
//		{
//			return Factory::getApplication()->getDocument();
//		}

		return EorisisFactory::getDocument();
	}

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

	public static function type()
	{
		static $val = null;
		if ($val === null)
		{
			$val = self::get()->getType();
		}

		return $val;
	}

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

	public static function is_html() : bool
	{
		return self::type() == 'html';
	}

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

	public static function is_html5() : bool
	{
		static $val = null;
		if ($val === null)
		{
			if (version_compare(JVERSION, 3, '>=') and self::is_html())
			{
				if (application::is_backend() and in_array(application::get_template(), ['atum','isis','hathor']))
				{
					self::get()->setHtml5(true);
					$val = true;
				}
				else
				{
					$val = (bool)self::get()->isHtml5();
				}
			}
			else
			{
				$val = false;
			}
		}

		return $val;
	}

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

	public static function media_version() : string
	{
		if (version_compare(JVERSION, '3.2', '<'))
		{
			return '';
		}

		return (string)self::get()->getMediaVersion();
	}

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

	public static function html_display(string $option)
	{
		return EorisisHTMLHelper::_($option);
	}

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

	public static function html(string $option, $value, $txt)
	{
		// $txt: can be string, array
		return EorisisHTMLHelper::_($option, $value, $txt);
	}

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

	public static function set_page_title(string $new_title) // void
	{
        self::get()->setTitle($new_title);
	}

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

	public static function get_body()
	{
		if (version_compare(JVERSION, '3.2', '>='))
		{
			return application::get()->getBody();
		}

		return JResponse::getBody();
	}

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

	public static function set_body($html)
	{
		if (version_compare(JVERSION, '3.2', '>='))
		{
			application::get()->setBody($html);
			return;
		}

		JResponse::setBody($html);
	}
}
