//#region imports
import { crossPlatformPath, dotTaonFolder, Helpers__NS__error, Helpers__NS__mkdirp, Helpers__NS__writeJson, UtilsOs__NS__getRealHomeDir } from 'tnp-core/lib-prod';
import { JSONFilePreset } from 'tnp-core/lib-prod'; // @backend
//#endregion
export 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 = crossPlatformPath([
            UtilsOs__NS__getRealHomeDir(),
            `${dotTaonFolder}/apps/${this.dbName}-db/${this.ins.cliToolNameFn()}`,
        ]);
        try {
            Helpers__NS__mkdirp(userFolder);
        }
        catch (error) { }
        return crossPlatformPath([userFolder, 'db.json']);
        //#endregion
    }
    //#region @backend
    async getConnection() {
        //#region @backendFunc
        const dbLocation = this.projectsDbLocation;
        // console.log({ dbLocation })
        try {
            this.lowDB = await JSONFilePreset(dbLocation, this.defaultDb);
        }
        catch (error) {
            Helpers__NS__error(`[taon-helpers] Cannot use db.json file for projects in location, restoring default db.`, true, true);
            Helpers__NS__writeJson(dbLocation, this.defaultDb);
            this.lowDB = await JSONFilePreset(dbLocation, this.defaultDb);
        }
        return this.lowDB;
        //#endregion
        // @ts-ignore
        return void 0;
    }
}
//# sourceMappingURL=c:/Users/darek/projects/npm/taon-dev/tnp-helpers/dist-nocutsrc-prod/lib/base/classes/base-db.js.map