"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestimExecutionsApi = void 0;
const testimAuthApi_1 = require("./testimAuthApi");
const node_fetch_1 = __importStar(require("node-fetch"));
const apiErrors_1 = require("./apiErrors");
const consts_1 = require("../consts");
class TestimExecutionsApi {
    constructor(baseUrl = consts_1.BASE_BACKEND_URL, testimAuthApi = new testimAuthApi_1.TestimAuthApi(baseUrl), fetch = node_fetch_1.default) {
        this.baseUrl = baseUrl;
        this.testimAuthApi = testimAuthApi;
        this.fetch = fetch;
    }
    async createExecution(execution, signal) {
        const headers = new node_fetch_1.Headers();
        headers.set('content-type', 'application/json');
        this.testimAuthApi.addAuthorizationHeader(headers);
        const response = await this.fetch(`${this.baseUrl}/result/run`, {
            method: 'POST',
            headers,
            signal: signal,
            body: JSON.stringify(execution),
        });
        if (!response.ok) {
            const body = await response.text();
            throw new apiErrors_1.TestimApiError(new Error(), `Error creating execution:${response.statusText}\n${body}`, 'E_CREATING_EXECUTION_FAILED');
        }
    }
}
exports.TestimExecutionsApi = TestimExecutionsApi;
