"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_fetch_1 = __importDefault(require("node-fetch"));
const logger_1 = __importDefault(require("./logger"));
const devOpsConnection_1 = require("./devOpsConnection");
const devOpsTestApi = async (host, token, project) => {
    const webApi = await (0, devOpsConnection_1.getWebApi)(host, token);
    const testApi = await webApi.getTestApi();
    const authorization = `Basic ${Buffer.from(`${token}:`).toString("base64")}`;
    return {
        getSuiteTestPoints: async (testPlanId, testSuiteId) => await testApi.getPoints(project, Number.parseInt(testPlanId, 10), testSuiteId),
        getTestConfiguration: async (configurationId) => {
            // first endpoint should work but we add alternatives to make sure we fetch the configuration
            const endpoints = [
                `${host}${project}/_apis/testplan/configurations/${configurationId}?api-version=7.1`,
                `${host}${project}/_apis/test/configurations/${configurationId}?api-version=7.1`,
            ];
            for (const endpoint of endpoints) {
                logger_1.default.info(`Getting test configuration from ${endpoint}`);
                const response = await (0, node_fetch_1.default)(endpoint, {
                    method: "GET",
                    headers: {
                        Authorization: authorization,
                        "Content-Type": "application/json",
                    },
                });
                if (response.ok) {
                    return (await response.json());
                }
                else {
                    logger_1.default.error(`Failed to get test configuration ${configurationId} from ${endpoint}: ${response.status} ${response.statusText}`);
                }
            }
            throw new Error(`Failed to get test configuration ${configurationId}: data not found`);
        },
        createTestRun: async (testRun) => await testApi.createTestRun(testRun, project),
        getTestRunResults: async (testRunId) => await testApi.getTestResults(project, testRunId),
        updateTestRunResult: async (testResults, testRunId) => await testApi.updateTestResults(testResults, project, testRunId),
        updateTestRun: async (testRunUpdate, testRunId) => await testApi.updateTestRun(testRunUpdate, project, testRunId),
    };
};
exports.default = devOpsTestApi;
//# sourceMappingURL=devOpsTestApi.js.map