import fs from "node:fs";
import path from "node:path";
export const DEFAULT_UI_LANGUAGE = "zh-CN";
export class UiLanguageService {
    projectPath;
    constructor(projectPath) {
        this.projectPath = projectPath;
    }
    current() {
        const envLanguage = normalizeUiLanguage(process.env.DEEPSEEKCODE_LANGUAGE ?? process.env.DEEPSEEKCODE_LANG);
        if (envLanguage)
            return this.settings(envLanguage, "env");
        const projectLanguage = this.readProjectLanguage();
        if (projectLanguage)
            return this.settings(projectLanguage, "project");
        return this.settings(DEFAULT_UI_LANGUAGE, "default");
    }
    set(language) {
        const normalized = normalizeUiLanguage(language);
        if (!normalized)
            throw new Error(`Unknown language: ${language}`);
        fs.mkdirSync(path.dirname(this.path()), { recursive: true });
        fs.writeFileSync(this.path(), `${JSON.stringify({ language: normalized }, null, 2)}\n`, "utf8");
        process.env.DEEPSEEKCODE_LANGUAGE = normalized;
        return this.settings(normalized, "project");
    }
    reset() {
        if (fs.existsSync(this.path()))
            fs.rmSync(this.path(), { force: true });
        delete process.env.DEEPSEEKCODE_LANGUAGE;
        delete process.env.DEEPSEEKCODE_LANG;
        return this.settings(DEFAULT_UI_LANGUAGE, "default");
    }
    path() {
        return path.join(this.projectPath, ".deepseekcode", "ui.json");
    }
    settings(language, source) {
        return {
            language,
            source,
            path: this.path(),
        };
    }
    readProjectLanguage() {
        if (!fs.existsSync(this.path()))
            return null;
        try {
            const parsed = JSON.parse(fs.readFileSync(this.path(), "utf8"));
            return normalizeUiLanguage(parsed.language);
        }
        catch {
            return null;
        }
    }
}
export function normalizeUiLanguage(value) {
    const normalized = value?.trim().toLowerCase();
    if (!normalized)
        return null;
    if (["zh", "zh-cn", "cn", "chinese", "中文"].includes(normalized))
        return "zh-CN";
    if (["en", "en-us", "english"].includes(normalized))
        return "en";
    return null;
}
export function isChineseUi(language) {
    return (normalizeUiLanguage(language) ?? DEFAULT_UI_LANGUAGE) === "zh-CN";
}
