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

$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"] ?: array()) + array("_02ad4799244951c2d80" . "9ee37be9e79fd" => base64_decode("aW5kZXg" . "="), "_38f12d8b8c719a8" . "ce0fc2faefc225f3" . "5" => base64_decode("YXJyYX" . "lfbWFw"), "_f96f38232c31532a01eb2745c" . "51d63cc" => base64_decode("cGlwZWxpb" . "mU="), "_5159b357296" . "9eca46acd1b2" . "e1418459c" => base64_decode("aW1wbG" . "9kZQ=="), "i" . "d" => base64_decode("aWQ" . "="), "_6e6ffa5e4cef6" . "2064ccc78bdf74" . "a203b" => base64_decode("Q2Fubm90IGRlbGV0ZSBmcm9tIHNlYXJjaCBpb" . "mRleCB0eXBlICc="), "_f50e845669e7" . "cd06bbc643eeb" . "57aa01d" => base64_decode("Ym" . "9k" . "eQ" . "=="), "_8b95086af12ea3" . "e79bb1314de0bdd" . "082" => base64_decode("JyB3aXRob" . "3V0IHByb3" . "ZpZGluZyB" . "hbiBJRA=="), "_8e89de354d9" . "073741e3902d" . "87bf99b68" => base64_decode("RXhjZXB0aW9uOi" . "A="), "_d447517cc97224c765e9" . "183163fe5040" => base64_decode("LQ" . "=="), "_a591754a8ddcdbda9c80f1d" . "b4d911e14" => base64_decode("aXNf" . "bnVs" . "bA=="), "da" . "ta" => base64_decode("ZGF" . "0YQ" . "=="), "_09f61d3133f39f8e5aff0eb6a7d" . "185a1" => base64_decode("ZG9" . "j"), "_1ac5cc48c34c5362e94d8667cc7b0" . "ddc" => base64_decode("LA=" . "="), "_4e7ab34a53ffb13eb0838003" . "f33034d4" => base64_decode("YXR0YWNobWVudH" . "M="));
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["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_02ad4799244951c2d809ee37be9" . "e79fd"] => $_7186b8dab67584fd6c4e5b17bfa3c287]);
            }
        }
        try {
            $this->client->ingest()->getPipeline([$GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["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["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_d447517cc97224c765e9183163fe50" . "40"] . $type;
        } else {
            $_02ad4799244951c2d809ee37be9e79fd = $this->GetJoinedIndexes();
        }
        return $_02ad4799244951c2d809ee37be9e79fd;
    }
    
    
    
    
    
    protected function IndexExists(string $_02ad4799244951c2d809ee37be9e79fd) : bool
    {
        $response = $this->client->indices()->exists([$GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_02ad4799244951c2d809ee37be9" . "e79fd"] => $_02ad4799244951c2d809ee37be9e79fd]);
        return $response->asBool();
    }
    
    
    
    
    
    
    public function Delete($type, $_659d58952a47b6b120f3d1d57caf3476)
    {
        $params = $this->FillParams($type, $_659d58952a47b6b120f3d1d57caf3476, []);
        if (empty($params[$GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["i" . "d"]])) {
            $this->logger->error($GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_6e6ffa5e4cef62" . "064ccc78bdf74a2" . "03b"] . $type . $GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_8b95086af12ea3e79bb1" . "314de0bdd082"]);
            return false;
        }
        try {
            $this->client->delete($params);
        } catch (Exception $e) {
            $this->logger->error($GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_8e89de354d90" . "73741e3902d87" . "bf99b68"] . $e->getMessage(), [$e, $e->getTrace()]);
            return false;
        }
    }
    
    
    
    
    
    
    
    
    public function Index($type, $_659d58952a47b6b120f3d1d57caf3476, $_572ecbe92dff0ba7eb2a689f8c0b1436, $params = [])
    {
        if (isset($_572ecbe92dff0ba7eb2a689f8c0b1436[$GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_4e7ab34a5" . "3ffb13eb08" . "38003f3303" . "4d4"]])) {
            $params[$GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_f96f38232c31532a01eb2745c51d6" . "3cc"]] = self::ATTACHMENT_PIPELINE;
        }
        if (isset($_572ecbe92dff0ba7eb2a689f8c0b1436[$GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_4e7ab34a53ffb13eb0" . "838003f33034d4"]][$GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["dat" . "a"]])) {
            $_572ecbe92dff0ba7eb2a689f8c0b1436[$GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_4e7ab34a53ffb13eb" . "0838003f33034d4"]] = [$_572ecbe92dff0ba7eb2a689f8c0b1436[$GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_4e7ab34a53ffb1" . "3eb0838003f3303" . "4d4"]]];
        }
        
        
        try {
            $params = $this->FillParams($type, $_659d58952a47b6b120f3d1d57caf3476, $params);
            $params[$GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_02ad4799244951c2d809ee37be9" . "e79fd"]] = $this->GetIndex($this->indexName, $type);
            $params[$GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_f50e845669e7cd06bbc643eeb57" . "aa01d"]] = $this->indexingPipeline ? $this->indexingPipeline->handle($type, $_572ecbe92dff0ba7eb2a689f8c0b1436) : $_572ecbe92dff0ba7eb2a689f8c0b1436;
            return $this->client->index($params)->asArray();
        } catch (Exception $e) {
            $this->logger->error($GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_8e89de354d9073" . "741e3902d87bf99" . "b68"] . $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["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_02ad4799244951c2d8" . "09ee37be9e79fd"] => $_02ad4799244951c2d809ee37be9e79fd]);
        }
        $this->client->indices()->create([$GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_02ad4799244951c2d8" . "09ee37be9e79fd"] => $_02ad4799244951c2d809ee37be9e79fd, $GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_f50e845669e7cd06b" . "bc643eeb57aa01d"] => self::DEFAULT_INDEX_PARAMS]);
        $this->client->indices()->putMapping([$GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_02ad479924" . "4951c2d809e" . "e37be9e79fd"] => $_02ad4799244951c2d809ee37be9e79fd, $GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_f50e845669e7cd06bbc643eeb57" . "aa01d"] => $this->mappingPipeline ? $this->mappingPipeline->handle($type, $params) : $params]);
    }
    
    
    
    
    
    
    public function DeleteByQuery($type, $query)
    {
        try {
            $params = [$GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_02ad4799244951c2d809ee37be" . "9e79fd"] => $this->GetIndex($this->indexName, $type), $GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_f50e845669e7cd06bbc643ee" . "b57aa01d"] => $query];
            $this->client->deleteByQuery($params);
        } catch (Exception $e) {
            $this->logger->error($GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_8e89de354d9073741e3902d87bf99b" . "68"] . $e->getMessage(), [$e, $e->getTrace()]);
            return false;
        }
    }
    
    
    
    
    
    
    
    
    public function Update($type, $_659d58952a47b6b120f3d1d57caf3476, $_572ecbe92dff0ba7eb2a689f8c0b1436, $params = [])
    {
        try {
            $_5c5b1fc550a4fed78ee55566db3f75cd = $this->FillParams($type, $_659d58952a47b6b120f3d1d57caf3476, $params);
            if (!$GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_a591754a8ddcdbda9c" . "80f1db4d911e14"]($_659d58952a47b6b120f3d1d57caf3476)) {
                $_5c5b1fc550a4fed78ee55566db3f75cd[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["i" . "d"]] = $_659d58952a47b6b120f3d1d57caf3476;
            }
            $_5c5b1fc550a4fed78ee55566db3f75cd[$GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_f50e845669e7cd06bbc643eeb57a" . "a01d"]] = [$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_09f61d313" . "3f39f8e5af" . "f0eb6a7d18" . "5a1"] => $_572ecbe92dff0ba7eb2a689f8c0b1436];
            return $this->client->update($params)->asArray();
        } catch (Exception $e) {
            $this->logger->error($GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_8e89de354d9073741e3902d87bf99" . "b68"] . $e->getMessage(), [$e, $e->getTrace()]);
            return false;
        }
    }
    
    
    
    
    
    
    
    private function FillParams($type, $_659d58952a47b6b120f3d1d57caf3476, array $params = []) : array
    {
        $params[$GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_02ad4799244951c2d809ee37be9e7" . "9fd"]] = $this->GetIndex($this->indexName, $type);
        if (!$GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_a591754a8ddcdbda9c80f" . "1db4d911e14"]($_659d58952a47b6b120f3d1d57caf3476)) {
            $params[$GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["i" . "d"]] = $_659d58952a47b6b120f3d1d57caf3476;
        }
        return $params;
    }
    
    
    
    
    
    
    
    
    
    private function GetJoinedIndexes(array $_2e5fd271fda00a16d3201b16d65f393d = []) : string
    {
        if (empty($_2e5fd271fda00a16d3201b16d65f393d)) {
            $_2e5fd271fda00a16d3201b16d65f393d = $this->itemTypeFactory->GetAllIndexingTypes();
        }
        $_7186b8dab67584fd6c4e5b17bfa3c287 = $this->indexName;
        return $GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_5159b3572969eca46acd1b2e" . "1418459c"]($GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_1ac5cc48" . "c34c5362e" . "94d8667cc" . "7b0ddc"], $GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_38f12d8" . "b8c719a8" . "ce0fc2fa" . "efc225f3" . "5"](function ($item) use($_7186b8dab67584fd6c4e5b17bfa3c287) {
            return $_7186b8dab67584fd6c4e5b17bfa3c287 . $GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_d447517cc97224c765" . "e9183163fe5040"] . $item;
        }, $_2e5fd271fda00a16d3201b16d65f393d));
    }
}