import type { UiMessage } from "@/data/dto/chat";

import type { ChatState } from "../chat-state";

// The initial history request remains bounded even though pagination is disabled.
export const CHAT_HISTORY_LIMIT = 50;

export function applyHistoryLoadedOutput(
  output: {
    messages: UiMessage[];
  },
): Pick<ChatState, "messages" | "historyLoaded"> {
  return {
    messages: output.messages,
    historyLoaded: true,
  };
}

export function applyNetworkHistoryLoadedOutput(
  context: ChatState,
  output: {
    messages: UiMessage[];
    localCount: number;
  },
): Pick<ChatState, "messages" | "historyLoaded"> {
  const localSnapshotSize = output.localCount === 0 ? 1 : output.localCount;
  const optimisticTail = context.messages.slice(localSnapshotSize);
  return {
    messages: [...output.messages, ...optimisticTail],
    historyLoaded: true,
  };
}
