"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");
const snowflake_promise_1 = require("snowflake-promise");
var SnowflakeModule;
(function (SnowflakeModule) {
    const SCHEMA = {
        docs: "https://developers.snowflake.com/",
        description: "Snowflake is a solution for data warehousing, data lakes, data engineering, data science, data application development, and securely sharing and consuming shared data.",
        friendlyName: "Snowflake",
        type: "Relational",
        datasource: {
            account: {
                type: "string",
                required: true,
            },
            username: {
                type: "string",
                required: true,
            },
            password: {
                type: "password",
                required: true,
            },
            warehouse: {
                type: "string",
                required: true,
            },
            database: {
                type: "string",
                required: true,
            },
            schema: {
                type: "string",
                required: true,
            },
        },
        query: {
            create: {
                type: datasource_1.QueryTypes.SQL,
            },
            read: {
                type: datasource_1.QueryTypes.SQL,
            },
            update: {
                type: datasource_1.QueryTypes.SQL,
            },
            delete: {
                type: datasource_1.QueryTypes.SQL,
            },
        },
    };
    class SnowflakeIntegration {
        constructor(config) {
            this.client = new snowflake_promise_1.Snowflake(config);
        }
        internalQuery(query) {
            return __awaiter(this, void 0, void 0, function* () {
                yield this.client.connect();
                try {
                    return yield this.client.execute(query.sql);
                }
                catch (err) {
                    throw (err === null || err === void 0 ? void 0 : err.message.split(":")[1]) || (err === null || err === void 0 ? void 0 : err.message);
                }
            });
        }
        create(query) {
            return __awaiter(this, void 0, void 0, function* () {
                return this.internalQuery(query);
            });
        }
        read(query) {
            return __awaiter(this, void 0, void 0, function* () {
                return this.internalQuery(query);
            });
        }
        update(query) {
            return __awaiter(this, void 0, void 0, function* () {
                return this.internalQuery(query);
            });
        }
        delete(query) {
            return __awaiter(this, void 0, void 0, function* () {
                return this.internalQuery(query);
            });
        }
    }
    module.exports = {
        schema: SCHEMA,
        integration: SnowflakeIntegration,
    };
})(SnowflakeModule || (SnowflakeModule = {}));
