"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeConfig = exports.loadValidatedConfig = exports.loadConfig = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const env_paths_1 = __importDefault(require("env-paths"));
const toml_1 = __importDefault(require("@iarna/toml"));
const gluegun_1 = require("gluegun");
const DEFAULT_CONFIG = {};
const CONFIG_FILE = path_1.default.join(env_paths_1.default('graph-cli', { suffix: '' }).config, 'indexing.toml');
exports.loadConfig = () => {
    if (fs_1.default.existsSync(CONFIG_FILE)) {
        return toml_1.default.parse(fs_1.default.readFileSync(CONFIG_FILE, 'utf-8'));
    }
    else {
        return DEFAULT_CONFIG;
    }
};
exports.loadValidatedConfig = () => {
    const config = exports.loadConfig();
    const errors = [];
    if (!config.api) {
        errors.push(`- 'api' is not set. Please run 'graph indexer connect <url>' first.`);
    }
    if (errors.length > 0) {
        gluegun_1.print.error(`Failed to load indexer CLI configuration:`);
        gluegun_1.print.error(errors.join('\n'));
        process.exit(1);
    }
    return config;
};
exports.writeConfig = (config) => {
    if (!fs_1.default.existsSync(path_1.default.dirname(CONFIG_FILE))) {
        fs_1.default.mkdirSync(path_1.default.dirname(CONFIG_FILE), { recursive: true });
    }
    fs_1.default.writeFileSync(CONFIG_FILE, toml_1.default.stringify(config), { encoding: 'utf-8' });
};
