"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);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, privateMap, value) {
    if (!privateMap.has(receiver)) {
        throw new TypeError("attempted to set private field on non-instance");
    }
    privateMap.set(receiver, value);
    return value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
    if (!privateMap.has(receiver)) {
        throw new TypeError("attempted to get private field on non-instance");
    }
    return privateMap.get(receiver);
};
var _config;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigService = void 0;
const common_1 = require("@nestjs/common");
const class_transformer_1 = require("class-transformer");
const class_validator_1 = require("class-validator");
const fs = require("fs-extra");
const path = require("path");
const os = require("os");
const config_dto_1 = require("./config.dto");
let ConfigService = class ConfigService {
    constructor() {
        _config.set(this, void 0);
        this.configPath = path.join(os.homedir(), '.shellops.json');
        this.transformOpts = {
            enableCircularCheck: true,
            enableImplicitConversion: true,
            exposeDefaultValues: true,
            strategy: 'exposeAll',
        };
        __classPrivateFieldSet(this, _config, this.loadConfig());
    }
    get config() {
        return __classPrivateFieldGet(this, _config);
    }
    set config(value) {
        console.log('config changed');
        __classPrivateFieldSet(this, _config, value);
        this.saveConfig(value);
    }
    async onModuleInit() {
        await class_validator_1.validateOrReject(this.config);
    }
    saveConfig(value = this.config) {
        fs.writeJSONSync(this.configPath, class_transformer_1.classToPlain(value, this.transformOpts), { spaces: 2 });
    }
    loadConfig() {
        if (!fs.existsSync(this.configPath)) {
            const configDto = new config_dto_1.ConfigDto();
            fs.writeJSONSync(this.configPath, class_transformer_1.classToPlain(configDto, this.transformOpts), { spaces: 2 });
            return configDto;
        }
        else {
            const configJson = fs.readJsonSync(this.configPath);
            const configDto = class_transformer_1.plainToClass(config_dto_1.ConfigDto, configJson, this.transformOpts);
            return configDto;
        }
    }
};
_config = new WeakMap();
ConfigService = __decorate([
    common_1.Injectable(),
    __metadata("design:paramtypes", [])
], ConfigService);
exports.ConfigService = ConfigService;
//# sourceMappingURL=config.service.js.map