export function diffState(squadName, prev, next) {
    const events = [];
    // No previous state — emit full state
    if (!prev) {
        events.push({ type: 'squad:state', squadName, state: next });
        return events;
    }
    // Squad-level changes
    const prevAgentMap = new Map(prev.agents.map((a) => [a.id, a]));
    const agentChanged = next.agents.some((a) => {
        const pa = prevAgentMap.get(a.id);
        return !pa || pa.status !== a.status;
    });
    const squadChanged = prev.status !== next.status ||
        prev.step.current !== next.step.current ||
        JSON.stringify(prev.handoff) !== JSON.stringify(next.handoff);
    if (squadChanged || agentChanged) {
        events.push({ type: 'squad:state', squadName, state: next });
    }
    // Checkpoint detection — when squad status transitions to 'checkpoint'
    if (prev.status !== 'checkpoint' && next.status === 'checkpoint') {
        const checkpointAgent = next.agents.find((a) => a.status === 'checkpoint');
        if (checkpointAgent) {
            events.push({
                type: 'squad:checkpoint',
                squadName,
                agentId: checkpointAgent.id,
                agentName: checkpointAgent.name,
                question: checkpointAgent.message ?? '',
            });
        }
    }
    // Agent-level changes
    for (const agent of next.agents) {
        const prevAgent = prevAgentMap.get(agent.id);
        if (!prevAgent || prevAgent.status !== agent.status) {
            events.push({
                type: 'agent:status',
                agentId: agent.id,
                agentName: agent.name,
                status: agent.status,
            });
        }
        // Step change detection
        if (prevAgent &&
            agent.stepIndex !== undefined &&
            prevAgent.stepIndex !== undefined &&
            prevAgent.stepIndex !== agent.stepIndex) {
            events.push({
                type: 'agent:step_complete',
                squadName,
                agentId: agent.id,
                agentName: agent.name,
                stepIndex: prevAgent.stepIndex,
                stepLabel: prevAgent.stepLabel ?? '',
            });
            events.push({
                type: 'agent:step_start',
                squadName,
                agentId: agent.id,
                agentName: agent.name,
                stepIndex: agent.stepIndex,
                stepLabel: agent.stepLabel ?? '',
            });
        }
        // Agent DM: message field populated when it wasn't before
        if (agent.message && (!prevAgent || !prevAgent.message)) {
            events.push({
                type: 'chat:agent_dm',
                agentId: agent.id,
                agentName: agent.name,
                message: agent.message,
            });
        }
    }
    return events;
}
//# sourceMappingURL=state-parser.js.map