"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 });
exports.S3Service = void 0;
const client_s3_1 = require("@aws-sdk/client-s3");
const GetObjectCommand_1 = require("@aws-sdk/client-s3/commands/GetObjectCommand");
const DeleteObjectCommand_1 = require("@aws-sdk/client-s3/commands/DeleteObjectCommand");
const constants_1 = require("../../../constants");
class S3Header {
    constructor(header) {
        this.header = header;
    }
    getETag() {
        return this.header.ETag && this.header.ETag.replace(/"/g, "");
    }
}
class S3Service {
    constructor(client, options = {}) {
        this.client = client;
        this.options = options;
    }
    getHeader(key) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.options.bucketName) {
                throw new Error("Bucket name not configured");
            }
            if (!key) {
                throw new Error("Key is not provided");
            }
            try {
                const headOutput = yield this.client.send(new client_s3_1.HeadObjectCommand({
                    Bucket: this.options.bucketName,
                    Key: key
                }));
                return new S3Header(headOutput);
            }
            catch (e) {
                return new S3Header({ $metadata: {}, ETag: undefined });
            }
        });
    }
    deleteObject(key) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.options.bucketName) {
                throw new Error("Bucket name not configured");
            }
            if (!key) {
                throw new Error("Key is not provided");
            }
            yield this.client.send(new DeleteObjectCommand_1.DeleteObjectCommand({
                Key: key,
                Bucket: this.options.bucketName
            }));
        });
    }
    putObject(key, body, contentType, cacheControl) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.options.bucketName) {
                throw new Error("Bucket name not configured");
            }
            if (!body) {
                throw new Error("Data is not provided");
            }
            yield this.client.send(new client_s3_1.PutObjectCommand({
                Key: key,
                Body: body,
                Bucket: this.options.bucketName,
                ContentType: contentType,
                CacheControl: cacheControl !== null && cacheControl !== void 0 ? cacheControl : constants_1.SWR_CACHE_CONTROL_HEADER
            }));
        });
    }
    getObject(key) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.options.bucketName) {
                throw new Error("Bucket name not configured");
            }
            const data = new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
                const getObjectCommand = new GetObjectCommand_1.GetObjectCommand({
                    Key: key,
                    Bucket: this.options.bucketName
                });
                try {
                    const response = yield this.client.send(getObjectCommand);
                    const responseDataChunks = [];
                    response.Body.on("data", (chunk) => responseDataChunks.push(chunk));
                    response.Body.once("end", () => resolve(responseDataChunks.join("")));
                }
                catch (err) {
                    return reject(err);
                }
            }));
            return data;
        });
    }
}
exports.S3Service = S3Service;
