import { Router } from 'express';
import fs from 'fs';
import path from 'path';
const LOG_FILE = path.resolve(process.cwd(), 'dev.log');
const router = Router();
// POST /__logs - receive browser console logs
router.post('/', (req, res) => {
    try {
        const { level, args } = req.body;
        const prefix = level === 'error' ? '🔴' : level === 'warn' ? '🟡' : '🌐';
        const timestamp = new Date().toISOString().slice(11, 23); // HH:MM:SS.mmm
        const message = args
            .map((a) => (typeof a === 'object' ? JSON.stringify(a) : String(a)))
            .join(' ');
        const line = `${timestamp} ${prefix} ${message}`;
        // Log to terminal
        console.log(line);
        // Append to file
        fs.appendFileSync(LOG_FILE, line + '\n');
        res.sendStatus(200);
    }
    catch {
        const fallback = `${new Date().toISOString().slice(11, 23)} 🌐 [parse error] ${JSON.stringify(req.body)}`;
        console.log(fallback);
        fs.appendFileSync(LOG_FILE, fallback + '\n');
        res.sendStatus(200);
    }
});
export default router;
