<?php
namespace Claromentis\Infocapture;

$GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"] = (@$GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"] ?: array()) + array("_d706c160e45bf7434fff82a5" . "976f0e19" => base64_decode("YXJyYXlfa2" . "V5cw=="), "i" . "d" => base64_decode("a" . "W" . "Q" . "="), "_ae66fc14c5ccbdbb5a6" . "f0b5c097fe554" => base64_decode("TElLRV9" . "BTkQ="), "_25867e3e3db28969253f3a370c" . "3166ba" => base64_decode("ZGVzYw" . "=="), "_fac247f575ab6e9caa9b49b1d" . "93cb6ee" => base64_decode("SXNzdWUgYWNjZXNzIGRlbmllZA" . "=="), "_21927a1" . "f1e29c97" . "ceeb5892" . "faccb65f" . "9" => base64_decode("UHJvamVjdCBhY2Nlc" . "3MgZGVuaWVk"), "_064a039fdabac69a532d32" . "25d0ed0f57" => base64_decode("UHJvamVjdCBub" . "3QgZm91bmQ="), "_597358b143d8796" . "d9510d8b223403a8" . "e" => base64_decode("SXNzdW" . "Ugbm90" . "IGZvdW" . "5k"), "_bd71c3b5" . "c201fc11d" . "b82ed14ce" . "424de5" => base64_decode("aW5f" . "YXJy" . "YXk="), "_a82e7c9cba136709" . "54fbe01d604c242a" => base64_decode("aGlzdG" . "9yeQ=="), "_db618ca8e79f97b4d" . "ff753ec837942cf" => base64_decode("cHJvamVj" . "dF9pZA=="));
use Claromentis\Infocapture\Repository\IssueRepository;
use Claromentis\Infocapture\Filter\ProjectFilter;
use Claromentis\Infocapture\Repository\FormRepository;
use Claromentis\Infocapture\Repository\IssueHistoryRepository;
use Claromentis\Infocapture\Repository\ProjectRepository;
use FBForm;
use HDIssue;
use HDProject;
use HDProjectsList;
use Claromentis\Core\Security\SecurityContext;
use Claromentis\Infocapture\Exception\NotFoundException;
use Claromentis\Infocapture\Exception\PermissionException;
use Claromentis\Infocapture\Filter\IssueFilter;
use ICIssuesListProvider;






class Api
{
    protected HDProjectsList $projectsList;
    protected FormRepository $formRepository;
    protected IssueRepository $issueRepository;
    protected ProjectRepository $projectRepository;
    protected ICIssuesListProvider $issuesListProvider;
    protected IssueHistoryRepository $issueHistoryRepository;
    public function __construct(HDProjectsList $_4455bfbb82fcf1577034089e2fb7d61b, FormRepository $_2a8eab9adcccd98c59e9bc1dd3326bb7, IssueRepository $_29bcd2ba8b1ed8e200778c5f35e00305, ProjectRepository $_9375ba1d91fdf6590ddb3a4874c783d8, ICIssuesListProvider $_baf4db07cf1f02bad978e239117689d1, IssueHistoryRepository $_900f8a9de284d41db272c12d2e1cc8fc)
    {
        $this->projectsList = $_4455bfbb82fcf1577034089e2fb7d61b;
        $this->formRepository = $_2a8eab9adcccd98c59e9bc1dd3326bb7;
        $this->issueRepository = $_29bcd2ba8b1ed8e200778c5f35e00305;
        $this->projectRepository = $_9375ba1d91fdf6590ddb3a4874c783d8;
        $this->issuesListProvider = $_baf4db07cf1f02bad978e239117689d1;
        $this->issueHistoryRepository = $_900f8a9de284d41db272c12d2e1cc8fc;
    }
    
    
    
    
    
    
    
