import {
  getDefaultModelForProvider,
  getModelDisplayName,
  isModelValidForProvider
} from "@dexto/llm";
const CHATGPT_RATE_LIMIT_WARNING_THRESHOLD = 80;
function shouldShowChatGPTRateLimitHint(status) {
  if (!status) {
    return false;
  }
  return status.exceeded || status.usedPercent >= CHATGPT_RATE_LIMIT_WARNING_THRESHOLD;
}
function formatResetTime(resetsAt) {
  if (!resetsAt) {
    return null;
  }
  const date = new Date(resetsAt);
  if (Number.isNaN(date.getTime())) {
    return null;
  }
  return new Intl.DateTimeFormat("en-US", {
    hour: "numeric",
    minute: "2-digit"
  }).format(date);
}
function getChatGPTRateLimitHint(status) {
  const resetTime = formatResetTime(status.resetsAt);
  if (status.exceeded) {
    return resetTime ? `ChatGPT cap reached \xB7 resets ${resetTime}` : "ChatGPT cap reached";
  }
  const usedPercent = Math.max(0, Math.min(100, Math.round(status.usedPercent)));
  return resetTime ? `ChatGPT cap ${usedPercent}% used \xB7 resets ${resetTime}` : `ChatGPT cap ${usedPercent}% used`;
}
function resolveChatGPTFallbackModel(currentModel) {
  const currentModelIsValid = isModelValidForProvider("openai", currentModel);
  const fallbackModel = currentModelIsValid ? currentModel : getDefaultModelForProvider("openai") ?? currentModel;
  return {
    provider: "openai",
    model: fallbackModel,
    displayName: getModelDisplayName(fallbackModel, "openai"),
    usedDefaultFallback: !currentModelIsValid && fallbackModel !== currentModel
  };
}
export {
  CHATGPT_RATE_LIMIT_WARNING_THRESHOLD,
  getChatGPTRateLimitHint,
  resolveChatGPTFallbackModel,
  shouldShowChatGPTRateLimitHint
};
