<?php
namespace Claromentis\Menu\Infobar;

$GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"] = (@$GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"] ?: array()) + array("_49c6565477ee9f82" . "2b499a53e6d2995f" => base64_decode("YXJyYXlfa2V5X2V4a" . "XN0cw=="), "_c133e115f1ce8ed7b5a6bced4" . "c874862" => base64_decode("YXJyYXl" . "fcG9w"), "_31767d7ad58" . "e706dbf47b7a" . "51a0fe6c4" => base64_decode("Y291b" . "nQ="), "_822680520327e9387ae2d5c06" . "5ea52e1" => base64_decode("YXJyYXlfZmxpc" . "A=="), "_d706c160e4" . "5bf7434fff8" . "2a5976f0e19" => base64_decode("YXJyYXlfa2" . "V5cw=="));
use Claromentis\Core\Acl\AclRepository;
use Claromentis\Core\Security\SecurityContext;
use Claromentis\Menu\Exception\MenuItemForbiddenException;
use Claromentis\Menu\Exception\MenuItemNotFoundException;
use Claromentis\Menu\Exception\MenuItemValidationException;
use Claromentis\Menu\Exception\MenuItemRepositoryException;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;






class ApplicationsListItems
{
    use LoggerAwareTrait;
    private AclRepository $aclRepository;
    private MenuItemFactory $menuItemFactory;
    private MenuItemRepository $repository;
    private array $cachedDisplayableItems = [];
    public function __construct(MenuItemRepository $_9cc4826fc470a6d42bd40c876b9a30bd, MenuItemFactory $menuItemFactory, AclRepository $aclRepository, LoggerInterface $logger)
    {
        $this->repository = $_9cc4826fc470a6d42bd40c876b9a30bd;
        $this->menuItemFactory = $menuItemFactory;
        $this->aclRepository = $aclRepository;
        $this->setLogger($logger);
    }
    
    
    
    
    
    
    
    
    public function canView(array $_9cee5807b5bab08d25cd4213e63cb764, SecurityContext $context) : bool
    {
        $_efbacf3fcde43c382e36d399f1cd12f0 = $GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_822680520327e9387ae2d5c065ea" . "52e1"]($this->repository->getViewableIds($context));
        foreach ($_9cee5807b5bab08d25cd4213e63cb764 as $id) {
            if (!$GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_49c6565477ee9f822b499a53e6d2" . "995f"]($id, $_efbacf3fcde43c382e36d399f1cd12f0)) {
                return false;
            }
        }
        return true;
    }
    
    
    
    
    
    
    
    
    
