"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseDb = void 0;
//#region imports
const lib_1 = require("tnp-core/lib");
const lib_2 = require("tnp-core/lib"); // @backend
//#endregion
class BaseDb {
    ins;
    dbName;
    defaultDb;
    constructor(ins, dbName, defaultDb) {
        this.ins = ins;
        this.dbName = dbName;
        this.defaultDb = defaultDb;
    }
    //#region @backend
    lowDB;
    //#endregion
    get isUsingDB() {
        //#region @backendFunc
        return !!this.lowDB;
        //#endregion
    }
    get projectsDbLocation() {
        //#region @backendFunc
        const userFolder = (0, lib_1.crossPlatformPath)([
            lib_1.UtilsOs.getRealHomeDir(),
            `${lib_1.dotTaonFolder}/apps/${this.dbName}-db/${this.ins.cliToolNameFn()}`,
        ]);
        try {
            lib_1.Helpers.mkdirp(userFolder);
        }
        catch (error) { }
        return (0, lib_1.crossPlatformPath)([userFolder, 'db.json']);
        //#endregion
    }
    //#region @backend
    async getConnection() {
        //#region @backendFunc
        const dbLocation = this.projectsDbLocation;
        // console.log({ dbLocation })
        try {
            this.lowDB = await (0, lib_2.JSONFilePreset)(dbLocation, this.defaultDb);
        }
        catch (error) {
            lib_1.Helpers.error(`[taon-helpers] Cannot use db.json file for projects in location, restoring default db.`, true, true);
            lib_1.Helpers.writeJson(dbLocation, this.defaultDb);
            this.lowDB = await (0, lib_2.JSONFilePreset)(dbLocation, this.defaultDb);
        }
        return this.lowDB;
        //#endregion
        // @ts-ignore
        return void 0;
    }
}
exports.BaseDb = BaseDb;
//# sourceMappingURL=base-db.js.map