"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GlobalConfigService = void 0;
const common_1 = require("@nestjs/common");
const fs_1 = require("fs");
const promises_1 = require("fs/promises");
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
const os_1 = require("os");
const REPOBURG_DIR = (0, path_1.join)((0, os_1.homedir)(), '.repoburg');
const CONFIG_FILE = (0, path_1.join)(REPOBURG_DIR, 'config.json');
let GlobalConfigService = class GlobalConfigService {
    constructor() {
        this.config = {};
        if (process.env.NODE_ENV !== 'test') {
            (0, fs_extra_1.ensureDirSync)(REPOBURG_DIR);
            this.initPromise = this.loadConfig();
        }
        else {
            this.initPromise = Promise.resolve();
        }
    }
    async waitForInit() {
        await this.initPromise;
    }
    async loadConfig() {
        try {
            if ((0, fs_1.existsSync)(CONFIG_FILE)) {
                const content = await (0, promises_1.readFile)(CONFIG_FILE, 'utf-8');
                this.config = JSON.parse(content);
            }
            else {
                await this.saveConfig();
            }
        }
        catch (error) {
            console.error('Failed to load global config:', error);
            this.config = {};
        }
    }
    async saveConfig() {
        try {
            if (process.env.NODE_ENV !== 'test') {
                await (0, promises_1.writeFile)(CONFIG_FILE, JSON.stringify(this.config, null, 2), 'utf-8');
            }
        }
        catch (error) {
            console.error('Failed to save global config:', error);
        }
    }
    getConfig() {
        return { ...this.config };
    }
    async updateConfig(updates) {
        const cleanUpdates = Object.fromEntries(Object.entries(updates).filter(([_, v]) => v !== undefined));
        this.config = { ...this.config, ...cleanUpdates };
        await this.saveConfig();
        return this.getConfig();
    }
    get(key) {
        return this.config[key];
    }
    _setConfig(config) {
        this.config = { ...config };
    }
};
exports.GlobalConfigService = GlobalConfigService;
exports.GlobalConfigService = GlobalConfigService = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [])
], GlobalConfigService);
//# sourceMappingURL=global-config.service.js.map