/**
 * API Route for Code Generation
 * Runs httpsnippet on the server to avoid browser compatibility issues
 */ import { NextResponse } from 'next/server';
import { HTTPSnippet } from '@hoppscotch/httpsnippet';
export async function POST(request) {
    try {
        const body = await request.json();
        if (!body.harRequest || !body.lang || !body.client) {
            return NextResponse.json({
                success: false,
                error: 'Missing required fields: harRequest, lang, client'
            }, {
                status: 400
            });
        }
        // Create HTTPSnippet instance
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const snippet = new HTTPSnippet(body.harRequest);
        // Generate code
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const code = snippet.convert(body.lang, body.client, {
            indent: '  '
        });
        if (typeof code === 'string') {
            return NextResponse.json({
                success: true,
                code
            });
        }
        if (Array.isArray(code) && code.length > 0) {
            return NextResponse.json({
                success: true,
                code: code[0]
            });
        }
        return NextResponse.json({
            success: false,
            error: 'Code generation returned unexpected result'
        }, {
            status: 500
        });
    } catch (error) {
        console.error('Code generation error:', error);
        return NextResponse.json({
            success: false,
            error: error instanceof Error ? error.message : 'Unknown error during code generation'
        }, {
            status: 500
        });
    }
}
