"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const datasource_1 = require("../definitions/datasource");
var ElasticsearchModule;
(function (ElasticsearchModule) {
    const { Client } = require("@elastic/elasticsearch");
    const SCHEMA = {
        docs: "https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/index.html",
        description: "Elasticsearch is a search engine based on the Lucene library. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents.",
        friendlyName: "ElasticSearch",
        type: "Non-relational",
        datasource: {
            url: {
                type: datasource_1.DatasourceFieldTypes.STRING,
                required: true,
                default: "http://localhost:9200",
            },
        },
        query: {
            create: {
                type: datasource_1.QueryTypes.FIELDS,
                customisable: true,
                fields: {
                    index: {
                        type: datasource_1.DatasourceFieldTypes.STRING,
                        required: true,
                    },
                },
            },
            read: {
                type: datasource_1.QueryTypes.FIELDS,
                customisable: true,
                fields: {
                    index: {
                        type: datasource_1.DatasourceFieldTypes.STRING,
                        required: true,
                    },
                },
            },
            update: {
                type: datasource_1.QueryTypes.FIELDS,
                customisable: true,
                fields: {
                    id: {
                        type: datasource_1.DatasourceFieldTypes.STRING,
                        required: true,
                    },
                    index: {
                        type: datasource_1.DatasourceFieldTypes.STRING,
                        required: true,
                    },
                },
            },
            delete: {
                type: datasource_1.QueryTypes.FIELDS,
                fields: {
                    index: {
                        type: datasource_1.DatasourceFieldTypes.STRING,
                        required: true,
                    },
                    id: {
                        type: datasource_1.DatasourceFieldTypes.STRING,
                        required: true,
                    },
                },
            },
        },
    };
    class ElasticSearchIntegration {
        constructor(config) {
            this.config = config;
            this.client = new Client({ node: config.url });
        }
        create(query) {
            return __awaiter(this, void 0, void 0, function* () {
                const { index, json } = query;
                try {
                    const result = yield this.client.index({
                        index,
                        body: json,
                    });
                    return result.body;
                }
                catch (err) {
                    console.error("Error writing to elasticsearch", err);
                    throw err;
                }
                finally {
                    yield this.client.close();
                }
            });
        }
        read(query) {
            return __awaiter(this, void 0, void 0, function* () {
                const { index, json } = query;
                try {
                    const result = yield this.client.search({
                        index: index,
                        body: json,
                    });
                    return result.body.hits.hits.map(({ _source }) => _source);
                }
                catch (err) {
                    console.error("Error querying elasticsearch", err);
                    throw err;
                }
                finally {
                    yield this.client.close();
                }
            });
        }
        update(query) {
            return __awaiter(this, void 0, void 0, function* () {
                const { id, index, json } = query;
                try {
                    const result = yield this.client.update({
                        id,
                        index,
                        body: json,
                    });
                    return result.body;
                }
                catch (err) {
                    console.error("Error querying elasticsearch", err);
                    throw err;
                }
                finally {
                    yield this.client.close();
                }
            });
        }
        delete(query) {
            return __awaiter(this, void 0, void 0, function* () {
                try {
                    const result = yield this.client.delete(query);
                    return result.body;
                }
                catch (err) {
                    console.error("Error deleting from elasticsearch", err);
                    throw err;
                }
                finally {
                    yield this.client.close();
                }
            });
        }
    }
    module.exports = {
        schema: SCHEMA,
        integration: ElasticSearchIntegration,
    };
})(ElasticsearchModule || (ElasticsearchModule = {}));
