<?php
namespace Claromentis\Infocapture;

$GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"] ?: array()) + array("_bd71c3b5c201fc11db82" . "ed14ce424de5" => base64_decode("aW5fYXJ" . "yYXk="), "_ae66fc14c5ccbdbb5a6f0b5c09" . "7fe554" => base64_decode("TElLRV9BTk" . "Q="), "_25867e3e3db28969253f3a370" . "c3166ba" => base64_decode("ZGVzY" . "w=="), "_fac247f575ab6e9caa9b49b1d" . "93cb6ee" => base64_decode("SXNzdWUgYWNjZXNzIGRlbml" . "lZA=="), "_db618ca8" . "e79f97b4d" . "ff753ec83" . "7942cf" => base64_decode("cHJv" . "amVj" . "dF9p" . "ZA=="), "_a82e7c9cba13670954fbe01d604" . "c242a" => base64_decode("aGlzdG9yeQ=" . "="), "i" . "d" => base64_decode("aW" . "Q="), "_064a039fdabac69a" . "532d3225d0ed0f57" => base64_decode("UHJvamVjdCBub3QgZm" . "91bmQ="), "_d706c160e45b" . "f7434fff82a59" . "76f0e19" => base64_decode("YXJyYXlfa" . "2V5cw=="), "_597358b143d8796d9510d8b223403a8" . "e" => base64_decode("SXNzdWUgbm90IGZvd" . "W5k"), "_21927a1f1e29c97ceeb5892faccb65f" . "9" => base64_decode("UHJvamV" . "jdCBhY2" . "Nlc3MgZ" . "GVuaWVk"));
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["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_597358b143d8796d9510d8b223403a" . "8e"]);
        }
        $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["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_a82e7c9cba13670954fbe01d604c" . "242a"], $_8e0ce41b9e1fa25e238257fe368c5da0[(int) $_13fd70a895d3350e21a9d4872e0260b5->GetProperty($GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["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["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_bd71c3b5c201fc11db82ed14ce4" . "24de5"]($status, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_d706c160e45bf7434fff82a5" . "976f0e19"]($_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["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_ae66fc14c5ccbdbb5a6f0b5c097fe" . "554"], $_7f5aa4e96d4b415a86ed72ebff677cbe];
                }
                $_baf4db07cf1f02bad978e239117689d1->SetFieldsConstraints($_8dd62279a981b9051240125b1b6fbc70);
            }
        }
        
        
        
        $_baf4db07cf1f02bad978e239117689d1->SetSortingMode(FB_SORT_BY_DATE_FILLED, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_25867e3e3db2" . "8969253f3a370" . "c3166ba"]);
        
        
        $_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["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_064a039fdabac69a532d" . "3225d0ed0f57"]);
        }
        if (!$_0a3f284de65e596869ef47aa9e91f906->acl->UserCanViewProject($context)) {
            throw new PermissionException($GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_21927a1f1e29c97ceeb5892f" . "accb65f9"]);
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    private function validateIssue(SecurityContext $context, HDIssue $_13fd70a895d3350e21a9d4872e0260b5) : void
    {
        $_47d202493f86e587c8454fae73657c90 = (int) $_13fd70a895d3350e21a9d4872e0260b5->GetProperty($GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_db618ca" . "8e79f97b" . "4dff753e" . "c837942c" . "f"]);
        $_0a3f284de65e596869ef47aa9e91f906 = $this->getProject($context, $_47d202493f86e587c8454fae73657c90);
        if (!$_0a3f284de65e596869ef47aa9e91f906->acl->UserHasPermissionForIssue($context, $_13fd70a895d3350e21a9d4872e0260b5, PERM_VIEW)) {
            throw new PermissionException($GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_fac247f575ab6e9caa9b" . "49b1d93cb6ee"]);
        }
    }
}