"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigDatabase = exports.SshHttpsPreferenceConfig = exports.CodeEditorConfig = exports.ConfigDatabaseEx = void 0;
//#region imports
const lib_1 = require("tnp-core/lib");
const index_1 = require("../index");
const base_db_1 = require("./classes/base-db");
//#endregion
const defaultDb = {
    config: {},
};
//#region config database ext
class ConfigDatabaseEx {
    KEY_PATH_DB;
    configDb;
    constructor(KEY_PATH_DB, configDb) {
        this.KEY_PATH_DB = KEY_PATH_DB;
        this.configDb = configDb;
    }
    //#region get value
    async getValue() {
        //#region @backendFunc
        const db = await this.configDb.getConnection();
        let editor = db.data.config[this.KEY_PATH_DB];
        return editor;
        //#endregion
    }
    //#endregion
    //#region set value
    async setValue(v) {
        //#region @backendFunc
        const db = await this.configDb.getConnection();
        await db.update(data => (data.config[this.KEY_PATH_DB] = v));
        return v;
        //#endregion
    }
    //#endregion
    //#region select one value
    async selectOneValue(arrOfValues, question = 'Select default global value') {
        //#region @backendFunc
        let value = await index_1.HelpersTaon.consoleGui.select(question, arrOfValues.map(name => {
            return {
                name,
                value: name,
            };
        }));
        return value;
        //#endregion
    }
}
exports.ConfigDatabaseEx = ConfigDatabaseEx;
//#endregion
//#region code editor config
class CodeEditorConfig extends ConfigDatabaseEx {
    async getValue() {
        //#region @backendFunc
        let editor = await super.getValue();
        if (!editor) {
            editor = await this.selectCodeEditor();
        }
        return editor;
        //#endregion
    }
    async selectCodeEditor() {
        //#region @backendFunc
        let editor = await this.selectOneValue(lib_1.UtilsOs.EditorArr, 'Select default code editor');
        await this.setValue(editor);
        return editor;
        //#endregion
    }
}
exports.CodeEditorConfig = CodeEditorConfig;
//#endregion
//#region ssh https preference config
class SshHttpsPreferenceConfig extends ConfigDatabaseEx {
    async getValue() {
        //#region @backendFunc
        let editor = await super.getValue();
        if (!editor) {
            editor = await this.selectHttpOrSsh();
        }
        return editor;
        //#endregion
    }
    async selectHttpOrSsh() {
        //#region @backendFunc
        let editor = await this.selectOneValue(lib_1.CoreModels.GitConnectionArr, 'Select  default git connection type');
        await this.setValue(editor);
        return editor;
        //#endregion
    }
}
exports.SshHttpsPreferenceConfig = SshHttpsPreferenceConfig;
//#endregion
class ConfigDatabase extends base_db_1.BaseDb {
    constructor(ins) {
        super(ins, 'config', defaultDb);
        // const editor = await this.codeEditor.getValue
    }
    codeEditor = new CodeEditorConfig('selected-code-editor', this);
    httpOrHttps = new CodeEditorConfig('selected-git-connection-type', this);
}
exports.ConfigDatabase = ConfigDatabase;
//# sourceMappingURL=config-database.js.map