<?php

declare(strict_types=1);

namespace kodorvan\neurobot\models\telegram\middlewares;

// Files of the project
use kodorvan\neurobot\models\core,
	kodorvan\neurobot\models\account as model,
	kodorvan\neurobot\models\authorizations;

// The library for languages support
use mirzaev\languages\language;

// The library for escaping all markdown symbols
use function mirzaev\unmarkdown;

// Framework for Telegram
use SergiX44\Nutgram\Nutgram as telegram,
	SergiX44\Nutgram\Telegram\Properties\ParseMode as mode;

// Built-in libraries
use Error as error;

/**
 * Telegram middleware: message
 *
 * @package kodorvan\neurobot\models\telegram\middlewares
 *
 * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
 * @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
 */
final class message extends core
{
	/**
	 * Message
	 *
	 * Check the message text is not an any command
	 *
	 * @param telegram $robot
	 * @param $next
	 *
	 * @return void
	 */
	public function __invoke(telegram $robot, $next): void
	{
		// Is the process stopped?
		if ($robot->get('stop')) return;

		// Initializing localization
		$localization = $robot->get('localization') ?? new localization(LANGUAGE_DEFAULT ?? language::en);

		// Initializing the message
		$message = $robot->message();

		// Initializing the message text
		$text = $message?->text ?? $message?->caption;

		// Initializing the robot commands
		$commands = array_map(function ($command) {
			// Exit (success)
			return $command->command;
		}, $robot->getMyCommands() ?? []);

		// Initializing the robot expressions
		$expressions = [
			'✂️ ' . $localization['chat_new']
		];

		if (
			!array_search("/$text", $commands, strict: true)
			&& !array_search($text, $expressions, strict: true)
		) {
			// The message text is not any command or special expression

			// Continuation of the process
			$next($robot);
		}
	}
}
