"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestimAssetsApi = void 0;
const node_fetch_1 = __importStar(require("node-fetch"));
const testimAuthApi_1 = require("./testimAuthApi");
const apiErrors_1 = require("./apiErrors");
const form_data_1 = __importDefault(require("form-data"));
class TestimAssetsApi {
    constructor(baseUrl = 'https://services.testim.io', testimAuthApi = new testimAuthApi_1.TestimAuthApi(baseUrl), bucket = 'test-result-artifacts', fetch = node_fetch_1.default) {
        this.baseUrl = baseUrl;
        this.testimAuthApi = testimAuthApi;
        this.bucket = bucket;
        this.fetch = fetch;
    }
    async getAsset({ projectId, pathInsideBucket, }) {
        const headers = new node_fetch_1.Headers();
        this.testimAuthApi.addAuthorizationHeader(headers);
        const response = await this.fetch(`${this.baseUrl}/storage/${this.bucket}/${projectId}/${pathInsideBucket}`, {
            headers,
        });
        if (response.status === 404) {
            throw new apiErrors_1.TestimNotFoundError(`File ${pathInsideBucket} not found in bucket ${this.bucket}`);
        }
        if (!response.ok) {
            throw new apiErrors_1.TestimApiError(new Error(await response.text()), 'failed to get resource', 'E_UPLOAD_REQUEST_FAILED');
        }
        return response;
    }
    async uploadAsset({ asset, projectId, pathInsideBucket, contentType, signal }) {
        const headers = new node_fetch_1.Headers();
        this.testimAuthApi.addAuthorizationHeader(headers);
        const data = new form_data_1.default();
        data.append('file', asset);
        data.append('projectId', projectId);
        if (contentType) {
            data.append('mimeType', contentType);
        }
        data.append('storageType', 'azure');
        data.append('name', pathInsideBucket);
        const response = await this.fetch(`${this.baseUrl}/storage/${this.bucket}/${projectId}/${pathInsideBucket}`, {
            headers,
            body: data,
            method: 'POST',
            signal: signal,
        });
        if (!response.ok) {
            const text = await response.text();
            throw new apiErrors_1.TestimApiError(new Error(text), response.statusText, 'E_UPLOAD_REQUEST_FAILED');
        }
    }
}
exports.TestimAssetsApi = TestimAssetsApi;
