<?php

declare(strict_types=1);

namespace Apexl\Io\Menu\Hooks;

use Apexl\Io\Interfaces\AbstractHook;
use Apexl\Io\Menu\Interfaces\ProvidesMenuItemsInterface;
use Apexl\Io\Menu\Services\MenuManager;
use Apexl\Io\Module;

final readonly class RegisterMenuItems implements AbstractHook
{
    public function hooks(): array
    {
        return ['initialiseModule'];
    }

    public function __invoke(Module $module, MenuManager $menu): Module
    {
        if (method_exists($module, 'registerMenuItems')) {
            \Apexl\Io\Logger\logger('error')->warning(
                sprintf(
                    'Deprecation - Module \'%s\' should register menu items by implementing %s\n',
                    $module->getName(),
                    ProvidesMenuItemsInterface::class,
                ),
            );
            $module->registerMenuItems($menu);

            return $module;
        }

        if ($module instanceof ProvidesMenuItemsInterface) {
            $module->menuItems($menu);
        }

        return $module;
    }
}
