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

$GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"] ?: array()) + array("_02ad4799244951c2d809e" . "e37be9e79fd" => base64_decode("aW5k" . "ZXg="), "_f96f38232" . "c31532a01e" . "b2745c51d6" . "3cc" => base64_decode("cGlwZWxpb" . "mU="), "_38f12d8b8c719a8ce0fc2f" . "aefc225f35" => base64_decode("YXJyYXlfbW" . "Fw"), "i" . "d" => base64_decode("a" . "W" . "Q" . "="), "_f50e845669e7cd06bbc643e" . "eb57aa01d" => base64_decode("Ym9ke" . "Q=="), "_8b95086af12ea" . "3e79bb1314de0b" . "dd082" => base64_decode("JyB3aXRob3V0IHByb3ZpZGluZyBhbiBJ" . "RA=="), "dat" . "a" => base64_decode("ZGF0YQ=" . "="), "_d447517cc97224c765e9183163fe50" . "40" => base64_decode("LQ=" . "="), "_1ac5cc48c34c5362e94" . "d8667cc7b0ddc" => base64_decode("L" . "A" . "=" . "="), "_4e7ab34a53ffb13eb0838003f33" . "034d4" => base64_decode("YXR0YW" . "NobWVu" . "dHM="), "_8e89de354d907" . "3741e3902d87bf" . "99b68" => base64_decode("RXhjZXB0aW9uOiA" . "="), "_09f61d3133f39f8e5a" . "ff0eb6a7d185a1" => base64_decode("ZG" . "9j"), "_a591754a8ddcdbda9c80" . "f1db4d911e14" => base64_decode("aXNf" . "bnVs" . "bA=="), "_5159b3572969eca46acd1b2e" . "1418459c" => base64_decode("aW1wbG9kZ" . "Q=="), "_6e6ffa5e4cef62064ccc78" . "bdf74a203b" => base64_decode("Q2Fubm90IGRlbGV0ZSBm" . "cm9tIHNlYXJjaCBpbmRl" . "eCB0eXBlICc="));
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["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_02ad4799244951c2d809" . "ee37be9e79fd"] => $_7186b8dab67584fd6c4e5b17bfa3c287]);
            }
        }
        try {
            $this->client->ingest()->getPipeline([$GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["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["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_d447517cc97224c765e9183163fe504" . "0"] . $type;
        } else {
            $_02ad4799244951c2d809ee37be9e79fd = $this->GetJoinedIndexes();
        }
        return $_02ad4799244951c2d809ee37be9e79fd;
    }
    
    
    
    
    
    protected function IndexExists(string $_02ad4799244951c2d809ee37be9e79fd) : bool
    {
        $response = $this->client->indices()->exists([$GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_02ad4799244951c2d8" . "09ee37be9e79fd"] => $_02ad4799244951c2d809ee37be9e79fd]);
        return $response->asBool();
    }
    
    
    
    
    
    
    public function Delete($type, $_659d58952a47b6b120f3d1d57caf3476)
    {
        $params = $this->FillParams($type, $_659d58952a47b6b120f3d1d57caf3476, []);
        if (empty($params[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["i" . "d"]])) {
            $this->logger->error($GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_6e6ffa5e4cef62064ccc7" . "8bdf74a203b"] . $type . $GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_8b95086af12ea3e79bb1314de0b" . "dd082"]);
            return false;
        }
        try {
            $this->client->delete($params);
        } catch (Exception $e) {
            $this->logger->error($GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_8e89de354d9" . "073741e3902d" . "87bf99b68"] . $e->getMessage(), [$e, $e->getTrace()]);
            return false;
        }
    }
    
    
    
    
    
    
    
    
    public function Index($type, $_659d58952a47b6b120f3d1d57caf3476, $_572ecbe92dff0ba7eb2a689f8c0b1436, $params = [])
    {
        if (isset($_572ecbe92dff0ba7eb2a689f8c0b1436[$GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_4e7ab34a53ffb13eb083" . "8003f33034d4"]])) {
            $params[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_f96f38232c31532a01eb2745" . "c51d63cc"]] = self::ATTACHMENT_PIPELINE;
        }
        if (isset($_572ecbe92dff0ba7eb2a689f8c0b1436[$GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_4e7ab34a53ff" . "b13eb0838003f" . "33034d4"]][$GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["d" . "a" . "t" . "a"]])) {
            $_572ecbe92dff0ba7eb2a689f8c0b1436[$GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_4e7ab34" . "a53ffb13" . "eb083800" . "3f33034d" . "4"]] = [$_572ecbe92dff0ba7eb2a689f8c0b1436[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_4e7ab34a53ffb13eb0838003f" . "33034d4"]]];
        }
        
        
        try {
            $params = $this->FillParams($type, $_659d58952a47b6b120f3d1d57caf3476, $params);
            $params[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_02ad4799244951c2d8" . "09ee37be9e79fd"]] = $this->GetIndex($this->indexName, $type);
            $params[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_f50e845669e7cd06bb" . "c643eeb57aa01d"]] = $this->indexingPipeline ? $this->indexingPipeline->handle($type, $_572ecbe92dff0ba7eb2a689f8c0b1436) : $_572ecbe92dff0ba7eb2a689f8c0b1436;
            return $this->client->index($params)->asArray();
        } catch (Exception $e) {
            $this->logger->error($GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_8e89de354d9073741" . "e3902d87bf99b68"] . $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["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_02ad47992" . "44951c2d80" . "9ee37be9e7" . "9fd"] => $_02ad4799244951c2d809ee37be9e79fd]);
        }
        $this->client->indices()->create([$GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_02ad4799244951c2d8" . "09ee37be9e79fd"] => $_02ad4799244951c2d809ee37be9e79fd, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_f50e845669e7" . "cd06bbc643eeb" . "57aa01d"] => self::DEFAULT_INDEX_PARAMS]);
        $this->client->indices()->putMapping([$GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_02ad4799244951c2d" . "809ee37be9e79fd"] => $_02ad4799244951c2d809ee37be9e79fd, $GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["_f50e845" . "669e7cd0" . "6bbc643e" . "eb57aa01" . "d"] => $this->mappingPipeline ? $this->mappingPipeline->handle($type, $params) : $params]);
    }
    
    
    
    
    
    
    public function DeleteByQuery($type, $query)
    {
        try {
            $params = [$GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_02ad4799244951c2d809ee37be9e79" . "fd"] => $this->GetIndex($this->indexName, $type), $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_f50e845669e7cd06bbc643eeb57" . "aa01d"] => $query];
            $this->client->deleteByQuery($params);
        } catch (Exception $e) {
            $this->logger->error($GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_8e89de3" . "54d90737" . "41e3902d" . "87bf99b6" . "8"] . $e->getMessage(), [$e, $e->getTrace()]);
            return false;
        }
    }
    
    
    
    
    
    
    
    
    public function Update($type, $_659d58952a47b6b120f3d1d57caf3476, $_572ecbe92dff0ba7eb2a689f8c0b1436, $params = [])
    {
        try {
            $_5c5b1fc550a4fed78ee55566db3f75cd = $this->FillParams($type, $_659d58952a47b6b120f3d1d57caf3476, $params);
            if (!$GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_a591754a8ddcdbd" . "a9c80f1db4d911e1" . "4"]($_659d58952a47b6b120f3d1d57caf3476)) {
                $_5c5b1fc550a4fed78ee55566db3f75cd[$GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["i" . "d"]] = $_659d58952a47b6b120f3d1d57caf3476;
            }
            $_5c5b1fc550a4fed78ee55566db3f75cd[$GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_f50e845" . "669e7cd0" . "6bbc643e" . "eb57aa01" . "d"]] = [$GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_09f61d3133" . "f39f8e5aff0" . "eb6a7d185a1"] => $_572ecbe92dff0ba7eb2a689f8c0b1436];
            return $this->client->update($params)->asArray();
        } catch (Exception $e) {
            $this->logger->error($GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_8e89de354d9073741e3902d87bf99b6" . "8"] . $e->getMessage(), [$e, $e->getTrace()]);
            return false;
        }
    }
    
    
    
    
    
    
    
    private function FillParams($type, $_659d58952a47b6b120f3d1d57caf3476, array $params = []) : array
    {
        $params[$GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_02ad4799" . "244951c2d" . "809ee37be" . "9e79fd"]] = $this->GetIndex($this->indexName, $type);
        if (!$GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_a591754a8ddcd" . "bda9c80f1db4d9" . "11e14"]($_659d58952a47b6b120f3d1d57caf3476)) {
            $params[$GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["i" . "d"]] = $_659d58952a47b6b120f3d1d57caf3476;
        }
        return $params;
    }
    
    
    
    
    
    
    
    
    
    private function GetJoinedIndexes(array $_2e5fd271fda00a16d3201b16d65f393d = []) : string
    {
        if (empty($_2e5fd271fda00a16d3201b16d65f393d)) {
            $_2e5fd271fda00a16d3201b16d65f393d = $this->itemTypeFactory->GetAllIndexingTypes();
        }
        $_7186b8dab67584fd6c4e5b17bfa3c287 = $this->indexName;
        return $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_5159b3572969eca46ac" . "d1b2e1418459c"]($GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_1ac5cc48c34" . "c5362e94d866" . "7cc7b0ddc"], $GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_38f12d8b8c719a8ce0fc2fa" . "efc225f35"](function ($item) use($_7186b8dab67584fd6c4e5b17bfa3c287) {
            return $_7186b8dab67584fd6c4e5b17bfa3c287 . $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_d447517cc97224c765e" . "9183163fe5040"] . $item;
        }, $_2e5fd271fda00a16d3201b16d65f393d));
    }
}