<?php

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

        /**
         * @var  \ThurData\Servers\KerioEmail\Core\Http\Request $request
         */
        $request = \ThurData\Servers\KerioEmail\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 \ThurData\Servers\KerioEmail\App\Services\ClientAreaSidebarService($request->get("id"), $primarySidebar);
        if (!$clientAreaSideBar->isActive() || !$clientAreaSideBar->isSupportedModule())
        {
            return;
        }
        if (!function_exists('ModuleBuildParams'))
        {
            require_once \ThurData\Servers\KerioEmail\Core\ModuleConstants::getFullPathWhmcs('includes') . DIRECTORY_SEPARATOR . "modulefunctions.php";
        }
        $params = \ModuleBuildParams($request->get("id"));
        \ThurData\Servers\KerioEmail\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();
            }
        }
// thurdev: check sort number 943 (https://whmcs.community/topic/257013-question-on-add_hook/)
    }, 943
);
