import 'dotenv/config';
import { Runner } from './core/runner.js';
import { Logger } from './utils/logger.js';
import { Recorder } from './recorder/recorder.js';
import { Compiler } from './recorder/compiler.js';
async function main() {
    const modeArg = process.argv.find(arg => arg.startsWith('--mode=')) || '--mode=run';
    const mode = modeArg.split('=')[1];
    const targetArg = process.argv.find(arg => arg.startsWith('--target='));
    const target = targetArg ? targetArg.split('=')[1] : '';
    try {
        if (mode === 'record') {
            if (!target) {
                Logger.error('Recording requires --target=<url>. Example: --mode=record --target=https://google.com');
                process.exit(1);
            }
            const siteName = new URL(target).hostname.replace(/\./g, '_');
            await Recorder.start(target);
            // Post-recording trigger compiler
            const events = Recorder.getCapturedEvents();
            Compiler.compile(events, siteName);
            Logger.log('Enterprise Generate Pipeline Complete.');
            Logger.log(`Use this to run: node dist/framework/index.js --mode=run --scenario=framework/scenarios/${siteName}.scenario.json`);
        }
        else if (mode === 'run' || mode === 'scrape' || mode === 'validate') {
            const scenarioArg = process.argv.find(arg => arg.startsWith('--scenario='));
            if (!scenarioArg) {
                Logger.error('Run mode requires --scenario=<path>.');
                process.exit(1);
            }
            const scenarioPath = scenarioArg.split('=')[1];
            await Runner.runScenario(scenarioPath);
        }
        else {
            Logger.error(`Unsupported mode: ${mode}`);
        }
    }
    catch (err) {
        Logger.error('Fatal platform error', err);
        process.exit(1);
    }
}
main();
