import { NextResponse } from 'next/server';
import { resolve } from '../../../../../lib/server-container.js';
export async function POST(_request, { params }) {
    const { id } = await params;
    try {
        const useCase = resolve('LaunchToolUseCase');
        const result = await useCase.execute({
            toolId: id,
            directoryPath: process.env.NEXT_PUBLIC_SHEP_INSTANCE_PATH ?? process.cwd(),
            headless: true,
        });
        if (result.ok) {
            return NextResponse.json({ editorName: result.editorName, path: result.path });
        }
        switch (result.code) {
            case 'tool_not_found':
                return NextResponse.json({ error: 'Tool not found' }, { status: 404 });
            case 'not_launchable':
                return NextResponse.json({ error: 'Tool has no openDirectory command' }, { status: 422 });
            case 'launch_failed':
                return NextResponse.json({ error: result.message }, { status: 500 });
        }
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'Failed to launch tool';
        return NextResponse.json({ error: message }, { status: 500 });
    }
}
