<?php

declare(strict_types=1);

namespace kodorvan\neurobot\models\functions\unzanzara;

/**
 * Unzanzara images
 *
 * Convert images from Telegram by Zanzara 
 *
 * from `$context->getMessage()->getPhoto()`
 * to 
 * [
 *   [
 *     'identifier' => 
 *		 [
 *       [small], 
 *       [medium], 
 *       [big]
 *       ...
 *     ]
 *   ]
 *   ...
 *]
 *
 * @param array $images Images from `$context->getMessage()->getPhoto()`
 *
 * @return array Images grouped by identifier and size
 *
 * @package kodorvan\neurobot\models\functions\unzanzara
 *
 * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
 * @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
 */
function images(array $images): array
{
	// Declaring the array of converted images
	$converted = [];

	foreach ($images as $image) {
		// Iterating over images

		// Initializing the image in the array of converted images
		$converted[$image->getFileId()] ??= [];

		// Initializing the image size in the array of converted images
		$converted[$image->getFileId()][$image->getFileUniqueId()] = [
			'width' => $image->getWidth(),
			'height' => $image->getHeight(),
			'size' => $image->getFileSize()
		];
	}

	foreach ($converted as &$image) {
		// Iterating over converted images

		// Sorting image sized by width
		$image = usort(
			$image,
			fn(array $a, array $b) => match (true) {
				$a['width'] < $b['width'] => -1,
				$a['width'] > $b['width'] => 1,
				default => 0
			}
		);
	}

	// Exit (success)
	return $converted;
}
