<?php
namespace Claromentis\Infocapture;

$GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"] ?: array()) + array("_25867e3e3db28969253f" . "3a370c3166ba" => base64_decode("ZGVz" . "Yw=="), "_d706c160" . "e45bf7434" . "fff82a597" . "6f0e19" => base64_decode("YXJyYXlfa2V" . "5cw=="), "_fac247f575ab6e9caa9b49b1d93cb6e" . "e" => base64_decode("SXNzdWUgYWNjZXNzIGRlb" . "mllZA=="), "_db618ca8e79f9" . "7b4dff753ec837" . "942cf" => base64_decode("cHJvamVjdF9pZA=" . "="), "_ae66fc14c5ccbdbb5a6f0b5c" . "097fe554" => base64_decode("TElLRV9BTkQ" . "="), "_21927a1f1e29c97ceeb5892f" . "accb65f9" => base64_decode("UHJvamVjdCBhY2Nlc" . "3MgZGVuaWVk"), "_064a039fdab" . "ac69a532d322" . "5d0ed0f57" => base64_decode("UHJvamVjdCBub3QgZm" . "91bmQ="), "_597358b143d8796d9510d8b22" . "3403a8e" => base64_decode("SXNzdWUgb" . "m90IGZvdW" . "5k"), "i" . "d" => base64_decode("aW" . "Q="), "_bd71c3b5c201f" . "c11db82ed14ce4" . "24de5" => base64_decode("aW5fYXJy" . "YXk="), "_a82e7c9cba13670954fbe01d" . "604c242a" => base64_decode("aGl" . "zdG" . "9ye" . "Q=="));
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["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_597358b143d8796d9510d8b22340" . "3a8e"]);
        }
        $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["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_a82e7c9cba13670954fbe01d604c242" . "a"], $_8e0ce41b9e1fa25e238257fe368c5da0[(int) $_13fd70a895d3350e21a9d4872e0260b5->GetProperty($GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["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["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_bd71c3b5c201fc1" . "1db82ed14ce424de" . "5"]($status, $GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_d706c160e45bf7434fff82a5976f0" . "e19"]($_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"]["_ae66fc14c5ccbdbb5a6f0b5c" . "097fe554"], $_7f5aa4e96d4b415a86ed72ebff677cbe];
                }
                $_baf4db07cf1f02bad978e239117689d1->SetFieldsConstraints($_8dd62279a981b9051240125b1b6fbc70);
            }
        }
        
        
        
        $_baf4db07cf1f02bad978e239117689d1->SetSortingMode(FB_SORT_BY_DATE_FILLED, $GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_25867e3e3db289692" . "53f3a370c3166ba"]);
        
        
        $_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["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_064a039fdabac69a532d3" . "225d0ed0f57"]);
        }
        if (!$_0a3f284de65e596869ef47aa9e91f906->acl->UserCanViewProject($context)) {
            throw new PermissionException($GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_21927a1f1e29c97ceeb5892faccb65f" . "9"]);
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    private function validateIssue(SecurityContext $context, HDIssue $_13fd70a895d3350e21a9d4872e0260b5) : void
    {
        $_47d202493f86e587c8454fae73657c90 = (int) $_13fd70a895d3350e21a9d4872e0260b5->GetProperty($GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_db618ca8e79f97b4dff753ec83794" . "2cf"]);
        $_0a3f284de65e596869ef47aa9e91f906 = $this->getProject($context, $_47d202493f86e587c8454fae73657c90);
        if (!$_0a3f284de65e596869ef47aa9e91f906->acl->UserHasPermissionForIssue($context, $_13fd70a895d3350e21a9d4872e0260b5, PERM_VIEW)) {
            throw new PermissionException($GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_fac247f575ab6e9caa9b49b1d93c" . "b6ee"]);
        }
    }
}