<?php
namespace Claromentis\Search\Indexing\Client;

$GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"] ?: array()) + array("_a591754a8ddcdbda9c" . "80f1db4d911e14" => base64_decode("aXNf" . "bnVs" . "bA=="), "_f50e845669e7cd06b" . "bc643eeb57aa01d" => base64_decode("Ym9" . "keQ" . "=="), "d" . "a" . "t" . "a" => base64_decode("ZGF0YQ=" . "="), "_d447517cc97224c765e91831" . "63fe5040" => base64_decode("L" . "Q" . "=" . "="), "_5159b3572969eca46acd1b2e1418" . "459c" => base64_decode("aW1wbG9kZ" . "Q=="), "_1ac5cc48c34c5362e94" . "d8667cc7b0ddc" => base64_decode("LA" . "=="), "_38f12d8b8c719a8ce0fc2faef" . "c225f35" => base64_decode("YXJyYXl" . "fbWFw"), "i" . "d" => base64_decode("aW" . "Q="), "_8b95086af12ea3e79bb13" . "14de0bdd082" => base64_decode("JyB3aXRob3V0IHByb" . "3ZpZGluZyBhbiBJRA" . "=="), "_f96f38232c31532a01eb2" . "745c51d63cc" => base64_decode("cGlwZWxpb" . "mU="), "_02ad4799244951c2d" . "809ee37be9e79fd" => base64_decode("aW5kZ" . "Xg="), "_6e6ffa5e4cef62" . "064ccc78bdf74a2" . "03b" => base64_decode("Q2Fubm90IGRlbG" . "V0ZSBmcm9tIHNl" . "YXJjaCBpbmRleC" . "B0eXBlICc="), "_8e89de354d907374" . "1e3902d87bf99b68" => base64_decode("RXhjZXB0aW9uOiA" . "="), "_4e7ab34a53ffb13eb0838003f3303" . "4d4" => base64_decode("YXR0" . "YWNo" . "bWVu" . "dHM="), "_09f61d3133f39f8e5aff0e" . "b6a7d185a1" => base64_decode("Z" . "G" . "9" . "j"));
use Claromentis\Search\Indexing\Client\ClientInterface;
use Claromentis\Search\Pipeline;
use Claromentis\Search\ItemTypeFactory;
use Elastic\Elasticsearch\Client;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Exception;
class ElasticsearchClient implements ClientInterface
{
    use LoggerAwareTrait;
    public const ATTACHMENT_PIPELINE = 'attachment';
    
    
    
    
    public const ATTACHMENT_PIPELINE_DEF = ['id' => 'attachment', "body" => ["processors" => [["attachment" => ["field" => "attachment.data", 'target_field' => 'attachment.info', "if" => "ctx.attachment != null"], "foreach" => ["field" => "attachments", "processor" => ["attachment" => ["target_field" => "_ingest._value.info", "field" => "_ingest._value.data"]], "if" => "ctx.attachments != null"]]]]];
    public const DEFAULT_INDEX_PARAMS = ["settings" => ['analysis' => ['analyzer' => ['filename' => ["type" => "custom", "tokenizer" => "whitespace", "filter" => ["filename_filter", 'file_extension_filter', "lowercase"]]], 'filter' => ['file_extension_filter' => ['type' => 'pattern_capture', 'preserve_original' => true, 'patterns' => [
        "(.*)\\.(?:.*)",
        
        
        "([^\\-_.]+)",
    ]], 'filename_filter' => ['type' => 'word_delimiter', 'preserve_original' => true]]]]];
    private Client $client;
    private ItemTypeFactory $itemTypeFactory;
    private string $indexName;
    private ?Pipeline $mappingPipeline;
    private ?Pipeline $indexingPipeline;
    
    
    
    
    
    
    
    
    
    
    
    public function __construct(Client $client, ItemTypeFactory $_b67b64785d0a0ac027d9e8975752a825, string $_7186b8dab67584fd6c4e5b17bfa3c287, ?Pipeline $_dbe0fd7338f962e46040f518d4c7744b, ?Pipeline $_88f6cb3cd1fcb832b0d337b4ee3f1ca5, ?LoggerInterface $logger = null)
    {
        $this->client = $client;
        $this->itemTypeFactory = $_b67b64785d0a0ac027d9e8975752a825;
        $this->indexName = $_7186b8dab67584fd6c4e5b17bfa3c287;
        $this->mappingPipeline = $_dbe0fd7338f962e46040f518d4c7744b;
        $this->indexingPipeline = $_88f6cb3cd1fcb832b0d337b4ee3f1ca5;
        $this->setLogger($logger ?? new NullLogger());
    }
    
    
    
    
    
