"use strict";
/**
 * Main analyzer entry point with dual module support
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.detectFramework = exports.uploadRoutes = exports.exportToJSON = exports.analyzeReactRouter = exports.analyzeVite = exports.analyzeNextApp = exports.analyzeNextPages = exports.analyzeExpress = void 0;
exports.analyze = analyze;
const express_js_1 = require("./frameworks/express.js");
Object.defineProperty(exports, "analyzeExpress", { enumerable: true, get: function () { return express_js_1.analyzeExpress; } });
const nextjs_pages_js_1 = require("./frameworks/nextjs-pages.js");
Object.defineProperty(exports, "analyzeNextPages", { enumerable: true, get: function () { return nextjs_pages_js_1.analyzeNextPages; } });
const nextjs_app_js_1 = require("./frameworks/nextjs-app.js");
Object.defineProperty(exports, "analyzeNextApp", { enumerable: true, get: function () { return nextjs_app_js_1.analyzeNextApp; } });
const vite_js_1 = require("./frameworks/vite.js");
Object.defineProperty(exports, "analyzeVite", { enumerable: true, get: function () { return vite_js_1.analyzeVite; } });
const react_router_js_1 = require("./frameworks/react-router.js");
Object.defineProperty(exports, "analyzeReactRouter", { enumerable: true, get: function () { return react_router_js_1.analyzeReactRouter; } });
const detect_js_1 = require("./detect.js");
const export_js_1 = require("./export.js");
const uploader_js_1 = require("./uploader.js");
// Global state for debouncing
const analysisState = new Map();
const DEBOUNCE_TIME = 5000;
/**
 * Main analyze function with auto-detection
 */
async function analyze(app, options) {
    // Skip in production
    if (options.skipInProduction !== false && process.env.NODE_ENV === 'production') {
        console.log('⏭️  Skipping analysis in production');
        return [];
    }
    // Debounce check
    if (options.runOnce !== false) {
        const cached = analysisState.get(options.projectName);
        const now = Date.now();
        if (cached && (now - cached.timestamp) < DEBOUNCE_TIME) {
            console.log(`⚡ Analysis skipped - already ran ${Math.round((now - cached.timestamp) / 1000)}s ago`);
            return cached.routes;
        }
    }
    console.log(`🔍 Analyzing ${options.projectName}...`);
    // CRITICAL: Prevent restart loops - force saveJson=false when uploading
    if (options.upload !== false && options.apiKey) {
        options.saveJson = false;
    }
    let routes = [];
    let framework = options.framework;
    // Auto-detect framework if not specified
    if (!framework) {
        const detected = (0, detect_js_1.detectFramework)(app);
        framework = detected.name;
        console.log(`📦 Detected framework: ${framework}`);
    }
    // Analyze based on framework
    switch (framework) {
        case 'express':
            if (!app) {
                console.error('❌ Express app instance required');
                return [];
            }
            routes = await (0, express_js_1.analyzeExpress)(app);
            break;
        case 'nextjs-pages':
            routes = await (0, nextjs_pages_js_1.analyzeNextPages)(options.nextDir || 'pages/api');
            break;
        case 'nextjs-app':
            routes = await (0, nextjs_app_js_1.analyzeNextApp)(options.nextDir || 'app');
            break;
        case 'vite':
            routes = await (0, vite_js_1.analyzeVite)(options.nextDir || 'vite.config.js');
            break;
        case 'react-router':
            routes = await (0, react_router_js_1.analyzeReactRouter)(options.nextDir || 'src/routes.tsx');
            break;
        case 'node-http':
        case 'unknown':
        default:
            console.warn(`⚠️  Framework "${framework}" not supported yet`);
            console.log('💡 Supported: express, nextjs-pages, nextjs-app, vite, react-router');
            console.log('💡 Tip: Specify framework manually: { framework: "express" }');
            return [];
    }
    console.log(`✅ Found ${routes.length} routes`);
    // Cache results
    if (options.runOnce !== false) {
        analysisState.set(options.projectName, {
            timestamp: Date.now(),
            routes
        });
    }
    // Save JSON (only if not uploading)
    if (options.saveJson) {
        const filePath = options.jsonPath || 'api-export.json';
        await (0, export_js_1.exportToJSON)(routes, options.projectName, filePath, options.baseUrl);
    }
    // Upload to APIForge
    if (options.upload !== false && options.apiKey) {
        await (0, uploader_js_1.uploadRoutes)(routes, {
            projectName: options.projectName,
            apiKey: options.apiKey,
            baseUrl: options.baseUrl,
            apiForgeUrl: options.apiForgeUrl,
        });
    }
    return routes;
}
var export_js_2 = require("./export.js");
Object.defineProperty(exports, "exportToJSON", { enumerable: true, get: function () { return export_js_2.exportToJSON; } });
var uploader_js_2 = require("./uploader.js");
Object.defineProperty(exports, "uploadRoutes", { enumerable: true, get: function () { return uploader_js_2.uploadRoutes; } });
var detect_js_2 = require("./detect.js");
Object.defineProperty(exports, "detectFramework", { enumerable: true, get: function () { return detect_js_2.detectFramework; } });
// Default export for CommonJS
exports.default = {
    analyze,
    analyzeExpress: express_js_1.analyzeExpress,
    analyzeNextPages: nextjs_pages_js_1.analyzeNextPages,
    analyzeNextApp: nextjs_app_js_1.analyzeNextApp,
    analyzeVite: vite_js_1.analyzeVite,
    analyzeReactRouter: react_router_js_1.analyzeReactRouter,
    exportToJSON: export_js_1.exportToJSON,
    uploadRoutes: uploader_js_1.uploadRoutes,
    detectFramework: detect_js_1.detectFramework
};
