import { createInterface } from "node:readline";
import { logger } from "../utils/logger.js";
/**
 * Prompt the user to manually paste an OAuth authorization code.
 *
 * Used as a fallback when the automatic OAuth callback server cannot bind
 * (e.g., port blocked by firewall, running in SSH session, etc.).
 *
 * Displays the auth URL for the user to visit manually, then reads the
 * authorization code from stdin.
 */
export async function manualCodeEntry(authUrl) {
    logger.info("Automatic callback server failed — falling back to manual mode");
    console.log("\nCould not start local callback server.");
    console.log("Please visit the following URL in your browser:\n");
    console.log(`  ${authUrl}\n`);
    console.log("After authorizing, copy the code from the page and paste it below.\n");
    const code = await promptForCode();
    if (!code) {
        throw new Error("No authorization code provided");
    }
    return code;
}
/**
 * Read a line from stdin with a prompt.
 * Returns the trimmed input.
 */
export async function promptForCode() {
    const rl = createInterface({
        input: process.stdin,
        output: process.stdout,
    });
    try {
        return await new Promise((resolve) => {
            rl.question("Authorization code: ", (answer) => {
                resolve(answer.trim());
            });
        });
    }
    finally {
        rl.close();
    }
}
//# sourceMappingURL=manual-flow.js.map