<?php
namespace Claromentis\Infocapture\Repository;

$GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"] ?: array()) + array("_6be7ef0f685c2a96fcae" . "1fc2ccc30198" => base64_decode("Y2hhbmd" . "lcw=="), "_ed3e1aa0ac98bd06" . "2e332a176b59d4f0" => base64_decode("IHdoZW4gbG9hZGluZyBpc3N1" . "ZSBoaXN0b3J5"), "_74d29cb" . "83677a22" . "05e6a7ce" . "d955e7e7" . "e" => base64_decode("IHdoZW4gbG9hZGluZyBpc3N1" . "ZSBoaXN0b3J5OiA="), "_74a64e2ccf03f2b75d319" . "08c6f140fad" => base64_decode("aC5pc3N1ZV9pZCB" . "JTiBpbjppbnQ6aX" . "NzdWVJZHM="), "_13d133c6" . "15714d508" . "32f54e91f" . "ea8fb0" => base64_decode("Zmllb" . "GQ="), "_040eaefa6" . "6260cc2eae" . "ebf3fb7e20" . "ed1" => base64_decode("d" . "Q" . "=" . "="), "_5de2842b2a047fb1" . "0ad57b32647a5164" => base64_decode("UHJvamVjdCBub3QgZm91bmQgZm9y" . "IGlzc3VlIA=="), "user_" . "id" => base64_decode("dXNlcl9pZA" . "=="), "_b2a1616419a412c8" . "72af907a317ff4b8" => base64_decode("Zmly" . "c3Ru" . "YW1l"), "_39638557be821c8556d28efa25c94" . "ff1" => base64_decode("aXNzdWVfaW" . "Q="), "_b7928b5c78f330e3ccad7" . "afb9bbbf2b0" => base64_decode("U0VMRUNUIGguaWQsIGgudXNlcl9pZCwgaC5pc3N1ZV9pZCwgaC5kYXRlMSwgaC5maWVsZCwgaC5jaGFuZ2VzLCBoLmZpZWxkX3N5bW5hbWUsIHUuZmlyc3RuYW1lLCB1LnN1c" . "m5hbWUKCQkJRlJPTSBoZF9pc3N1ZV9oaXN0b3J5IGgKCQkJT1JERVIgQlkgaC5kYXRlMSBBU0M="), "_307e0f578" . "6e72e5fa92" . "2896fcbdfa" . "e03" => base64_decode("a" . "A" . "=" . "="), "_083135d58cba" . "2106d77bbb72d" . "a5a4a2d" => base64_decode("ZmllbGRfc" . "3ltbmFtZQ" . "=="), "_3b1c56066ee6134b4364b618" . "bca373d6" => base64_decode("bmFtZV9kaXNwbGF" . "5"), "_92657887" . "7d7619633" . "75f967dc4" . "f578d7" => base64_decode("RXJyb3IgbG9hZG" . "luZyBmb3JtIGZv" . "ciBpc3N1ZSA="), "_4d707fab93bebb00bbe6" . "9e8159697c90" => base64_decode(""), "_e4543e131c67c631b147" . "51cc849d1310" => base64_decode("dX" . "Nl" . "cn" . "M="), "_2516886eb97" . "00de90ff5180" . "e49aa933f" => base64_decode("c3VybmFtZQ" . "=="), "_d54740b97424f9754a" . "3e5edec679cbe8" => base64_decode("aC51c2" . "VyX2lk" . "ID0gdS" . "5pZA=="), "i" . "d" => base64_decode("aWQ" . "="), "_18df014c6ae7e0de20cf" . "277dc0506335" => base64_decode("ZGF0Z" . "TE="));
use Claromentis\Core\Security\SecurityContext;
use Claromentis\Core\DAL;
use FBForm;
use HDIssue;
use HDProject;
use ICFieldPermsChecker;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
use Webmozart\Assert\Assert;







class IssueHistoryRepository implements LoggerAwareInterface
{
    use LoggerAwareTrait;
    protected DAL\Interfaces\DbInterface $database;
    protected FormRepository $formRepository;
    protected ProjectRepository $projectRepository;
    
    
    
    
    
    
    protected array $fieldPermissionCheckerCache = [];
    public function __construct(DAL\Interfaces\DbInterface $_4504853ce5094b088c8bdc3d570a0a5d, FormRepository $_2a8eab9adcccd98c59e9bc1dd3326bb7, ProjectRepository $_9375ba1d91fdf6590ddb3a4874c783d8)
    {
        $this->database = $_4504853ce5094b088c8bdc3d570a0a5d;
        $this->projectRepository = $_9375ba1d91fdf6590ddb3a4874c783d8;
        $this->formRepository = $_2a8eab9adcccd98c59e9bc1dd3326bb7;
        $this->logger = new NullLogger();
    }
    
    
    
    
    
    
    
    
    
    
    public function getIssueHistory(SecurityContext $context, HDProject $project, HDIssue $issue) : array
    {
        return $this->getHistoryForIssues($context, $project, [$issue])[(int) $issue->GetProperty($GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["i" . "d"])] ?? [];
    }
    
    
    
    
    
    
    
    
    
