<?php

$hookManager->register(
    function (\WHMCS\View\Menu\Item $primarySidebar)
    {

        /**
         * @var  \ModulesGarden\Servers\OpenXchange\Core\Http\Request $request
         */
        $request = \ModulesGarden\Servers\OpenXchange\Core\Helper\sl('request');

        /**
         * return if user is not logged
         */
        if(!$request->getSessionValue('uid'))
        {
            return;
        }

        /**
         * return if service id doesn't exists
         */
        if (!$request->getValue('id'))
        {
            return;
        }

        /**
         *
         */
        $clientAreaSideBar = new \ModulesGarden\Servers\OpenXchange\App\Services\ClientAreaSidebarService($request->getValue("id"), $primarySidebar);
        if (!$clientAreaSideBar->isActive() || !$clientAreaSideBar->isSupportedModule())
        {
            return;
        }
        if (!function_exists('ModuleBuildParams'))
        {
            require_once \ModulesGarden\Servers\OpenXchange\Core\ModuleConstants::getFullPathWhmcs('includes') . DIRECTORY_SEPARATOR . "modulefunctions.php";
        }
        $params = \ModuleBuildParams($request->getValue("id"));
        \ModulesGarden\Servers\OpenXchange\Core\Helper\sl("whmcsParams")->setParams($params);

        //Page Cancel
        if ($request->getValue('action') == "cancel")
        {
            $clientAreaSideBar->informationReplaceUri();
        } //Page Productdetails
        else
        {
            if ($request->getValue('action') == "productdetails")
            {
                $clientAreaSideBar->informationReplaceUri();
                $clientAreaSideBar->build();
            }
        }
    }, 943
);
