//#region imports
import { Helpers, UtilsOs, crossPlatformPath, dotTaonFolder, } from 'tnp-core/lib';
import { JSONFilePreset } from 'tnp-core/lib'; // @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.getRealHomeDir(),
            `${dotTaonFolder}/apps/${this.dbName}-db/${this.ins.cliToolNameFn()}`,
        ]);
        try {
            Helpers.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.error(`[taon-helpers] Cannot use db.json file for projects in location, restoring default db.`, true, true);
            Helpers.writeJson(dbLocation, this.defaultDb);
            this.lowDB = await JSONFilePreset(dbLocation, this.defaultDb);
        }
        return this.lowDB;
        //#endregion
        // @ts-ignore
        return void 0;
    }
}
//# sourceMappingURL=base-db.js.map