import { readFileSync, writeFileSync, mkdirSync, existsSync } from "node:fs";
import { dirname } from "node:path";
import { getGlobalSettingsPath, getProjectSettingsPath } from "./config.js";
/** Read pi settings from global or project scope. */
export function readSettings(scope = "global", projectPath) {
    const path = scope === "global" ? getGlobalSettingsPath() : getProjectSettingsPath(projectPath);
    if (!existsSync(path))
        return {};
    try {
        const raw = JSON.parse(readFileSync(path, "utf-8"));
        return raw && typeof raw === "object" ? raw : {};
    }
    catch {
        return {};
    }
}
/** Merge and write defaultProvider + defaultModel into existing pi settings. */
export function writeSettings(scope, provider, model, projectPath) {
    const path = scope === "global" ? getGlobalSettingsPath() : getProjectSettingsPath(projectPath);
    const dir = dirname(path);
    if (!existsSync(dir))
        mkdirSync(dir, { recursive: true });
    const existing = readSettings(scope, projectPath);
    const merged = {
        ...existing,
        defaultProvider: provider,
        defaultModel: model,
    };
    writeFileSync(path, JSON.stringify(merged, null, 2), "utf-8");
}
//# sourceMappingURL=settings.js.map