    public function hasApplication(string $_2d58a9232beb5db16f3d8b9aa1d58c71) : bool
    {
        $_38dc3c4573d1bf70268680af788c25d2 = null;
        try {
            $_38dc3c4573d1bf70268680af788c25d2 = $this->repository->get(null, null, [ItemType::APPLICATION], [$_2d58a9232beb5db16f3d8b9aa1d58c71]);
        } catch (\Throwable $throwable) {
            
            
            
        }
        return !empty($_38dc3c4573d1bf70268680af788c25d2);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    public function getApplication(string $_2d58a9232beb5db16f3d8b9aa1d58c71, ?SecurityContext $context = null) : MenuItemApplication
    {
        
        
        $_1233ecb7129e4ebf7bca209fc4ca2d77 = $this->repository->get(null, null, [ItemType::APPLICATION], [$_2d58a9232beb5db16f3d8b9aa1d58c71]);
        if (empty($_1233ecb7129e4ebf7bca209fc4ca2d77)) {
            throw MenuItemNotFoundException::appIdNotFound($_2d58a9232beb5db16f3d8b9aa1d58c71);
        }
        if ($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_31767d7ad58e706dbf47b7a51a0fe6" . "c4"]($_1233ecb7129e4ebf7bca209fc4ca2d77) > 1) {
            throw MenuItemValidationException::appIdNotUnique($_2d58a9232beb5db16f3d8b9aa1d58c71);
        }
        if ($context && !$this->canView($GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_d706c160e45bf7434" . "fff82a5976f0e19"]($_1233ecb7129e4ebf7bca209fc4ca2d77), $context)) {
            throw MenuItemForbiddenException::forbiddenId($_2d58a9232beb5db16f3d8b9aa1d58c71);
        }
        return $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_c133e115f1ce8ed7b5a" . "6bced4c874862"]($_1233ecb7129e4ebf7bca209fc4ca2d77);
    }
    
    
    
    
    
    
    
    
    public function getItem(int $id, SecurityContext $context) : MenuItemInterface
    {
        $_38dc3c4573d1bf70268680af788c25d2 = $this->repository->get([$id]);
        if (!isset($_c5ca63e2f3cceb505a5417b4abb64d55[$id])) {
            throw MenuItemNotFoundException::idNotFound($id);
        }
        $_c5ca63e2f3cceb505a5417b4abb64d55 = $_38dc3c4573d1bf70268680af788c25d2[$id];
        if (!$this->canView([$_c5ca63e2f3cceb505a5417b4abb64d55->getId()], $context)) {
            throw MenuItemForbiddenException::forbiddenId($id);
        }
        return $_c5ca63e2f3cceb505a5417b4abb64d55;
    }
    
    
    
    
    
    
    
    
    public function getDisplayableItems(SecurityContext $context) : array
    {
        $_65976ee34559dcb12bd6d5db68edf074 = $context->GetUserId();
        if (!isset($this->cachedDisplayableItems[$_65976ee34559dcb12bd6d5db68edf074])) {
            $_38dc3c4573d1bf70268680af788c25d2 = $this->repository->get(null, [InfobarMenuType::APPLICATIONS], null, null, $context);
            foreach ($_38dc3c4573d1bf70268680af788c25d2 as $_1f50e30be8c2fefa6881834e71d504a2 => $_c5ca63e2f3cceb505a5417b4abb64d55) {
                if ($_c5ca63e2f3cceb505a5417b4abb64d55 instanceof MenuItemLinkInterface && $_c5ca63e2f3cceb505a5417b4abb64d55->getDisabled()) {
                    unset($_38dc3c4573d1bf70268680af788c25d2[$_1f50e30be8c2fefa6881834e71d504a2]);
                }
            }
            $this->cachedDisplayableItems[$_65976ee34559dcb12bd6d5db68edf074] = $_38dc3c4573d1bf70268680af788c25d2;
        }
        return $this->cachedDisplayableItems[$_65976ee34559dcb12bd6d5db68edf074];
    }
    public function getAdminItems(array $_94bb23a8efc6e7795b00b21626283af1) : array
    {
        return $this->repository->get(null, $_94bb23a8efc6e7795b00b21626283af1);
    }
    
    
    
    
    
    
    public function getAdminItem(int $id) : MenuItemInterface
    {
        $_38dc3c4573d1bf70268680af788c25d2 = $this->repository->get([$id]);
        if (!isset($_38dc3c4573d1bf70268680af788c25d2[$id])) {
            throw MenuItemNotFoundException::idNotFound($id);
        }
        return $_38dc3c4573d1bf70268680af788c25d2[$id];
    }
    
    
    
    
    
    public function getLinks(SecurityContext $context) : array
    {
        return $this->repository->get(null, [InfobarMenuType::APPLICATIONS], [ItemType::APPLICATION, ItemType::LINK], null, $context);
    }
    
    
    
    
    
    
    
    
    
    
    
    public function saveNewDivider(int $menu) : MenuItemDivider
    {
        $_954f14aba204c4076de6dae38c5624c5 = $this->menuItemFactory->createDivider($menu, null, $this->repository->getMaxOrder($menu));
        $this->save($_954f14aba204c4076de6dae38c5624c5);
        return $_954f14aba204c4076de6dae38c5624c5;
    }
    
    
    
    
    
    
    
    
    
    
    public function save(MenuItemInterface $_c5ca63e2f3cceb505a5417b4abb64d55) : void
    {
        $this->repository->save($_c5ca63e2f3cceb505a5417b4abb64d55);
    }
}