/**
 * Minimal shared theme for syntax highlighting and extension API compatibility.
 *
 * This replaces the interactive/mode TUI theme system with a stripped-down
 * version that supports the core utilities used by the serve/relay path:
 * - getLanguageFromPath(): maps file extensions to highlight.js language IDs
 * - highlightCode(): syntax-highlights code blocks
 * - loadThemeFromPath(): loads a theme JSON file from disk
 * - Theme class: used by resource-loader and extension types
 *
 * All ANSI color formatting (theme.fg(), theme.bold(), etc.) is removed —
 * tool output is plain text for LLM consumption in serve/relay mode.
 */
import * as fs from "node:fs";
import { highlight, supportsLanguage } from "../utils/syntax-highlight.js";
// ============================================================================
// Language detection
// ============================================================================
const extToLang = {
    ts: "typescript",
    tsx: "typescript",
    js: "javascript",
    jsx: "javascript",
    mjs: "javascript",
    cjs: "javascript",
    py: "python",
    rb: "ruby",
    rs: "rust",
    go: "go",
    java: "java",
    kt: "kotlin",
    swift: "swift",
    c: "c",
    h: "c",
    cpp: "cpp",
    cc: "cpp",
    cxx: "cpp",
    hpp: "cpp",
    cs: "csharp",
    php: "php",
    sh: "bash",
    bash: "bash",
    zsh: "bash",
    fish: "fish",
    ps1: "powershell",
    sql: "sql",
    html: "html",
    htm: "html",
    css: "css",
    scss: "scss",
    sass: "sass",
    less: "less",
    json: "json",
    yaml: "yaml",
    yml: "yaml",
    toml: "toml",
    xml: "xml",
    md: "markdown",
    markdown: "markdown",
    dockerfile: "dockerfile",
    makefile: "makefile",
    cmake: "cmake",
    lua: "lua",
    perl: "perl",
    r: "r",
    scala: "scala",
    clj: "clojure",
    ex: "elixir",
    exs: "elixir",
    erl: "erlang",
    hs: "haskell",
    ml: "ocaml",
    vim: "vim",
    graphql: "graphql",
    proto: "protobuf",
    tf: "hcl",
    hcl: "hcl",
};
export function getLanguageFromPath(filePath) {
    const ext = filePath.split(".").pop()?.toLowerCase();
    if (!ext)
        return undefined;
    return extToLang[ext];
}
// ============================================================================
// Syntax highlighting
// ============================================================================
/** Identity formatter — returns text as-is (no ANSI styling in serve mode). */
function identity(text) {
    return text;
}
const neutralHighlightTheme = {
    keyword: identity,
    built_in: identity,
    literal: identity,
    number: identity,
    string: identity,
    comment: identity,
    function: identity,
    title: identity,
    class: identity,
    type: identity,
    attr: identity,
    variable: identity,
    params: identity,
    operator: identity,
    punctuation: identity,
};
/**
 * Highlight code with syntax coloring based on file extension or language.
 * In serve mode, returns plain text (no ANSI coloring for LLM consumption).
 */
export function highlightCode(code, lang) {
    const validLang = lang && supportsLanguage(lang) ? lang : undefined;
    if (!validLang) {
        return code.split("\n");
    }
    try {
        return highlight(code, {
            language: validLang,
            ignoreIllegals: true,
            theme: neutralHighlightTheme,
        }).split("\n");
    }
    catch {
        return code.split("\n");
    }
}
// ============================================================================
// Theme loading (for resource-loader)
// ============================================================================
/** Resolve built-in theme directory using config path helpers. */
function getThemesDir() {
    try {
        // This function is exported from config.ts and uses the actual package directory
        const { getThemesDir: fn } = require("../../config.js");
        return fn();
    }
    catch {
        // Fallback: themes were in modes/interactive/theme/, try to find them
        try {
            const { join } = require("node:path");
            const { existsSync } = require("node:fs");
            const packageDir = join(__dirname, "../../..");
            for (const base of ["src", "dist"]) {
                const candidates = [
                    join(packageDir, base, "modes", "interactive", "theme"),
                ];
                for (const dir of candidates) {
                    if (existsSync(dir))
                        return dir;
                }
            }
        }
        catch {
            // ignore
        }
        return __dirname;
    }
}
function getBuiltinThemes() {
    const themesDir = getThemesDir();
    if (!themesDir)
        return {};
    try {
        const { join } = require("node:path");
        const { readFileSync } = require("node:fs");
        return {
            dark: JSON.parse(readFileSync(join(themesDir, "dark.json"), "utf-8")),
            light: JSON.parse(readFileSync(join(themesDir, "light.json"), "utf-8")),
        };
    }
    catch {
        return {};
    }
}
export function loadThemeFromPath(themePath) {
    const content = fs.readFileSync(themePath, "utf-8");
    const json = JSON.parse(content);
    return {
        name: json.name,
        sourcePath: themePath,
    };
}
// ============================================================================
// Registered themes (for extension API compatibility)
// ============================================================================
const registeredThemes = new Map();
export function setRegisteredThemes(themes) {
    registeredThemes.clear();
    for (const t of themes) {
        if (t.name) {
            registeredThemes.set(t.name, t);
        }
    }
}
export function getAvailableThemes() {
    return Array.from(new Set([
        ...Object.keys(getBuiltinThemes()),
        ...registeredThemes.keys(),
    ])).sort();
}
