"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getClient = exports.getEndpointParsed = void 0;
const client_s3_1 = require("@aws-sdk/client-s3");
const node_http_handler_1 = require("@aws-sdk/node-http-handler");
const logger_1 = require("../../../helpers/logger");
const proxy_1 = require("../../../helpers/proxy");
const requests_1 = require("../../../helpers/requests");
const config_1 = require("../../../initializers/config");
const logger_2 = require("./logger");
function getProxyRequestHandler() {
    if (!(0, proxy_1.isProxyEnabled)())
        return null;
    const { agent } = (0, requests_1.getAgent)();
    return new node_http_handler_1.NodeHttpHandler({
        httpAgent: agent.http,
        httpsAgent: agent.https
    });
}
let endpointParsed;
function getEndpointParsed() {
    if (endpointParsed)
        return endpointParsed;
    endpointParsed = new URL(getEndpoint());
    return endpointParsed;
}
exports.getEndpointParsed = getEndpointParsed;
let s3Client;
function getClient() {
    if (s3Client)
        return s3Client;
    const OBJECT_STORAGE = config_1.CONFIG.OBJECT_STORAGE;
    s3Client = new client_s3_1.S3Client({
        endpoint: getEndpoint(),
        region: OBJECT_STORAGE.REGION,
        credentials: OBJECT_STORAGE.CREDENTIALS.ACCESS_KEY_ID
            ? {
                accessKeyId: OBJECT_STORAGE.CREDENTIALS.ACCESS_KEY_ID,
                secretAccessKey: OBJECT_STORAGE.CREDENTIALS.SECRET_ACCESS_KEY
            }
            : undefined,
        requestHandler: getProxyRequestHandler()
    });
    logger_1.logger.info('Initialized S3 client %s with region %s.', getEndpoint(), OBJECT_STORAGE.REGION, (0, logger_2.lTags)());
    return s3Client;
}
exports.getClient = getClient;
let endpoint;
function getEndpoint() {
    if (endpoint)
        return endpoint;
    const endpointConfig = config_1.CONFIG.OBJECT_STORAGE.ENDPOINT;
    endpoint = endpointConfig.startsWith('http://') || endpointConfig.startsWith('https://')
        ? config_1.CONFIG.OBJECT_STORAGE.ENDPOINT
        : 'https://' + config_1.CONFIG.OBJECT_STORAGE.ENDPOINT;
    return endpoint;
}
//# sourceMappingURL=client.js.map