"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.performBearerTokenAuthPostRequest = exports.performBasicAuthPostRequest = exports.generateRandomStringAsync = exports.captureFailureVideo = exports.captureScreenshot = void 0;
const test_1 = require("@playwright/test");
const crypto_1 = require("crypto");
const axios_1 = __importDefault(require("axios"));
const constants_1 = require("./utils/constants");
async function captureScreenshot(page, testInfo) {
    const screenshotPath = `test-results/screenshots/screenshot-${(0, crypto_1.randomUUID)()}.png`;
    await page.screenshot({
        path: screenshotPath,
        fullPage: true,
        timeout: 200000,
    });
    await testInfo.attach('screenshot', {
        path: screenshotPath,
        contentType: 'image/png',
    });
    testInfo.annotations.push({
        type: 'testrail_attachment',
        description: screenshotPath,
    });
}
exports.captureScreenshot = captureScreenshot;
async function captureFailureVideo(page, testInfo) {
    if (testInfo.status === 'failed') {
        const videoPath = `test-results/videos/video-${testInfo.title}-chromium.webm`;
        const video = await page.video();
        if (video) {
            await page.close(); // Close explicitly to unblock saveAs
            await video.saveAs(videoPath);
            await testInfo.attach('video', {
                path: videoPath,
                contentType: 'video/webm',
            });
            testInfo.annotations.push({
                type: 'testrail_attachment',
                description: videoPath,
            });
        }
    }
}
exports.captureFailureVideo = captureFailureVideo;
function generateRandomStringAsync(length) {
    return new Promise((resolve) => {
        setTimeout(() => {
            const alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            let result = '';
            for (let i = 0; i < length; i++) {
                const randomIndex = Math.floor(Math.random() * alphabet.length);
                result += alphabet.charAt(randomIndex);
            }
            resolve(result);
        }, 100);
    });
}
exports.generateRandomStringAsync = generateRandomStringAsync;
async function performBasicAuthPostRequest(requestUrl, username, password) {
    await (0, test_1.expect)(async () => {
        const authResponse = await axios_1.default.post(requestUrl, {
            auth: {
                username: username,
                password: password,
            },
        });
        (0, test_1.expect)(authResponse.status).toBe(200);
    }).toPass(constants_1.defaultAssertionOptions);
}
exports.performBasicAuthPostRequest = performBasicAuthPostRequest;
async function performBearerTokenAuthPostRequest(requestUrl, bearerToken) {
    try {
        const authToken = bearerToken;
        const apiUrl = requestUrl;
        const response = await axios_1.default.post(apiUrl, {
            headers: {
                Authorization: `Bearer ${authToken}`,
            },
        });
        if (response.status === 200) {
            console.log('Bearer Token API request successful.');
            console.log('Response Data:', response.data);
        }
        else {
            console.error('Bearer Token API request failed.');
        }
    }
    catch (error) {
        console.error('Bearer Token API request error:', error);
    }
}
exports.performBearerTokenAuthPostRequest = performBearerTokenAuthPostRequest;
