<?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\cms;
\defined('_JEXEC') or die;

use strpos, version_compare, array_replace, is_bool, ltrim;
use eorisis_setup\framework\lib\core\cms\document as cms_document;
use eorisis_setup\framework\lib\core\cms\uri as cms_uri;
use eorisis_setup\framework\lib\core\cms\asset;

class head
{
	public function __construct() {}

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

	public static function add_switcher_media() // void
	{
		static $done = null;
		if ($done)
		{
			return;
		}

		// Joomla >= 4 rejects subdirectory in the url in media manager when the site is installed in a subdir
		$uri = version_compare(JVERSION, 4, '>=') ? '/' : cms_uri::webroot_relative();
		self::add_css_uri([
			'name' => 'switcher.min.css',
			'uri' => $uri.'media/system/css/fields/switcher.min.css',
			'version' => true
		]);
		$done = true;
	}

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

	public static function add_jquery_inline(string $script) // void
	{
		self::add_script_inline('jQuery(function($,undefined){'.$script.'});');
	}

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

	public static function add_script_inline(string $content, array $args = []) // void
	{
		if (version_compare(JVERSION, 4, '>='))
		{
			$asset = new asset;
			$asset->set_inline('script', $content, $args);
			return;
		}

		cms_document::get()->addScriptDeclaration($content);
	}

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

	public static function add_css_inline(string $content, array $args = []) // void
	{
		if (version_compare(JVERSION, 4, '>='))
		{
			$asset = new asset;
			$asset->set_inline('style', $content, $args);
			return;
		}

		cms_document::get()->addStyleDeclaration($content);
	}

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

	public static function add_css_uri(array $args) // void
	{
		$args = array_replace([
			'name' => '',
			'uri' => '',
			'version' => '',
			'media' => 'all'
		], $args);

		if (version_compare(JVERSION, 4, '>='))
		{
			$options['type'] = 'style';
			$options['name'] = $args['name'];

			if (strpos((string)$args['uri'], '//') === 0)
			{
				$options['uri'] = $args['uri'];
			}
			else
			{
				$options['uri'] = ltrim($args['uri'], '/');
			}

			if ($args['version'])
			{
				$options['version'] = is_bool($args['version']) ? 'auto' : $args['version'];
			}

			if ($args['media'])
			{
				$options['attributes']['media'] = $args['media'];
			}

			$asset = new asset;
			$asset->add_uri($options);
			return;
		}

		if (version_compare(JVERSION, '3.7', '>='))
		{
			$options = [];

			if ($args['version'])
			{
				$options['version'] = is_bool($args['version']) ? 'auto' : $args['version'];
			}

			$attribs = $args['media'] ? ['media' => $args['media']] : [];
			cms_document::get()->addStyleSheet($args['uri'], $options, $attribs);
			return;
		}

		$type = 'text/css';

		if ($args['version'] and version_compare(JVERSION, '3.2', '>='))
		{
			$version = is_bool($args['version']) ? cms_document::media_version() : $args['version'];
			cms_document::get()->addStyleSheetVersion($args['uri'], $version, $type, $args['media']);
			return;
		}

		cms_document::get()->addStyleSheet($args['uri'], $type, $args['media']);
	}
}