    public function getHistoryForIssues(SecurityContext $context, array $issues) : array
    {
        Assert::allIsInstanceOf($issues, \HDIssue::class);
        $_8e0ce41b9e1fa25e238257fe368c5da0 = [];
        $_da0d73cd7917aee5b5e197470d0d5fd8 = [];
        $_552d23e660e1f7e50b2d0ece28383f07 = [];
        $_61fa5e90812d5dd0dbb8277c66c7c4e4 = $this->projectRepository->findProjectsForIssues($issues);
        $_65976ee34559dcb12bd6d5db68edf074 = $context->GetUserId();
        
        
        foreach ($issues as $issue) {
            $issueId = (int) $issue->GetProperty($GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["i" . "d"]);
            $project = $_61fa5e90812d5dd0dbb8277c66c7c4e4[$issueId] ?? null;
            if ($project && $project->acl->UserHasPermissionForIssue($context, $issue, HD_PERM_VIEW_HISTORY)) {
                $_da0d73cd7917aee5b5e197470d0d5fd8[] = $issueId;
                $_552d23e660e1f7e50b2d0ece28383f07[$issueId] = $issue;
            }
        }
        if (empty($_da0d73cd7917aee5b5e197470d0d5fd8)) {
            return $_8e0ce41b9e1fa25e238257fe368c5da0;
        }
        
        
        $_30f942154645a3deca356c5cbdef7313 = new DAL\QueryBuilder($GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_b7928b5c7" . "8f330e3cca" . "d7afb9bbbf" . "2b0"]);
        $_30f942154645a3deca356c5cbdef7313->AddJoin($GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_307e0f5786e72e5fa9" . "22896fcbdfae03"], $GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_e4543e131c67c631b14751cc849" . "d1310"], $GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_040eaefa66" . "260cc2eaeeb" . "f3fb7e20ed1"], $GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_d54740b97424f9754a3e5ede" . "c679cbe8"]);
        $_30f942154645a3deca356c5cbdef7313->AddWhereAndClause($GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_74a64e2cc" . "f03f2b75d3" . "1908c6f140" . "fad"], $_da0d73cd7917aee5b5e197470d0d5fd8);
        $_1f24efd4735b7a11b391147435e7d35d = $this->database->query($_30f942154645a3deca356c5cbdef7313->GetQuery());
        
        
        while ($row = $_1f24efd4735b7a11b391147435e7d35d->fetchArray()) {
            $issueId = (int) $row[$GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_39638557be" . "821c8556d28" . "efa25c94ff1"]];
            $issue = $_552d23e660e1f7e50b2d0ece28383f07[$issueId] ?? null;
            try {
                $form = $this->formRepository->getIssueForm($issue);
            } catch (\Throwable $throwable) {
                $this->logger->warning($GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_926578877d761963375f967d" . "c4f578d7"] . $issueId . $GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_74d29cb83677a2205e6a7ced95" . "5e7e7e"] . $throwable->getMessage());
                $this->logger->debug($throwable);
                continue;
            }
            $project = $_61fa5e90812d5dd0dbb8277c66c7c4e4[$issueId] ?? null;
            if (!$project) {
                $this->logger->warning($GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_5de2842b" . "2a047fb10" . "ad57b3264" . "7a5164"] . $issueId . $GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_ed3e1aa0ac98bd062e" . "332a176b59d4f0"]);
                continue;
            }
            
            
            $_ed3c4d6acf46ce8acad849e22f58a5ed = $this->getFieldPermissionChecker($project)->SetUserId($_65976ee34559dcb12bd6d5db68edf074)->SetIssue($issue)->SetForm($form);
            $symname = $row[$GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_083135d58cb" . "a2106d77bbb7" . "2da5a4a2d"]] ?? $GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_4d707fab93bebb00bbe" . "69e8159697c90"];
            if ($_ed3c4d6acf46ce8acad849e22f58a5ed->CanView($symname)) {
                
                
                $_8e0ce41b9e1fa25e238257fe368c5da0[$issueId][(int) $row[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["i" . "d"]]] = $this->formatIssueHistoryEntry($row);
            }
        }
        return $_8e0ce41b9e1fa25e238257fe368c5da0;
    }
    
    
    
    
    
    
    
    protected function formatIssueHistoryEntry(array $row) : array
    {
        return [$GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["i" . "d"] => (int) ($row[$GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["i" . "d"]] ?? 0), HD_HISTORY_USER => $row[$GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["us" . "er" . "_i" . "d"]] ?? 0, HD_HISTORY_DATE => $row[$GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_18df014" . "c6ae7e0d" . "e20cf277" . "dc050633" . "5"]] ?? $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_4d707fa" . "b93bebb0" . "0bbe69e8" . "159697c9" . "0"], HD_HISTORY_FIELD => $row[$GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_13d133c615714d50" . "832f54e91fea8fb0"]] ?? $GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_4d707fab93bebb00bbe69e815" . "9697c90"], HD_HISTORY_CHANGES => $row[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_6be7ef0f685c2a96fcae1fc2c" . "cc30198"]] ?? $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_4d707fab93bebb00bbe69" . "e8159697c90"], HD_HISTORY_USER_NAME => $GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_3b1c56066ee613" . "4b4364b618bca37" . "3d6"]($row[$GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_b2a1616419" . "a412c872af9" . "07a317ff4b8"]] ?? $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_4d707fab93bebb00bbe" . "69e8159697c90"], $row[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_2516886eb970" . "0de90ff5180e4" . "9aa933f"]] ?? $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_4d707fab93bebb00bbe69e8" . "159697c90"]), HD_HISTORY_FIELD_SYMNAME => $row[$GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_083135d58cba2106d77bbb72da5" . "a4a2d"]] ?? $GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_4d707fab93bebb0" . "0bbe69e8159697c9" . "0"]];
    }
    
    
    
    
    
    
    
    public function getFieldPermissionChecker(\HDProject $project) : ICFieldPermsChecker
    {
        if (!isset($this->fieldPermissionCheckerCache[$project->GetId()])) {
            $this->fieldPermissionCheckerCache[$project->GetId()] = new ICFieldPermsChecker($project);
        }
        return $this->fieldPermissionCheckerCache[$project->GetId()];
    }
}