import type { MessageAgentQueueUpdate } from "$lib/types/MessageUpdate"; import type { AgentQueueMutation, AgentQueueReplacement, AgentQueueSnapshot, } from "./activityTypes"; export interface AgentQueueMutationTarget { queue: AgentQueueMutation["queue"]; index: number; value: string; expected: AgentQueueSnapshot; } function cloneSnapshot(snapshot: AgentQueueSnapshot): AgentQueueSnapshot { return { steering: [...snapshot.steering], followUp: [...snapshot.followUp], }; } export function snapshotAgentQueue( update: Pick ): AgentQueueSnapshot { return { steering: [...update.steering], followUp: [...update.followUp], }; } export function captureAgentQueueMutationTarget( update: Pick, queue: AgentQueueMutation["queue"], index: number ): AgentQueueMutationTarget | undefined { const expected = snapshotAgentQueue(update); const value = expected[queue][index]; if (typeof value !== "string") return; return { queue, index, value, expected }; } export function agentQueueMatchesSnapshot( update: Pick, expected: AgentQueueSnapshot ): boolean { return ( update.steering.length === expected.steering.length && update.followUp.length === expected.followUp.length && update.steering.every((value, index) => value === expected.steering[index]) && update.followUp.every((value, index) => value === expected.followUp[index]) ); } export function createAgentQueueReplacement( target: AgentQueueMutationTarget, action: AgentQueueMutation["action"], value?: string ): AgentQueueReplacement | undefined { if (target.expected[target.queue][target.index] !== target.value) return; if (action === "edit" && typeof value !== "string") return; const expected = cloneSnapshot(target.expected); const replacement = cloneSnapshot(target.expected); if (action === "remove") replacement[target.queue].splice(target.index, 1); else replacement[target.queue][target.index] = value as string; return { expected, replacement }; }