    public function getProject(SecurityContext $context, int $_47d202493f86e587c8454fae73657c90) : HDProject
    {
        $_0a3f284de65e596869ef47aa9e91f906 = $this->projectRepository->findProjectOrFail($_47d202493f86e587c8454fae73657c90);
        $this->validateProject($context, $_0a3f284de65e596869ef47aa9e91f906);
        return $_0a3f284de65e596869ef47aa9e91f906;
    }
    
    
    
    
    
    
    
    
    public function getProjects(SecurityContext $context, ProjectFilter $_3863b891050928d75cadf9341bee6e83) : array
    {
        $this->projectsList->SetPermsChecking(HD_PERM_ACCESS);
        
        
        $this->projectsList->limit = $_3863b891050928d75cadf9341bee6e83->getLimit();
        $this->projectsList->start_from = $_3863b891050928d75cadf9341bee6e83->getOffset();
        if ($_3863b891050928d75cadf9341bee6e83->GetStatus() !== null) {
            $this->projectsList->SetFilterStatus(\ICProjectStatus::FromRestName($_3863b891050928d75cadf9341bee6e83->GetStatus()));
        }
        $_68680c984182bf7876b2bb0ab77ebe01 = [];
        foreach ($this->projectsList->GetList() as $_0a3f284de65e596869ef47aa9e91f906) {
            $_68680c984182bf7876b2bb0ab77ebe01[] = $_0a3f284de65e596869ef47aa9e91f906;
        }
        return $_68680c984182bf7876b2bb0ab77ebe01;
    }
    
    
    
    
    
    
    
    public function getProjectsCount(ProjectFilter $_3863b891050928d75cadf9341bee6e83) : int
    {
        $this->projectsList->SetPermsChecking(HD_PERM_ACCESS);
        
        
        $this->projectsList->limit = 0;
        $this->projectsList->start_from = 0;
        if ($_3863b891050928d75cadf9341bee6e83->GetStatus() !== null) {
            $this->projectsList->SetFilterStatus(\ICProjectStatus::FromRestName($_3863b891050928d75cadf9341bee6e83->GetStatus()));
        }
        return $this->projectsList->GetCount();
    }
    
    
    
    
    
    
    
    
    
    
    
    
    public function getProjectIssue(SecurityContext $context, int $_47d202493f86e587c8454fae73657c90, int $_918f19473774a5cc858d1d78d17239fd) : HDIssue
    {
        $_ea94b42bce739d2b7107769e1b04306c = $this->issueRepository->getIssueId($_47d202493f86e587c8454fae73657c90, $_918f19473774a5cc858d1d78d17239fd);
        return $this->getIssue($context, $_ea94b42bce739d2b7107769e1b04306c);
    }
    
    
    
    
    
    
    
    
    
    
    