    public function CreateIndex($params = [])
    {
        $_2e5fd271fda00a16d3201b16d65f393d = $this->itemTypeFactory->GetAllIndexingTypes();
        foreach ($_2e5fd271fda00a16d3201b16d65f393d as $_02ad4799244951c2d809ee37be9e79fd) {
            $_7186b8dab67584fd6c4e5b17bfa3c287 = $this->GetIndex($this->indexName, $_02ad4799244951c2d809ee37be9e79fd);
            if ($this->IndexExists($_7186b8dab67584fd6c4e5b17bfa3c287)) {
                $this->client->indices()->delete([$GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["_02ad4799244951c2d809ee37be9e79f" . "d"] => $_7186b8dab67584fd6c4e5b17bfa3c287]);
            }
        }
        try {
            $this->client->ingest()->getPipeline([$GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["i" . "d"] => self::ATTACHMENT_PIPELINE]);
        } catch (Exception $e) {
            $this->client->ingest()->putPipeline(self::ATTACHMENT_PIPELINE_DEF);
        }
    }
    
    
    
    
    
    
    public function GetIndex(string $_02ad4799244951c2d809ee37be9e79fd, ?string $type = null) : string
    {
        if (!empty($type)) {
            $_02ad4799244951c2d809ee37be9e79fd .= $GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_d447517cc9" . "7224c765e91" . "83163fe5040"] . $type;
        } else {
            $_02ad4799244951c2d809ee37be9e79fd = $this->GetJoinedIndexes();
        }
        return $_02ad4799244951c2d809ee37be9e79fd;
    }
    
    
    
    
    
