/**
 * Renderer configuration — ported from MCP-SuperAssistant
 * Auto-detects website and applies per-site DOM selectors
 */
import type { FunctionCallRendererConfig } from "./types"

export const DEFAULT_CONFIG: FunctionCallRendererConfig = {
  knownLanguages: [
    "xml", "html", "python", "javascript", "js", "ruby", "bash", "shell",
    "css", "json", "java", "c", "cpp", "csharp", "php", "typescript", "ts",
    "go", "rust", "swift", "kotlin", "sql",
  ],
  handleLanguageTags: true,
  maxLinesAfterLangTag: 3,
  targetSelectors: ["pre", "code"],
  enableDirectMonitoring: true,
  streamingContainerSelectors: [".pre", ".code"],
  function_result_selector: [],
  updateThrottle: 25,
  streamingMonitoringInterval: 100,
  largeContentThreshold: Number.MAX_SAFE_INTEGER,
  progressiveUpdateInterval: 250,
  maxContentPreviewLength: Number.MAX_SAFE_INTEGER,
  usePositionFixed: false,
  stabilizeTimeout: 500,
  debug: false,
  useHostTheme: true,
  enableStalledStreamDetection: true,
  stalledStreamTimeout: 3000,
  stalledStreamCheckInterval: 1000,
  useCodeMirrorExtraction: false,
}

/** Website-specific overrides — selectors for finding function call blocks in AI chat pages */
export const WEBSITE_CONFIGS: Array<{
  urlPattern: string | RegExp
  config: Partial<FunctionCallRendererConfig>
}> = [
  {
    urlPattern: "chatgpt.com",
    config: {
      targetSelectors: ["pre"],
      streamingContainerSelectors: ["pre"],
      function_result_selector: ['div[data-message-author-role="user"]'],
    },
  },
  {
    urlPattern: "chat.openai.com",
    config: {
      targetSelectors: ["pre"],
      streamingContainerSelectors: ["pre"],
      function_result_selector: ['div[data-message-author-role="user"]'],
    },
  },
  {
    urlPattern: "gemini",
    config: {
      targetSelectors: ["pre"],
      streamingContainerSelectors: ["pre"],
      function_result_selector: ["div.query-content"],
    },
  },
  {
    urlPattern: "grok.com",
    config: {
      targetSelectors: ["code"],
      streamingContainerSelectors: ["code"],
      function_result_selector: ["div.relative.items-end"],
    },
  },
  {
    urlPattern: "chat.deepseek.com",
    config: {
      targetSelectors: ["pre", "code"],
      streamingContainerSelectors: ["pre", "code"],
      function_result_selector: ["div._9663006"],
    },
  },
  {
    urlPattern: "perplexity",
    config: {
      targetSelectors: ["pre"],
      streamingContainerSelectors: [".pre"],
      function_result_selector: ['div.group\\/query', '.group\\/query'],
    },
  },
  {
    urlPattern: "openrouter.ai",
    config: {
      targetSelectors: ["pre"],
      streamingContainerSelectors: ["pre"],
      function_result_selector: ['div[data-testid="user-message"]'],
    },
  },
  {
    urlPattern: "t3.chat",
    config: {
      targetSelectors: ["pre"],
      streamingContainerSelectors: ["pre"],
      function_result_selector: ['div[aria-label="Your message"]'],
    },
  },
  {
    urlPattern: "chat.mistral.ai",
    config: {
      targetSelectors: ["pre"],
      streamingContainerSelectors: ["pre"],
      function_result_selector: ['div[data-message-part-type="answer"]'],
    },
  },
]

/** Get merged config for current website */
export function getConfig(): FunctionCallRendererConfig {
  if (typeof window === "undefined") return { ...DEFAULT_CONFIG }

  const url = window.location.href
  let config = { ...DEFAULT_CONFIG }

  for (const site of WEBSITE_CONFIGS) {
    const matches =
      typeof site.urlPattern === "string"
        ? url.includes(site.urlPattern)
        : site.urlPattern.test(url)

    if (matches) {
      config = { ...config, ...site.config }
      break
    }
  }

  return config
}

/** Active configuration singleton */
export const CONFIG = getConfig()
