/** In-memory cache of ACP available_commands_update per task. */

import type { AgentCommand } from './provider-types.js';
import { mergeCommands } from './provider-commands.js';

export type { AgentCommand };

const commandsByTask = new Map<string, AgentCommand[]>();

export function setTaskCommands(taskId: string, commands: AgentCommand[]): void {
  if (commands.length === 0) return;
  commandsByTask.set(taskId, commands);
}

/** Merge by command name; later lists override earlier entries. */
export function mergeTaskCommands(taskId: string, commands: AgentCommand[]): void {
  if (commands.length === 0) return;
  const merged = mergeCommands(commandsByTask.get(taskId) ?? [], commands);
  commandsByTask.set(taskId, merged);
}

export function getTaskCommands(taskId: string): AgentCommand[] | null {
  return commandsByTask.get(taskId) ?? null;
}

export function clearTaskCommands(taskId: string): void {
  commandsByTask.delete(taskId);
}
