#!/usr/bin/env bun
/**
 * Download sourcemaps for all JS files listed in urls.txt
 *
 * Usage:
 *   bun scripts/update-urls.ts   # First, update urls.txt from build manifest
 *   bun scripts/download-sourcemaps.ts  # Then download sourcemaps
 */

import { mkdir } from "fs/promises";

const OUTPUT_DIR = "sourcemaps";

await mkdir(OUTPUT_DIR, { recursive: true });

// Read urls.txt
const urlsFile = Bun.file("urls.txt");
if (!(await urlsFile.exists())) {
  console.error("urls.txt not found. Run 'bun scripts/update-urls.ts' first.");
  process.exit(1);
}

const urls = (await urlsFile.text())
  .split("\n")
  .map((line) => line.trim())
  .filter((line) => line && line.includes("/_next/") && line.endsWith(".js"));

console.log(`Found ${urls.length} JS files in urls.txt\n`);

let downloaded = 0;
let skipped = 0;
let failed = 0;

for (const jsUrl of urls) {
  const filename = jsUrl.split("/").pop()!;
  const mapFile = `${OUTPUT_DIR}/${filename}.map`;

  // Check if already exists
  const exists = await Bun.file(mapFile).exists();
  if (exists) {
    skipped++;
    continue;
  }

  const mapUrl = `${jsUrl}.map`;

  try {
    const response = await fetch(mapUrl);
    if (response.ok) {
      const content = await response.text();
      // Verify it's a valid sourcemap (starts with {)
      if (content.startsWith("{")) {
        await Bun.write(mapFile, content);
        console.log(`✓ ${filename}.map`);
        downloaded++;
      } else {
        failed++;
      }
    } else {
      failed++;
    }
  } catch (e) {
    failed++;
  }
}

console.log(`\n✅ Downloaded: ${downloaded}`);
console.log(`⏭️  Skipped (exists): ${skipped}`);
console.log(`❌ Failed/No sourcemap: ${failed}`);
console.log(`\nTotal sourcemaps: ${downloaded + skipped}`);
