import { specStore } from '../storage/index.js';
import { handleDistribute, handlePreCheck, handleDecompose } from './orchestrate-distribution.js';
import { getCleanState } from './orchestrate/state.js';
import { handleRegister, handleHeartbeat, handleLock, handleUnlock, handleStatus, handleResolveConflict, } from './orchestrate/session-handlers.js';
import { handleBrief, handleJournal } from './orchestrate/journal-brief-handlers.js';
// ---------------------------------------------------------------------------
// Main handler
// ---------------------------------------------------------------------------
export async function handleOrchestrate(params) {
    const { projectId, action, sessionId, specId, resourcePath, specIds, agentCount, limit, experienceLevel, lockTtlMinutes, fileOwnership, journalAction, eventType, commitHash, } = params;
    try {
        switch (action) {
            case 'register':
                return await handleRegister(projectId, sessionId, specId, fileOwnership);
            case 'heartbeat':
                return await handleHeartbeat(projectId, sessionId, lockTtlMinutes);
            case 'lock':
                return await handleLock(projectId, sessionId, resourcePath, specId, lockTtlMinutes, fileOwnership);
            case 'unlock':
                return await handleUnlock(projectId, sessionId, resourcePath, specId);
            case 'distribute': {
                const state = await getCleanState(projectId);
                const allSpecs = await specStore.listSpecs(projectId);
                const activeSessions = state.sessions.filter((s) => s.status === 'active');
                return handleDistribute(activeSessions, allSpecs);
            }
            case 'status':
                return await handleStatus(projectId, experienceLevel);
            case 'resolve-conflict':
                return await handleResolveConflict(projectId, sessionId, specId, resourcePath);
            case 'pre-check':
                return await handlePreCheck(specIds ?? []);
            case 'decompose':
                if (!specId) {
                    return {
                        content: [
                            {
                                type: 'text',
                                text: JSON.stringify({ error: 'specId is required for decompose action' }, null, 2),
                            },
                        ],
                        isError: true,
                    };
                }
                return await handleDecompose(specId, agentCount ?? 2);
            case 'brief':
                return await handleBrief(projectId, experienceLevel);
            case 'journal':
                return await handleJournal(sessionId, journalAction, eventType, specId, commitHash, limit ?? 10);
            default:
                return {
                    content: [
                        {
                            type: 'text',
                            text: JSON.stringify({ error: `Unknown orchestration action: ${String(action)}` }, null, 2),
                        },
                    ],
                    isError: true,
                };
        }
    }
    catch (err) {
        const message = err instanceof Error ? err.message : String(err);
        return {
            content: [{ type: 'text', text: JSON.stringify({ error: message, action }, null, 2) }],
            isError: true,
        };
    }
}
//# sourceMappingURL=orchestrate.js.map