import fs from "node:fs";
import path from "node:path";
import { DEFAULT_TERMINAL_THEME, getTerminalTheme, normalizeTerminalThemeName, terminalThemes, } from "./themeCatalog.js";
export class ThemeService {
    projectPath;
    constructor(projectPath) {
        this.projectPath = projectPath;
    }
    list() {
        return terminalThemes;
    }
    current() {
        const envTheme = normalizeTerminalThemeName(process.env.DEEPSEEKCODE_THEME);
        if (envTheme) {
            return this.settings(envTheme, "env");
        }
        const projectTheme = this.readProjectTheme();
        if (projectTheme) {
            return this.settings(projectTheme, "project");
        }
        return this.settings(DEFAULT_TERMINAL_THEME, "default");
    }
    set(name) {
        const theme = normalizeTerminalThemeName(name);
        if (!theme)
            throw new Error(`Unknown theme: ${name}`);
        fs.mkdirSync(path.dirname(this.path()), { recursive: true });
        fs.writeFileSync(this.path(), `${JSON.stringify({ theme }, null, 2)}\n`, "utf8");
        process.env.DEEPSEEKCODE_THEME = theme;
        return this.settings(theme, "project");
    }
    reset() {
        if (fs.existsSync(this.path()))
            fs.rmSync(this.path(), { force: true });
        delete process.env.DEEPSEEKCODE_THEME;
        return this.settings(DEFAULT_TERMINAL_THEME, "default");
    }
    path() {
        return path.join(this.projectPath, ".deepseekcode", "theme.json");
    }
    settings(theme, source) {
        return {
            theme,
            source,
            path: this.path(),
            definition: getTerminalTheme(theme),
        };
    }
    readProjectTheme() {
        if (!fs.existsSync(this.path()))
            return null;
        try {
            const parsed = JSON.parse(fs.readFileSync(this.path(), "utf8"));
            return normalizeTerminalThemeName(parsed.theme);
        }
        catch {
            return null;
        }
    }
}
