import { readFileSync } from "fs";
import { join } from "path";
import { SUPPORTED_PROVIDERS, invalidProviderError } from "../constants.js";
import { KASTELL_DIR } from "./paths.js";
import { secureMkdirSync, secureWriteFileSync } from "./secureWrite.js";
const DEFAULTS_FILE = join(KASTELL_DIR, "config.json");
const VALID_KEYS = ["provider", "region", "size", "name"];
function ensureConfigDir() {
    secureMkdirSync(KASTELL_DIR, { recursive: true });
}
export function getDefaults() {
    try {
        const data = readFileSync(DEFAULTS_FILE, "utf-8");
        const parsed = JSON.parse(data);
        if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
            return {};
        return parsed;
    }
    catch {
        return {};
    }
}
export function setDefault(key, value) {
    if (!VALID_KEYS.includes(key)) {
        throw new Error(`Invalid config key: ${key}. Valid keys: ${VALID_KEYS.join(", ")}`);
    }
    if (key === "provider" && !SUPPORTED_PROVIDERS.includes(value)) {
        throw new Error(invalidProviderError(value));
    }
    ensureConfigDir();
    const config = getDefaults();
    config[key] = value;
    secureWriteFileSync(DEFAULTS_FILE, JSON.stringify(config, null, 2));
}
export function getDefault(key) {
    const config = getDefaults();
    return config[key];
}
export function resetDefaults() {
    ensureConfigDir();
    secureWriteFileSync(DEFAULTS_FILE, JSON.stringify({}, null, 2));
}
export { DEFAULTS_FILE, VALID_KEYS };
//# sourceMappingURL=providerConfig.js.map