"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerManualSteps = registerManualSteps;
const promises_1 = require("node:readline/promises");
const node_process_1 = require("node:process");
function requirePage(ctx, op) {
    if (!ctx.ui || !ctx.ui.page) {
        throw new Error(`Missing UI page on context for op "${op}"`);
    }
    return ctx.ui.page;
}
function registerManualSteps(registry) {
    registry.register("ui.waitForManual", async (ctx, step) => {
        const page = requirePage(ctx, "ui.waitForManual");
        const inputConfig = (step.with ?? {});
        const message = inputConfig.message?.trim() || "Manual interaction required.";
        const prompt = inputConfig.prompt?.trim() || "Press Enter to continue...";
        const timeoutMs = inputConfig.timeoutMs;
        const bringToFront = inputConfig.bringToFront ?? true;
        if (ctx.ui?.isHeadless()) {
            throw new Error('Op "ui.waitForManual" requires headed mode. Re-run with --headless false.');
        }
        if (!node_process_1.stdin.isTTY || !node_process_1.stdout.isTTY) {
            throw new Error('Op "ui.waitForManual" requires an interactive terminal (TTY).');
        }
        if (bringToFront) {
            await page.bringToFront();
        }
        console.log("");
        console.log("════════ Manual Interaction Required ════════");
        console.log(message);
        console.log(`Scenario: ${ctx.scenarioId}`);
        console.log(`Current URL: ${page.url()}`);
        console.log(prompt);
        const rl = (0, promises_1.createInterface)({ input: node_process_1.stdin, output: node_process_1.stdout });
        try {
            await rl.question("");
        }
        finally {
            rl.close();
        }
        if (inputConfig.waitForSelector) {
            await page.waitForSelector(inputConfig.waitForSelector, {
                state: "visible",
                timeout: timeoutMs,
            });
        }
        if (inputConfig.waitForUrlEquals) {
            await page.waitForURL((url) => url.toString() === inputConfig.waitForUrlEquals, { timeout: timeoutMs });
        }
        if (inputConfig.waitForUrlContains) {
            await page.waitForURL((url) => url.toString().includes(String(inputConfig.waitForUrlContains)), { timeout: timeoutMs });
        }
    });
}
//# sourceMappingURL=manual.step.js.map