#!/usr/bin/env node
/**
 * Debug script to open a browser with auth state loaded.
 *
 * This helps debug what apps are available and what URLs to use.
 */
async function main() {
    const playwright = await import("playwright");
    const path = await import("path");
    const fs = await import("fs");
    // Find the repo root by looking for package.json with workspaces
    let repoRoot = process.cwd();
    let foundRoot = false;
    while (repoRoot !== "/" && repoRoot !== ".") {
        const packageJsonPath = path.join(repoRoot, "package.json");
        if (fs.existsSync(packageJsonPath)) {
            try {
                const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
                if (packageJson.workspaces) {
                    foundRoot = true;
                    break;
                }
            }
            catch {
                // Failed to parse package.json, continue searching
            }
        }
        const parentDir = path.dirname(repoRoot);
        if (parentDir === repoRoot) {
            break;
        }
        repoRoot = parentDir;
    }
    if (!foundRoot) {
        console.log("⚠️  Could not find workspace root, using process.cwd()");
        repoRoot = process.cwd();
    }
    // Use absolute path to storage state based on known location
    const storageStatePath = path.resolve(repoRoot, "packages/ui-code-mode/playwright/temp/storageState.json");
    console.log(`🔍 Looking for storage state at: ${storageStatePath}`);
    console.log(`   Repo root: ${repoRoot}`);
    console.log(`   File exists: ${fs.existsSync(storageStatePath)}`);
    const browser = await playwright.chromium.launch({
        headless: false,
    });
    let browserContext;
    if (fs.existsSync(storageStatePath)) {
        console.log("📦 Loading auth state from storageState.json...");
        browserContext = await browser.newContext({
            storageState: storageStatePath,
        });
    }
    else {
        console.log("⚠️  No storage state found, proceeding without auth...");
        browserContext = await browser.newContext();
    }
    const page = await browserContext.newPage();
    // Log console messages from the page
    page.on("console", (msg) => console.log("PAGE LOG:", msg.text()));
    page.on("pageerror", (error) => console.error("PAGE ERROR:", error));
    console.log("📍 Navigating to http://localhost:3000...");
    await page.goto("http://localhost:3000", { waitUntil: "domcontentloaded" });
    console.log("");
    console.log("✅ Browser is open! You can now:");
    console.log("   1. Navigate to different pages");
    console.log("   2. Check which user you're logged in as");
    console.log("   3. See what apps are available");
    console.log("   4. Find the correct app URL or folder path");
    console.log("");
    console.log("Press Ctrl+C to close the browser when done.");
    // Keep the script running
    await new Promise(() => { });
}
main().catch((error) => {
    console.error("Fatal error:", error);
    process.exit(1);
});
export {};
//# sourceMappingURL=debug-browser.js.map