<?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->getSession('uid'))
        {
            return;
        }

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

        /**
         *
         */
        $clientAreaSideBar = new \ModulesGarden\Servers\OpenXchange\App\Services\ClientAreaSidebarService($request->get("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->get("id"));
        \ModulesGarden\Servers\OpenXchange\Core\Helper\sl("whmcsParams")->setParams($params);

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