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

use com_eorisis_pixels\framework\lib\core\cms\uri as cms_uri;
use com_eorisis_pixels\framework\lib\core\model\params;
use com_eorisis_pixels\framework\lib\core\model\xml;

class config
{
	const EXTENSION = 'eorisis_pixels';
	const COMPONENT_ELEMENT = 'com_eorisis_pixels';
	const SYSTEM_PLUGIN_NAME = 'eorisis_pixels';
	const DEFAULT_VIEW = 'dashboard';
	const TABLE_METAPIXELS = 'eorisis_pixels_metapixels';
	const TABLE_SECTIONS = 'eorisis_pixels_sections';
	const TABLE_TRAFFICFILTERS = 'eorisis_pixels_trafficfilters';

	const LIST_VIEWS = [
		'dashboard',
		'metapixels',
		'trafficfilters'
	];

	const EDIT_VIEWS = [
		'pref',
		'ui',
		'updates',
		'metapixels',
		'metapixelopt',
		'trafficfilter',
		'trafficfilteropt'
	];

	private static $xml;

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

	public function __construct() {}

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

	public static function init() // void
	{
		static $done = false;
		if ($done)
		{
			return;
		}

		$path = JPATH_ADMINISTRATOR.'/components/'.self::COMPONENT_ELEMENT.'/'.self::EXTENSION.'.xml';
		self::$xml = xml::element_from_file($path, false);
		$done = true;
	}

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

	public static function get(string $key, $default = '')
	{
		return params::xml_attr($key, self::$xml, $default);
	}

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

	public static function media_destination() : string
	{
		return (string)self::$xml->media['destination'];
	}

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

	public static function media_uri_relative(bool $jversion_check = false) : string
	{
		// Joomla >= 4 rejects subdirectory in the url in media manager when the site is installed in a subdir
		if ($jversion_check and \version_compare(JVERSION, 4, '>='))
		{
			$uri = '/';
		}
		else
		{
			$uri = cms_uri::webroot_relative();
		}

		return $uri.'media/'.self::$xml->media['destination'];
	}

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

    public static function current_table(string $extension) : string
    {
		switch ($extension)
		{
			case self::COMPONENT_ELEMENT.'.metapixels':
				return '#__'.self::TABLE_METAPIXELS;

			case self::COMPONENT_ELEMENT.'.trafficfilters':
				return '#__'.self::TABLE_TRAFFICFILTERS;
		}

        return '';
    }
}
