"use strict";
/**
 * Copyright: (c) Myia SAS 2026.
 * This file and its contents are licensed under the AGPLv3 License.
 * Please see the LICENSE file at the root of this repository
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.configureTracer = configureTracer;
exports.getTracerConfig = getTracerConfig;
exports.onTestTeardown = onTestTeardown;
exports.resetTeardownHooks = resetTeardownHooks;
exports.drainTeardownHooks = drainTeardownHooks;
const logger_1 = require("../../util/logger");
let currentConfig = {};
let teardownHooks = [];
/**
 * Register the tracer's extension config. Typically called once at
 * the top of `playwright.config.ts`, before `defineConfig(...)`. Later
 * calls overwrite earlier ones — there is no merge.
 */
function configureTracer(config) {
    currentConfig = config;
}
/**
 * Read the currently-registered config. Returns an empty object when
 * the user never called `configureTracer` — the fixture treats that
 * as "NDJSON-only, no bindings."
 */
function getTracerConfig() {
    return currentConfig;
}
/**
 * Register a function to run when the current test tears down. Runs
 * BEFORE user bindings' `stop()` so SDKs that use
 * `onTestTeardown(...)` still see any globals the bindings installed.
 *
 * Errors raised by a hook are logged to stderr and swallowed.
 */
function onTestTeardown(fn) {
    teardownHooks.push(fn);
}
/**
 * Internal. Clears the teardown-hook registry. The fixture calls this
 * at the start of every test to defend against a hook leaking across
 * test boundaries if a prior test crashed before drain ran.
 */
function resetTeardownHooks() {
    teardownHooks = [];
}
/**
 * Internal. Runs every registered teardown hook in registration order,
 * then clears the registry. Errors are logged and swallowed.
 */
async function drainTeardownHooks() {
    const hooks = teardownHooks;
    teardownHooks = [];
    for (const hook of hooks) {
        try {
            await hook();
        }
        catch (err) {
            logger_1.log.error('teardown hook failed', err);
        }
    }
}