    public function getIssue(SecurityContext $context, int $_ea94b42bce739d2b7107769e1b04306c) : HDIssue
    {
        $_13fd70a895d3350e21a9d4872e0260b5 = $this->issueRepository->findIssue($_ea94b42bce739d2b7107769e1b04306c);
        if ($_13fd70a895d3350e21a9d4872e0260b5 === null) {
            throw new NotFoundException($GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_597358b143d8796d9510d8b223" . "403a8e"]);
        }
        $this->validateIssue($context, $_13fd70a895d3350e21a9d4872e0260b5);
        return $_13fd70a895d3350e21a9d4872e0260b5;
    }
    
    
    
    
    
    
    
    
    
    
    
    public function getIssues(SecurityContext $context, IssueFilter $_03e9164d0a343b742df4793268b80767) : array
    {
        $_03e9164d0a343b742df4793268b80767->setContext($context);
        $_baf4db07cf1f02bad978e239117689d1 = $this->setIssuesListFilters($this->issuesListProvider, $_03e9164d0a343b742df4793268b80767);
        return $_baf4db07cf1f02bad978e239117689d1->GetList();
    }
    
    
    
    
    
    
    
    
    
    
    
    public function getIssuesCount(SecurityContext $context, IssueFilter $_03e9164d0a343b742df4793268b80767) : int
    {
        $_03e9164d0a343b742df4793268b80767->setContext($context);
        $_baf4db07cf1f02bad978e239117689d1 = $this->setIssuesListFilters($this->issuesListProvider, $_03e9164d0a343b742df4793268b80767);
        
        
        $_baf4db07cf1f02bad978e239117689d1->SetLimits();
        return (int) $_baf4db07cf1f02bad978e239117689d1->GetCount();
    }
    
    
    
    
    
    
    
    
    
    public function getIssueForm(SecurityContext $context, HDIssue $_13fd70a895d3350e21a9d4872e0260b5) : FBForm
    {
        $this->validateIssue($context, $_13fd70a895d3350e21a9d4872e0260b5);
        return $this->formRepository->getIssueForm($_13fd70a895d3350e21a9d4872e0260b5);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    public function loadHistoryForIssues(SecurityContext $context, array $_a020ae3bf420cc88a5518a5ff484ac6f) : void
    {
        $_8e0ce41b9e1fa25e238257fe368c5da0 = $this->issueHistoryRepository->getHistoryForIssues($context, $_a020ae3bf420cc88a5518a5ff484ac6f);
        foreach ($_a020ae3bf420cc88a5518a5ff484ac6f as $_13fd70a895d3350e21a9d4872e0260b5) {
            $_13fd70a895d3350e21a9d4872e0260b5->SetProperty($GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_a82e7c9cba13670954f" . "be01d604c242a"], $_8e0ce41b9e1fa25e238257fe368c5da0[(int) $_13fd70a895d3350e21a9d4872e0260b5->GetProperty($GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["i" . "d"])] ?? null);
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    private function setIssuesListFilters(ICIssuesListProvider $_baf4db07cf1f02bad978e239117689d1, IssueFilter $_03e9164d0a343b742df4793268b80767) : ICIssuesListProvider
    {
        
        
        $context = $_03e9164d0a343b742df4793268b80767->getContext();
        if ($context) {
            $_baf4db07cf1f02bad978e239117689d1->SetTargetUser($context->GetUserId());
            $_baf4db07cf1f02bad978e239117689d1->ApplyPermissions();
        }
        
        
        if (!empty($_03e9164d0a343b742df4793268b80767->getProjectId())) {
            if ($context) {
                $_0a3f284de65e596869ef47aa9e91f906 = $this->getProject($context, $_03e9164d0a343b742df4793268b80767->getProjectId());
            } else {
                $_0a3f284de65e596869ef47aa9e91f906 = $this->projectRepository->findProjectOrFail($_03e9164d0a343b742df4793268b80767->getProjectId());
            }
            $_baf4db07cf1f02bad978e239117689d1->SetFilterProject($_0a3f284de65e596869ef47aa9e91f906);
        }
        
        
        if (!empty($_03e9164d0a343b742df4793268b80767->getReporterList())) {
            $_baf4db07cf1f02bad978e239117689d1->SetFilterReporter($_03e9164d0a343b742df4793268b80767->getReporterList());
        }
        if (!empty($_03e9164d0a343b742df4793268b80767->getAssigneeList())) {
            $_baf4db07cf1f02bad978e239117689d1->SetFilterAssigned($_03e9164d0a343b742df4793268b80767->getAssigneeList());
        }
        if ($_03e9164d0a343b742df4793268b80767->getCreatedDate() !== null) {
            $_baf4db07cf1f02bad978e239117689d1->SetFilterDateCreated($_03e9164d0a343b742df4793268b80767->getCreatedDate()->getDate());
        }
        if ($_03e9164d0a343b742df4793268b80767->getModifiedDate() !== null) {
            $_baf4db07cf1f02bad978e239117689d1->SetFilterDateLastModified($_03e9164d0a343b742df4793268b80767->getModifiedDate()->getDate());
        }
        if ($_03e9164d0a343b742df4793268b80767->getStatus() !== null) {
            $status = $_03e9164d0a343b742df4793268b80767->getStatus();
            $_0a3f284de65e596869ef47aa9e91f906 = $_baf4db07cf1f02bad978e239117689d1->GetFilterProject();
            $_40f409357d2903a03cc93925dad205eb = $_0a3f284de65e596869ef47aa9e91f906->GetStatuses();
            if ($GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_bd71c3b5" . "c201fc11d" . "b82ed14ce" . "424de5"]($status, $GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_d706c160e45bf7434fff" . "82a5976f0e19"]($_40f409357d2903a03cc93925dad205eb))) {
                $_baf4db07cf1f02bad978e239117689d1->SetFilterStatus($status);
            }
        }
        if ($_03e9164d0a343b742df4793268b80767->getArchived() !== null) {
            $_baf4db07cf1f02bad978e239117689d1->SetFilterArchived(true, (int) $_03e9164d0a343b742df4793268b80767->getArchived());
        }
        if ($_03e9164d0a343b742df4793268b80767->getMonitoring()) {
            $_baf4db07cf1f02bad978e239117689d1->SetFilterMonitoring($_03e9164d0a343b742df4793268b80767->getMonitoring());
        }
        
        
        if (!empty($_03e9164d0a343b742df4793268b80767->getQuery())) {
            $_7f5aa4e96d4b415a86ed72ebff677cbe = $_03e9164d0a343b742df4793268b80767->getQuery();
            $_fc95e77eab852b63129161149bcdac0d = $_03e9164d0a343b742df4793268b80767->getQueryFields();
            if (empty($_fc95e77eab852b63129161149bcdac0d)) {
                $_baf4db07cf1f02bad978e239117689d1->SetFilterKeywords($_7f5aa4e96d4b415a86ed72ebff677cbe);
            } else {
                $_8dd62279a981b9051240125b1b6fbc70 = [];
                foreach ($_fc95e77eab852b63129161149bcdac0d as $_13d133c615714d50832f54e91fea8fb0) {
                    $_8dd62279a981b9051240125b1b6fbc70[] = [$_13d133c615714d50832f54e91fea8fb0, $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_ae66fc14c5ccbdbb5a6f0b5c097f" . "e554"], $_7f5aa4e96d4b415a86ed72ebff677cbe];
                }
                $_baf4db07cf1f02bad978e239117689d1->SetFieldsConstraints($_8dd62279a981b9051240125b1b6fbc70);
            }
        }
        
        
        
        $_baf4db07cf1f02bad978e239117689d1->SetSortingMode(FB_SORT_BY_DATE_FILLED, $GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_25867e3e" . "3db289692" . "53f3a370c" . "3166ba"]);
        
        
        $_baf4db07cf1f02bad978e239117689d1->SetLimits($_03e9164d0a343b742df4793268b80767->getOffset(), $_03e9164d0a343b742df4793268b80767->getLimit());
        return $_baf4db07cf1f02bad978e239117689d1;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    private function validateProject(SecurityContext $context, HDProject $_0a3f284de65e596869ef47aa9e91f906) : void
    {
        if ($_0a3f284de65e596869ef47aa9e91f906->GetId() === null || $_0a3f284de65e596869ef47aa9e91f906->IsError()) {
            throw new NotFoundException($GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_064a039fdabac69a532d3225" . "d0ed0f57"]);
        }
        if (!$_0a3f284de65e596869ef47aa9e91f906->acl->UserCanViewProject($context)) {
            throw new PermissionException($GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_21927a1f1e29c97ceeb5892faccb65" . "f9"]);
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    private function validateIssue(SecurityContext $context, HDIssue $_13fd70a895d3350e21a9d4872e0260b5) : void
    {
        $_47d202493f86e587c8454fae73657c90 = (int) $_13fd70a895d3350e21a9d4872e0260b5->GetProperty($GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["_db618ca8e79f97b4dff753e" . "c837942cf"]);
        $_0a3f284de65e596869ef47aa9e91f906 = $this->getProject($context, $_47d202493f86e587c8454fae73657c90);
        if (!$_0a3f284de65e596869ef47aa9e91f906->acl->UserHasPermissionForIssue($context, $_13fd70a895d3350e21a9d4872e0260b5, PERM_VIEW)) {
            throw new PermissionException($GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_fac247f575ab6" . "e9caa9b49b1d93" . "cb6ee"]);
        }
    }
}