    protected function IndexExists(string $_02ad4799244951c2d809ee37be9e79fd) : bool
    {
        $response = $this->client->indices()->exists([$GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_02ad4799244951c2d809ee37b" . "e9e79fd"] => $_02ad4799244951c2d809ee37be9e79fd]);
        return $response->asBool();
    }
    
    
    
    
    
    
    public function Delete($type, $_659d58952a47b6b120f3d1d57caf3476)
    {
        $params = $this->FillParams($type, $_659d58952a47b6b120f3d1d57caf3476, []);
        if (empty($params[$GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["i" . "d"]])) {
            $this->logger->error($GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_6e6ffa5e4c" . "ef62064ccc7" . "8bdf74a203b"] . $type . $GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_8b95086af12ea3e79bb1314de0bdd08" . "2"]);
            return false;
        }
        try {
            $this->client->delete($params);
        } catch (Exception $e) {
            $this->logger->error($GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_8e89de354d9073741e390" . "2d87bf99b68"] . $e->getMessage(), [$e, $e->getTrace()]);
            return false;
        }
    }
    
    
    
    
    
    
    
    
    public function Index($type, $_659d58952a47b6b120f3d1d57caf3476, $_572ecbe92dff0ba7eb2a689f8c0b1436, $params = [])
    {
        if (isset($_572ecbe92dff0ba7eb2a689f8c0b1436[$GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_4e7ab34a53ffb13eb0838003f3" . "3034d4"]])) {
            $params[$GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_f96f38232c31532a01eb2745c51d63c" . "c"]] = self::ATTACHMENT_PIPELINE;
        }
        if (isset($_572ecbe92dff0ba7eb2a689f8c0b1436[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_4e7ab34a53ffb13eb08" . "38003f33034d4"]][$GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["da" . "ta"]])) {
            $_572ecbe92dff0ba7eb2a689f8c0b1436[$GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_4e7ab34a" . "53ffb13eb" . "0838003f3" . "3034d4"]] = [$_572ecbe92dff0ba7eb2a689f8c0b1436[$GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_4e7ab34a" . "53ffb13eb" . "0838003f3" . "3034d4"]]];
        }
        
        
        try {
            $params = $this->FillParams($type, $_659d58952a47b6b120f3d1d57caf3476, $params);
            $params[$GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_02ad4799244951c2d809ee37b" . "e9e79fd"]] = $this->GetIndex($this->indexName, $type);
            $params[$GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_f50e845669e7cd06bbc643eeb57aa01" . "d"]] = $this->indexingPipeline ? $this->indexingPipeline->handle($type, $_572ecbe92dff0ba7eb2a689f8c0b1436) : $_572ecbe92dff0ba7eb2a689f8c0b1436;
            return $this->client->index($params)->asArray();
        } catch (Exception $e) {
            $this->logger->error($GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_8e89de354d9073741e3902d87bf9" . "9b68"] . $e->getMessage(), [$e, $e->getTrace()]);
            return false;
        }
    }
    
    
    
    
    
    
    public function AddMapping($type, $params)
    {
        $_02ad4799244951c2d809ee37be9e79fd = $this->GetIndex($this->indexName, $type);
        if ($this->IndexExists($_02ad4799244951c2d809ee37be9e79fd)) {
            $this->client->indices()->delete([$GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_02ad4799244951c" . "2d809ee37be9e79f" . "d"] => $_02ad4799244951c2d809ee37be9e79fd]);
        }
        $this->client->indices()->create([$GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_02ad4799244951c2d809ee37be9" . "e79fd"] => $_02ad4799244951c2d809ee37be9e79fd, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_f50e845669e7cd0" . "6bbc643eeb57aa01" . "d"] => self::DEFAULT_INDEX_PARAMS]);
        $this->client->indices()->putMapping([$GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_02ad4799244951c2d809ee37be9e79f" . "d"] => $_02ad4799244951c2d809ee37be9e79fd, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_f50e845669e7cd0" . "6bbc643eeb57aa01" . "d"] => $this->mappingPipeline ? $this->mappingPipeline->handle($type, $params) : $params]);
    }
    
    
    
    
    
    
    public function DeleteByQuery($type, $query)
    {
        try {
            $params = [$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_02ad4799244951c2d" . "809ee37be9e79fd"] => $this->GetIndex($this->indexName, $type), $GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_f50e845669e7cd06bbc643eeb57" . "aa01d"] => $query];
            $this->client->deleteByQuery($params);
        } catch (Exception $e) {
            $this->logger->error($GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_8e89de354d907374" . "1e3902d87bf99b68"] . $e->getMessage(), [$e, $e->getTrace()]);
            return false;
        }
    }
    
    
    
    
    
    
    
    
    public function Update($type, $_659d58952a47b6b120f3d1d57caf3476, $_572ecbe92dff0ba7eb2a689f8c0b1436, $params = [])
    {
        try {
            $_5c5b1fc550a4fed78ee55566db3f75cd = $this->FillParams($type, $_659d58952a47b6b120f3d1d57caf3476, $params);
            if (!$GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_a591754a8ddcdbda9c80f1" . "db4d911e14"]($_659d58952a47b6b120f3d1d57caf3476)) {
                $_5c5b1fc550a4fed78ee55566db3f75cd[$GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["i" . "d"]] = $_659d58952a47b6b120f3d1d57caf3476;
            }
            $_5c5b1fc550a4fed78ee55566db3f75cd[$GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_f50e845669e7cd06bbc643eeb57" . "aa01d"]] = [$GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_09f61d3133f39f8e" . "5aff0eb6a7d185a1"] => $_572ecbe92dff0ba7eb2a689f8c0b1436];
            return $this->client->update($params)->asArray();
        } catch (Exception $e) {
            $this->logger->error($GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_8e89de354d90737" . "41e3902d87bf99b6" . "8"] . $e->getMessage(), [$e, $e->getTrace()]);
            return false;
        }
    }
    
    
    
    
    
    
    
    private function FillParams($type, $_659d58952a47b6b120f3d1d57caf3476, array $params = []) : array
    {
        $params[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_02ad4799244951c2d809" . "ee37be9e79fd"]] = $this->GetIndex($this->indexName, $type);
        if (!$GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_a591754a8ddcdbda9c80f1d" . "b4d911e14"]($_659d58952a47b6b120f3d1d57caf3476)) {
            $params[$GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["i" . "d"]] = $_659d58952a47b6b120f3d1d57caf3476;
        }
        return $params;
    }
    
    
    
    
    
    
    
    
    
    private function GetJoinedIndexes(array $_2e5fd271fda00a16d3201b16d65f393d = []) : string
    {
        if (empty($_2e5fd271fda00a16d3201b16d65f393d)) {
            $_2e5fd271fda00a16d3201b16d65f393d = $this->itemTypeFactory->GetAllIndexingTypes();
        }
        $_7186b8dab67584fd6c4e5b17bfa3c287 = $this->indexName;
        return $GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_5159b3572969eca46" . "acd1b2e1418459c"]($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_1ac5cc48c34c5362e94d8667cc7b0" . "ddc"], $GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_38f12d8b8c719a8ce0fc2faefc" . "225f35"](function ($item) use($_7186b8dab67584fd6c4e5b17bfa3c287) {
            return $_7186b8dab67584fd6c4e5b17bfa3c287 . $GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_d447517cc97" . "224c765e9183" . "163fe5040"] . $item;
        }, $_2e5fd271fda00a16d3201b16d65f393d));
    }
}