"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getS3Client = getS3Client;
exports.parseS3Url = parseS3Url;
// Singleton S3 instance initialized on-demand.
const client_s3_1 = require("@aws-sdk/client-s3");
const url_1 = require("./url");
let s3Instance;
function getS3Client() {
    if (!s3Instance) {
        const endpoint = process.env.RENOVATE_X_S3_ENDPOINT;
        const forcePathStyle = process.env.RENOVATE_X_S3_PATH_STYLE;
        s3Instance = new client_s3_1.S3Client({
            ...(endpoint && { endpoint }),
            ...(forcePathStyle && { forcePathStyle: true }),
        });
    }
    return s3Instance;
}
function parseS3Url(rawUrl) {
    const parsedUrl = typeof rawUrl === 'string' ? (0, url_1.parseUrl)(rawUrl) : rawUrl;
    if (parsedUrl === null) {
        return null;
    }
    if (parsedUrl.protocol !== 's3:') {
        return null;
    }
    return {
        Bucket: parsedUrl.host,
        Key: parsedUrl.pathname.substring(1),
    };
}
//# sourceMappingURL=s3.js.map