"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.attach = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const debug_1 = __importDefault(require("debug"));
const consoleLogsCollection_1 = require("./consoleLogsCollection");
const utils_1 = require("./utils");
const assortedHooks_1 = require("./assortedHooks");
const screenshotCollection_1 = require("./hooks/screenshotCollection");
const step_metadata_collection_1 = require("./hooks/step-metadata-collection");
const TestContext_1 = require("./TestContext");
const PuppeteerPageHooker_1 = require("./PuppeteerPageHooker");
const consts_1 = require("./consts");
const stacktraceHook_1 = require("./stacktraceHook");
const loggerDebug = debug_1.default('screenplay:debug');
async function attach({ page, startTestParams }, dateConstructor = Date) {
    const defaultTestName = dateConstructor.now().toString();
    const defaultStartTestParams = {
        runId: consts_1.FALLBACK_RUN_ID,
        fullName: `fullName ${defaultTestName}`,
        description: defaultTestName,
        fullSuitePath: 'pseudo fullSuitePath',
        projectRoot: process.cwd(),
    };
    const resolvedStartTestParams = Object.assign(defaultStartTestParams, startTestParams !== null && startTestParams !== void 0 ? startTestParams : {});
    const resultsDirFullPath = utils_1.testResultDirFromStartParams(resolvedStartTestParams);
    await fs_extra_1.default.mkdirp(resultsDirFullPath);
    const context = new TestContext_1.TestContext(resultsDirFullPath, resolvedStartTestParams.description, resolvedStartTestParams.fullName, dateConstructor);
    const instrumentor = new PuppeteerPageHooker_1.PuppeteerPageHooker(context, page);
    const hookedPage = instrumentor.wrapWithProxy(page);
    instrumentor.registerBeforeAllHook(assortedHooks_1.testSystemInfoHook);
    instrumentor.registerBeforeAllHook(consoleLogsCollection_1.logsBeforeAllHook);
    instrumentor.registerAfterAllHook(consoleLogsCollection_1.logsAfterAllHook);
    instrumentor.registerBeforeHook(stacktraceHook_1.stacktraceHook);
    instrumentor.registerBeforeHook(consoleLogsCollection_1.logsBeforeEachHook);
    instrumentor.registerAfterHook(consoleLogsCollection_1.logsAfterEachHook);
    instrumentor.registerAfterHook(assortedHooks_1.errorInStepHook);
    instrumentor.registerAfterHook(step_metadata_collection_1.puppeteerMetadata);
    instrumentor.registerBeforeHook(async (testContext, fnName, proxyContext) => {
        loggerDebug(`>>${proxyContext.constructor.name}>>${fnName}`);
    });
    instrumentor.registerBeforeHook(screenshotCollection_1.puppeteerScreenshot);
    instrumentor.registerAfterAllHook(assortedHooks_1.testEndHook);
    await instrumentor.start();
    async function endTest(endStatus) {
        await instrumentor.end(endStatus);
    }
    return {
        page: hookedPage,
        endTest,
    };
}
exports.attach = attach;
