import type { Provider } from "./api";

type PersistedChatModel = {
  lastUsedProvider: Provider | null;
  lastUsedModel: string | null;
};

export type ChatModelSelection = {
  provider: Provider;
  model: string;
};

export function getChatModelSelection(chat: PersistedChatModel | null): ChatModelSelection | null {
  if (!chat?.lastUsedProvider || !chat.lastUsedModel?.trim()) return null;
  return {
    provider: chat.lastUsedProvider,
    model: chat.lastUsedModel.trim(),
  };
}

export function getChatModelSelectionSyncKey(chatId: string | null, selection: ChatModelSelection | null) {
  if (!chatId || !selection) return null;
  return JSON.stringify([chatId, selection.provider, selection.model]);
}
