"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.forAfterEachEndTest = exports.forAfterEachEndTestOwnGlobals = exports.forBeforeEachOwnGlobals = exports.forBeforeEachGivenPage = exports.registerJasmineReporterToGlobal = void 0;
const screenplay = __importStar(require("../../index"));
const consts_1 = require("../consts");
let endTest;
let originalPage;
function registerJasmineReporterToGlobal() {
    if (typeof jasmine === 'undefined') {
        throw new Error('this file is only expected to be jasmine jest setupFilesAfterEnv');
    }
    jasmine.getEnv().addReporter({
        specStarted: (result) => {
            jasmine.currentTest = result;
        },
        specDone: (result) => {
            jasmine.currentTest = result;
        },
    });
}
exports.registerJasmineReporterToGlobal = registerJasmineReporterToGlobal;
async function forBeforeEachGivenPage(page) {
    const currentTest = screenplay.getJasmineCurrentTest();
    let runId;
    if ('runId' in global) {
        runId = global.runId;
    }
    else {
        runId = consts_1.FALLBACK_RUN_ID;
    }
    const startTestParams = {
        runId,
        projectRoot: process.cwd(),
        fullName: currentTest.fullName,
        description: currentTest.description,
        fullSuitePath: currentTest.testPath,
    };
    return screenplay.attach({ page, startTestParams });
}
exports.forBeforeEachGivenPage = forBeforeEachGivenPage;
async function forBeforeEachOwnGlobals() {
    if (typeof page === 'undefined') {
        throw new Error('Global page is missing');
    }
    const currentTest = screenplay.getJasmineCurrentTest();
    let runId;
    if ('runId' in global) {
        runId = global.runId;
    }
    else {
        runId = consts_1.FALLBACK_RUN_ID;
    }
    const startTestParams = {
        runId,
        projectRoot: process.cwd(),
        fullName: currentTest.fullName,
        description: currentTest.description,
        fullSuitePath: currentTest.testPath,
    };
    if (typeof jestPuppeteer !== 'undefined') {
        await jestPuppeteer.resetPage();
    }
    const { page: wrappedPage, endTest: endTestLocal } = await screenplay.attach({
        page,
        startTestParams,
    });
    originalPage = page;
    endTest = endTestLocal;
    global.page = wrappedPage;
    global.endTest = endTest;
    if (page !== wrappedPage) {
        throw new Error('failed to set global page to be the wrapped page');
    }
}
exports.forBeforeEachOwnGlobals = forBeforeEachOwnGlobals;
async function forAfterEachEndTestOwnGlobals() {
    global.endTest(screenplay.getEndStatusFromJasmineJest());
    global.page = originalPage;
}
exports.forAfterEachEndTestOwnGlobals = forAfterEachEndTestOwnGlobals;
async function forAfterEachEndTest(localEndTest) {
    localEndTest(screenplay.getEndStatusFromJasmineJest());
}
exports.forAfterEachEndTest = forAfterEachEndTest;
