export class ProjectPlanningRoutes {
    context;
    constructor(context) {
        this.context = context;
    }
    async handle(req) {
        const url = new URL(req.url);
        if (!url.pathname.startsWith('/api/projects/planning'))
            return null;
        try {
            if (url.pathname === '/api/projects/planning/status' && req.method === 'GET') {
                return Response.json(await this.context.projectPlanningService.status(readProjectSpaceFromUrl(url)));
            }
            if (url.pathname === '/api/projects/planning/state' && req.method === 'GET') {
                return Response.json(await this.context.projectPlanningService.getState({
                    ...readProjectSpaceFromUrl(url),
                    planningId: url.searchParams.get('planningId') ?? undefined,
                }));
            }
            if (url.pathname === '/api/projects/planning/state' && req.method === 'POST') {
                return this.admin(req, async () => Response.json(await this.context.projectPlanningService.upsertState(await this.readBody(req))));
            }
            if (url.pathname === '/api/projects/planning/evaluate' && req.method === 'POST') {
                return Response.json(await this.context.projectPlanningService.evaluate(await this.readOptionalBody(req)));
            }
            if (url.pathname === '/api/projects/planning/decisions' && req.method === 'GET') {
                return Response.json(await this.context.projectPlanningService.listDecisions(readProjectSpaceFromUrl(url)));
            }
            if (url.pathname === '/api/projects/planning/decisions' && req.method === 'POST') {
                return this.admin(req, async () => Response.json(await this.context.projectPlanningService.recordDecision(await this.readBody(req))));
            }
            if (url.pathname === '/api/projects/planning/language' && req.method === 'GET') {
                return Response.json(await this.context.projectPlanningService.getLanguage(readProjectSpaceFromUrl(url)));
            }
            if (url.pathname === '/api/projects/planning/language' && req.method === 'POST') {
                return this.admin(req, async () => Response.json(await this.context.projectPlanningService.upsertLanguage(await this.readBody(req))));
            }
            return Response.json({ error: 'Unknown project planning route' }, { status: 404 });
        }
        catch (error) {
            return Response.json({ error: error instanceof Error ? error.message : String(error) }, { status: 400 });
        }
    }
    async admin(req, fn) {
        const adminError = this.context.requireAdmin(req);
        if (adminError)
            return adminError;
        return fn();
    }
    async readBody(req) {
        const body = await this.context.parseJsonBody(req);
        if (body instanceof Response)
            throw new Error(await body.text());
        return body;
    }
    async readOptionalBody(req) {
        const body = await this.context.parseOptionalJsonBody(req);
        if (body instanceof Response)
            throw new Error(await body.text());
        return (body ?? {});
    }
}
function readProjectSpaceFromUrl(url) {
    return {
        ...(url.searchParams.get('projectId') ? { projectId: url.searchParams.get('projectId') } : {}),
        ...(url.searchParams.get('knowledgeSpaceId') ? { knowledgeSpaceId: url.searchParams.get('knowledgeSpaceId') } : {}),
    };
}
