"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 S3Module;
(function (S3Module) {
    const AWS = require("aws-sdk");
    const SCHEMA = {
        docs: "https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html",
        description: "Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance.",
        friendlyName: "Amazon S3",
        type: "Object store",
        datasource: {
            region: {
                type: "string",
                required: false,
                default: "us-east-1",
            },
            accessKeyId: {
                type: "password",
                required: true,
            },
            secretAccessKey: {
                type: "password",
                required: true,
            },
            endpoint: {
                type: "string",
                required: false,
            },
            signatureVersion: {
                type: "string",
                required: false,
                default: "v4",
            },
        },
        query: {
            read: {
                type: datasource_1.QueryTypes.FIELDS,
                fields: {
                    bucket: {
                        type: "string",
                        required: true,
                    },
                },
            },
        },
    };
    class S3Integration {
        constructor(config) {
            this.config = config;
            if (this.config.endpoint) {
                this.config.s3ForcePathStyle = true;
            }
            else {
                delete this.config.endpoint;
            }
            this.client = new AWS.S3(this.config);
        }
        read(query) {
            return __awaiter(this, void 0, void 0, function* () {
                const response = yield this.client
                    .listObjects({
                    Bucket: query.bucket,
                })
                    .promise();
                return response.Contents;
            });
        }
    }
    module.exports = {
        schema: SCHEMA,
        integration: S3Integration,
    };
})(S3Module || (S3Module = {}));
