/**
 * GET /api/applications/[id]/files/raw?path=relative/asset.png
 *
 * Streams a file from an application's repository as raw bytes with a
 * best-effort content-type. Used by the IDE tab to preview images and
 * other binary assets without round-tripping them through JSON.
 */
import { NextResponse } from 'next/server';
import { resolve } from '../../../../../../lib/server-container.js';
export const dynamic = 'force-dynamic';
function errorStatus(message) {
    if (/not found/i.test(message))
        return 404;
    if (/path is required/i.test(message))
        return 400;
    if (/escapes/i.test(message))
        return 400;
    if (/is a directory/i.test(message))
        return 400;
    if (/size limit/i.test(message))
        return 413;
    return 500;
}
export async function GET(request, { params }) {
    try {
        const { id } = await params;
        const url = new URL(request.url);
        const pathParam = url.searchParams.get('path');
        if (!pathParam) {
            return Response.json({ error: 'path query parameter is required' }, { status: 400 });
        }
        const useCase = resolve('ReadApplicationFileRawUseCase');
        const result = await useCase.execute({ applicationId: id, path: pathParam });
        // Copy into a fresh ArrayBuffer so Next.js 16's BodyInit type constraint
        // (which requires `ArrayBuffer`, not the wider `ArrayBufferLike`) is
        // satisfied. The prior version passing a Node Buffer directly was being
        // served with an incorrect length, causing the image to render broken.
        const ab = new ArrayBuffer(result.buffer.byteLength);
        new Uint8Array(ab).set(result.buffer);
        return new NextResponse(ab, {
            status: 200,
            headers: {
                'Content-Type': result.mimeType,
                'Content-Length': String(result.size),
                'Cache-Control': 'no-store',
            },
        });
    }
    catch (error) {
        // eslint-disable-next-line no-console
        console.error('[GET /api/applications/:id/files/raw]', error);
        const message = error instanceof Error ? error.message : String(error);
        return Response.json({ error: message }, { status: errorStatus(message) });
    }
}
