<?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 component
{
	public function __construct() {}

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

	public static function get_update_data(string $element) : array
	{
		if (\strpos($element, 'com_') === 0)
		{
			$extension = \substr($element, 4);
		}
		else
		{
			$extension = $element;
		}

		$dbo = \eorisis_setup\framework\lib\core\cms\application::dbo();
		$query = $dbo->getQuery(true);
		$query->select($dbo->quoteName('params'));
		$query->from($dbo->quoteName('#__'.$extension.'_sections'));
		$query->where($dbo->quoteName('name').'='.$dbo->quote('updates'));
		$dbo->setQuery($query);
		$result = \eorisis_setup\framework\lib\core\model\database::get_first_result($dbo);

		// Not displaying errors here because some installations may still use the old table
		if ($result['error'] or !isset($result['record']))
		{
			return [];
		}

		$item = (array)\json_decode((string)$result['record'], true);
		$item['auth_code'] = clean::auth_code(params::get('auth_code', $item));
		$item['edition'] = params::valid('edition', $item, ['free','pro'], '');

		return $item;
	}
}
