import type * as Provider from "./provider"
import { iife } from "@/util/iife"

const WIDELY_SUPPORTED_EFFORTS = ["low", "medium", "high"]
const OPENAI_EFFORTS = ["none", "minimal", ...WIDELY_SUPPORTED_EFFORTS, "xhigh"]

function anthropicAdaptiveEfforts(apiId: string): string[] | null {
  if (["opus-4-7", "opus-4.7"].some((v) => apiId.includes(v))) return ["low", "medium", "high", "xhigh", "max"]
  if (["opus-4-6", "opus-4.6", "sonnet-4-6", "sonnet-4.6"].some((v) => apiId.includes(v))) return ["low", "medium", "high", "max"]
  return null
}

export function variants(model: Provider.Model): Record<string, Record<string, any>> {
  if (["@saeeol/gateway", "@ai-sdk/openai-compatible"].includes(model.api.npm) && model.variants && Object.keys(model.variants).length > 0) return model.variants
  if (!model.capabilities.reasoning) return {}
  const id = model.id.toLowerCase()
  const adaptiveEfforts = anthropicAdaptiveEfforts(model.api.id)
  if (id.includes("deepseek-chat") || id.includes("deepseek-reasoner") || id.includes("deepseek-r1") || id.includes("deepseek-v3") || id.includes("minimax") || id.includes("k2p") || id.includes("qwen") || id.includes("big-pickle")) return {}
  if (id.includes("grok") && id.includes("grok-3-mini")) {
    const isOr = model.api.npm === "@openrouter/ai-sdk-provider" || model.api.npm === "@saeeol/gateway"
    return { low: { ...(isOr ? { reasoning: { effort: "low" } } : { reasoningEffort: "low" }) }, high: { ...(isOr ? { reasoning: { effort: "high" } } : { reasoningEffort: "high" }) } }
  }
  if (id.includes("grok")) return {}
  switch (model.api.npm) {
    case "@saeeol/gateway": case "@openrouter/ai-sdk-provider":
      if (id.includes("glm") || id.includes("kimi") || id.includes("qwen")) return { instant: { reasoning: { enabled: false } }, thinking: { reasoning: { enabled: true } } }
      if (!model.id.includes("gpt") && !model.id.includes("gemini-3") && !model.id.includes("claude") && !model.id.includes("mercury")) return {}
      return Object.fromEntries(OPENAI_EFFORTS.map((e) => [e, { reasoning: { effort: e } }]))
    case "@ai-sdk/gateway":
      if (model.id.includes("anthropic")) {
        if (adaptiveEfforts) return Object.fromEntries(adaptiveEfforts.map((e) => [e, { thinking: { type: "adaptive" }, effort: e }]))
        return { high: { thinking: { type: "enabled", budgetTokens: 16000 } }, max: { thinking: { type: "enabled", budgetTokens: 31999 } } }
      }
      if (model.id.includes("google")) {
        if (id.includes("2.5")) return { high: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } }, max: { thinkingConfig: { includeThoughts: true, thinkingBudget: 24576 } } }
        return Object.fromEntries(["low", "high"].map((e) => [e, { includeThoughts: true, thinkingLevel: e }]))
      }
      return Object.fromEntries(OPENAI_EFFORTS.map((e) => [e, { reasoningEffort: e }]))
    case "@ai-sdk/github-copilot":
      if (model.id.includes("gemini")) return {}
      if (model.id.includes("claude")) return Object.fromEntries(WIDELY_SUPPORTED_EFFORTS.map((e) => [e, { reasoningEffort: e }]))
      const copilotEfforts = iife(() => { if (id.includes("5.1-codex-max") || id.includes("5.2") || id.includes("5.3")) return [...WIDELY_SUPPORTED_EFFORTS, "xhigh"]; const arr = [...WIDELY_SUPPORTED_EFFORTS]; if (id.includes("gpt-5") && model.release_date >= "2025-12-04") arr.push("xhigh"); return arr })
      return Object.fromEntries(copilotEfforts.map((e) => [e, { reasoningEffort: e, reasoningSummary: "auto", include: ["reasoning.encrypted_content"] }]))
    case "@ai-sdk/cerebras": case "@ai-sdk/togetherai": case "@ai-sdk/xai": case "@ai-sdk/deepinfra": case "venice-ai-sdk-provider": case "@ai-sdk/openai-compatible":
      const efforts = [...WIDELY_SUPPORTED_EFFORTS]; if (model.api.id.toLowerCase().includes("deepseek-v4")) efforts.push("max")
      return Object.fromEntries(efforts.map((e) => [e, { reasoningEffort: e }]))
    case "@ai-sdk/azure":
      if (id === "o1-mini") return {}
      const azureEfforts = ["low", "medium", "high"]; if (id.includes("gpt-5-") || id === "gpt-5") azureEfforts.unshift("minimal"); if (model.release_date >= "2025-12-04") azureEfforts.push("xhigh")
      return Object.fromEntries(azureEfforts.map((e) => [e, { reasoningEffort: e, reasoningSummary: "auto", include: ["reasoning.encrypted_content"] }]))
    case "@ai-sdk/openai":
      if (id === "gpt-5-pro") return {}
      const openaiEfforts = iife(() => { if (id.includes("codex")) return id.includes("5.2") || id.includes("5.3") ? [...WIDELY_SUPPORTED_EFFORTS, "xhigh"] : WIDELY_SUPPORTED_EFFORTS; const arr = [...WIDELY_SUPPORTED_EFFORTS]; if (id.includes("gpt-5-") || id === "gpt-5") arr.unshift("minimal"); if (model.release_date >= "2025-11-13") arr.unshift("none"); if (model.release_date >= "2025-12-04") arr.push("xhigh"); return arr })
      return Object.fromEntries(openaiEfforts.map((e) => [e, { reasoningEffort: e, reasoningSummary: "auto", include: ["reasoning.encrypted_content"] }]))
    case "@ai-sdk/anthropic": case "@ai-sdk/google-vertex/anthropic":
      if (adaptiveEfforts) {
        let efforts = [...adaptiveEfforts]; if (model.providerID === "github-copilot") { if (model.api.id.includes("opus-4.7")) efforts = ["medium"]; efforts = efforts.filter((v) => v !== "max" && v !== "xhigh") }
        return Object.fromEntries(efforts.map((e) => [e, { thinking: { type: "adaptive", ...(model.api.id.includes("opus-4-7") || model.api.id.includes("opus-4.7") ? { display: "summarized" } : {}) }, effort: e }]))
      }
      return { high: { thinking: { type: "enabled", budgetTokens: Math.min(16_000, Math.floor(model.limit.output / 2 - 1)) } }, max: { thinking: { type: "enabled", budgetTokens: Math.min(31_999, model.limit.output - 1) } } }
    case "@ai-sdk/amazon-bedrock":
      if (adaptiveEfforts) return Object.fromEntries(adaptiveEfforts.map((e) => [e, { reasoningConfig: { type: "adaptive", maxReasoningEffort: e, ...(model.api.id.includes("opus-4-7") || model.api.id.includes("opus-4.7") ? { display: "summarized" } : {}) } }]))
      if (model.api.id.includes("anthropic")) return { high: { reasoningConfig: { type: "enabled", budgetTokens: 16000 } }, max: { reasoningConfig: { type: "enabled", budgetTokens: 31999 } } }
      return Object.fromEntries(WIDELY_SUPPORTED_EFFORTS.map((e) => [e, { reasoningConfig: { type: "enabled", maxReasoningEffort: e } }]))
    case "@ai-sdk/google-vertex": case "@ai-sdk/google":
      if (id.includes("2.5")) return { high: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } }, max: { thinkingConfig: { includeThoughts: true, thinkingBudget: 24576 } } }
      const levels = id.includes("3.1") ? ["low", "medium", "high"] : ["low", "high"]
      return Object.fromEntries(levels.map((e) => [e, { thinkingConfig: { includeThoughts: true, thinkingLevel: e } }]))
    case "@ai-sdk/mistral":
      if (!model.capabilities.reasoning) return {}
      const mistralId = model.api.id.toLowerCase(); if (!["mistral-small-2603", "mistral-small-latest", "mistral-medium-3.5"].some((mid) => mistralId.includes(mid))) return {}
      return { high: { reasoningEffort: "high" } }
    case "@ai-sdk/cohere": return {}
    case "@ai-sdk/groq": return Object.fromEntries(["none", ...WIDELY_SUPPORTED_EFFORTS].map((e) => [e, { reasoningEffort: e }]))
    case "@ai-sdk/perplexity": return {}
    case "@jerome-benoit/sap-ai-provider-v2":
      if (model.api.id.includes("anthropic")) {
        if (adaptiveEfforts) return Object.fromEntries(adaptiveEfforts.map((e) => [e, { thinking: { type: "adaptive" }, effort: e }]))
        return { high: { thinking: { type: "enabled", budgetTokens: 16000 } }, max: { thinking: { type: "enabled", budgetTokens: 31999 } } }
      }
      if (model.api.id.includes("gemini") && id.includes("2.5")) return { high: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } }, max: { thinkingConfig: { includeThoughts: true, thinkingBudget: 24576 } } }
      if (model.api.id.includes("gpt") || /\bo[1-9]/.test(model.api.id)) return Object.fromEntries(WIDELY_SUPPORTED_EFFORTS.map((e) => [e, { reasoningEffort: e }]))
      return {}
  }
  return {}
}
