import type { TicketActor } from "@/domain";

export type TicketCreatorFilterValue = "" | "user" | `agent:${string}`;

export function ticketActorLabel(
  actor: TicketActor,
  nameOf: (agentId: string) => string,
): string {
  switch (actor.kind) {
    case "agent":
      return `Agent : ${nameOf(actor.agentId)}`;
    case "system":
      return "Système";
    case "user":
    default:
      return "Utilisateur";
  }
}

export function creatorFilterValue(
  actor: { kind: "user" } | { kind: "agent"; agentId: string } | undefined,
): TicketCreatorFilterValue {
  if (!actor) return "";
  if (actor.kind === "user") return "user";
  return `agent:${actor.agentId}`;
}

export function creatorFilterFromValue(
  value: string,
): { kind: "user" } | { kind: "agent"; agentId: string } | undefined {
  if (value === "user") return { kind: "user" };
  if (value.startsWith("agent:")) {
    const agentId = value.slice("agent:".length);
    return agentId ? { kind: "agent", agentId } : undefined;
  }
  return undefined;
}
