<commit_before><?php declare(strict_types=1);

namespace Room11\Jeeves\BuiltIn\Commands;

use Amp\Promise;
use Room11\Jeeves\Chat\Client\ChatClient;
use Room11\Jeeves\Chat\Client\PendingMessage;
use Room11\Jeeves\Chat\Message\Command as CommandMessage;
use Room11\Jeeves\Storage\Admin as AdminStorage;
use Room11\Jeeves\Storage\CommandAlias as CommandAliasStorage;
use Room11\Jeeves\System\BuiltInActionManager;
use Room11\Jeeves\System\BuiltInCommand;
use Room11\Jeeves\System\BuiltInCommandInfo;
use Room11\Jeeves\System\PluginManager;
use function Amp\resolve;

class Alias implements BuiltInCommand
{
    private $chatClient;
    private $aliasStorage;
    private $adminStorage;
    private $builtInCommandManager;
    private $pluginManager;

    public function __construct(
        ChatClient $chatClient,
        CommandAliasStorage $aliasStorage,
        AdminStorage $adminStorage,
        BuiltInActionManager $builtInCommandManager,
        PluginManager $pluginManager
    ) {
        $this->chatClient = $chatClient;
        $this->aliasStorage = $aliasStorage;
        $this->adminStorage = $adminStorage;
        $this->builtInCommandManager = $builtInCommandManager;
        $this->pluginManager = $pluginManager;
    }

    private function addAlias(CommandMessage $command)
    {
        $room = $command->getRoom();
        $aliasCommand = $command->getParameter(0);
        $mapping = implode(' ', $command->getParameters(1));

        if ($this->builtInCommandManager->hasRegisteredCommand($aliasCommand)) {
            return $this->chatClient->postReply(
                $command,
                new PendingMessage(
                    "Command '{$aliasCommand}' is built in and cannot be altered",
                    $command
                )
            );
        }

        if ($this->pluginManager->isCommandMappedForRoom($room, $aliasCommand)) {
            return $this->chatClient->postReply(
                $command,
                new PendingMessage(
                    "Command '{$aliasCommand}' is already mapped. Use `!!command list` to display the currently mapped commands.",
                    $command
                )
            );
        }

        if (yield $this->aliasStorage->exists($room, $aliasCommand)) {
            return $this->chatClient->postReply(
                $command,
                new PendingMessage("Alias '!!{$aliasCommand}' already exists.", $command)
            );
        }

        yield $this->aliasStorage->add($room, $aliasCommand, $mapping);

        return $this->chatClient->postMessage(
            $room,
            new PendingMessage(
                "Command '!!{$aliasCommand}' aliased to '!!{\$mapping}'",
                $command
            )
        );
    }

    private function removeAlias(CommandMessage $command): \Generator
    {
        $aliasCommand = $command->getParameter(0);

        if (!yield $this->aliasStorage->exists($command->getRoom(), $aliasCommand)) {
            return $this->chatClient->postMessage(<commit_msg>More weirdo quotes<commit_after><?php declare(strict_types=1);

namespace Room11\Jeeves\BuiltIn\Commands;

use Amp\Promise;
use Room11\Jeeves\Chat\Client\ChatClient;
use Room11\Jeeves\Chat\Client\PendingMessage;
use Room11\Jeeves\Chat\Message\Command as CommandMessage;
use Room11\Jeeves\Storage\Admin as AdminStorage;
use Room11\Jeeves\Storage\CommandAlias as CommandAliasStorage;
use Room11\Jeeves\System\BuiltInActionManager;
use Room11\Jeeves\System\BuiltInCommand;
use Room11\Jeeves\System\BuiltInCommandInfo;
use Room11\Jeeves\System\PluginManager;
use function Amp\resolve;

class Alias implements BuiltInCommand
{
    private $chatClient;
    private $aliasStorage;
    private $adminStorage;
    private $builtInCommandManager;
    private $pluginManager;

    public function __construct(
        ChatClient $chatClient,
        CommandAliasStorage $aliasStorage,
        AdminStorage $adminStorage,
        BuiltInActionManager $builtInCommandManager,
        PluginManager $pluginManager
    ) {
        $this->chatClient = $chatClient;
        $this->aliasStorage = $aliasStorage;
        $this->adminStorage = $adminStorage;
        $this->builtInCommandManager = $builtInCommandManager;
        $this->pluginManager = $pluginManager;
    }

    private function addAlias(CommandMessage $command)
    {
        $room = $command->getRoom();
        $aliasCommand = $command->getParameter(0);
        $mapping = implode(' ', $command->getParameters(1));

        if ($this->builtInCommandManager->hasRegisteredCommand($aliasCommand)) {
            return $this->chatClient->postReply(
                $command,
                new PendingMessage(
                    "Command '{$aliasCommand}' is built in and cannot be altered",
                    $command
                )
            );
        }

        if ($this->pluginManager->isCommandMappedForRoom($room, $aliasCommand)) {
            return $this->chatClient->postReply(
                $command,
                new PendingMessage(
                    "Command '{$aliasCommand}' is already mapped. Use `!!command list` to display the currently mapped commands.",
                    $command
                )
            );
        }

        if (yield $this->aliasStorage->exists($room, $aliasCommand)) {
            return $this->chatClient->postReply(
                $command,
                new PendingMessage("Alias '!!{$aliasCommand}' already exists.", $command)
            );
        }

        yield $this->aliasStorage->add($room, $aliasCommand, $mapping);

        return $this->chatClient->postMessage(
            $room,
            new PendingMessage(
                "Command '!!{$aliasCommand}' aliased to '!!{$mapping}'",
                $command
            )
        );
    }

    private function removeAlias(CommandMessage $command): \Generator
    {
        $aliasCommand = $command->getParameter(0);

        if (!yield $this->aliasStorage->exists($command->getRoom(), $aliasCommand)) {
            return $this->chatClient->postMessage(
{'fraction_non_alphanumeric': 0.11112940401712217, 'fraction_numerical': 0.007902535396773132, 'mean_word_length': 2.455631399317406, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6621609', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1559, 'n_words': 327}