<?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\model\params;
use eorisis_setup\framework\lib\core\model\clean;

class auth
{
	const UPDATE_DATA = [
		'auth_code' => '',
		'edition' => ''
	];

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

	public function __construct() {}

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

	public static function get_update_data(string $type, string $element, string $folder) : array
	{
		// Temporary before everything gets moved to the Setup section
		if ($type == 'package')
		{
			$type = 'component';
			$element = \str_replace('pkg_', 'com_', $element);
		}

		switch ($type)
		{
			case 'component':
				return self::component_update_data($element);

			case 'template':
			case 'module':
				$update_data = self::UPDATE_DATA;
				$update_data['auth_code'] = (string)self::tmpl_mod_auth_code($type, $element);
				return $update_data;

			case 'plugin':
				$update_data = self::UPDATE_DATA;
				$update_data['auth_code'] = (string)self::plugin_auth_code($element, $folder);
				return $update_data;
		}

		return $update_data;
	}

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

	private static function component_update_data(string $element) : array
	{
		if (!\is_dir(JPATH_ADMINISTRATOR.'/components/'.$element))
		{
			return self::UPDATE_DATA;
		}

		$update_data = \eorisis_setup\framework\lib\core\model\component::get_update_data($element);
		if ($update_data)
		{
			return $update_data;
		}

		// Fallback to the old table
		$component_params = \eorisis_setup\framework\lib\core\cms\component::get_params($element);
		$update_data = self::UPDATE_DATA;
		$update_data['auth_code'] = clean::auth_code(params::get('auth_token', $component_params));
		return $update_data;
	}

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

	private static function tmpl_mod_auth_code(string $type, string $element) : string
	{
		$items = \eorisis_setup\framework\lib\core\model\extension::get_items($type, $element, ['params']);
		if (!$items)
		{
			return '';
		}

		foreach ($items as $item)
		{
			if (empty($item['params']))
			{
				continue;
			}

			$item_params = (array)\json_decode((string)$item['params'], true);
			$token = clean::auth_code(params::get('auth_token', $item_params));
			if ($token)
			{
				return $token;
			}
		}

		return '';
	}

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

	private static function plugin_auth_code(string $element, string $folder) : string
	{
		$plugin_params = \eorisis_setup\framework\lib\core\cms\plugin::get_params($element, $folder);

		// If plugin is enabled
		if ($plugin_params)
		{
			return clean::auth_code(params::get('auth_token', $plugin_params));
		}

		// If plugin is disabled
		return \eorisis_setup\framework\lib\core\model\plugin::get_authcode($element, $folder);
	}
}
