"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConfig = getConfig;
exports.setConfig = setConfig;
exports.getToken = getToken;
exports.getConfigPath = getConfigPath;
const fs = __importStar(require("node:fs"));
const path = __importStar(require("node:path"));
const profile_manager_1 = require("./profile-manager");
function getConfigDir() {
    return profile_manager_1.ProfileManager.getProfileDir();
}
function getConfigFile() {
    return path.join(getConfigDir(), 'config.json');
}
/**
 * Ensure config directory exists
 */
function ensureConfigDir() {
    const dir = getConfigDir();
    if (!fs.existsSync(dir)) {
        fs.mkdirSync(dir, { recursive: true });
    }
}
/**
 * Read configuration from ~/.flui/config.json
 * @returns Configuration object
 */
function getConfig() {
    ensureConfigDir();
    const configFile = getConfigFile();
    if (!fs.existsSync(configFile)) {
        return {};
    }
    try {
        const content = fs.readFileSync(configFile, 'utf-8');
        return JSON.parse(content);
    }
    catch (error) {
        throw new Error(`Failed to read config file: ${error instanceof Error ? error.message : String(error)}`);
    }
}
/**
 * Write configuration to ~/.flui/config.json
 * @param key Configuration key
 * @param value Configuration value
 */
function setConfig(key, value) {
    ensureConfigDir();
    const config = getConfig();
    config[key] = value;
    try {
        fs.writeFileSync(getConfigFile(), JSON.stringify(config, null, 2));
    }
    catch (error) {
        throw new Error(`Failed to write config file: ${error instanceof Error ? error.message : String(error)}`);
    }
}
/**
 * Get Hetzner token from config
 * @returns Hetzner API token
 * @throws Error if token is not configured
 */
function getToken() {
    const config = getConfig();
    if (!config.hetzner_token) {
        throw new Error('Hetzner token not configured.\n' +
            'Please set it using:\n' +
            '  flui config set hetzner_token YOUR_TOKEN\n\n' +
            'Get your token from: https://console.hetzner.cloud/');
    }
    return config.hetzner_token;
}
/**
 * Get config file path
 * @returns Path to config file
 */
function getConfigPath() {
    return getConfigFile();
}
