"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var chatgpt_rate_limit_exports = {};
__export(chatgpt_rate_limit_exports, {
  CHATGPT_RATE_LIMIT_WARNING_THRESHOLD: () => CHATGPT_RATE_LIMIT_WARNING_THRESHOLD,
  getChatGPTRateLimitHint: () => getChatGPTRateLimitHint,
  resolveChatGPTFallbackModel: () => resolveChatGPTFallbackModel,
  shouldShowChatGPTRateLimitHint: () => shouldShowChatGPTRateLimitHint
});
module.exports = __toCommonJS(chatgpt_rate_limit_exports);
var import_llm = require("@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 = (0, import_llm.isModelValidForProvider)("openai", currentModel);
  const fallbackModel = currentModelIsValid ? currentModel : (0, import_llm.getDefaultModelForProvider)("openai") ?? currentModel;
  return {
    provider: "openai",
    model: fallbackModel,
    displayName: (0, import_llm.getModelDisplayName)(fallbackModel, "openai"),
    usedDefaultFallback: !currentModelIsValid && fallbackModel !== currentModel
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  CHATGPT_RATE_LIMIT_WARNING_THRESHOLD,
  getChatGPTRateLimitHint,
  resolveChatGPTFallbackModel,
  shouldShowChatGPTRateLimitHint
});
