'use strict';
import { isTest } from '../../fd-toolbox/lib/environments.js';
import { apiInternalServerError } from '../../fd-toolbox/server/web/errors.js';
import { getDbType, getDbConnectionString } from './db-config.js';
import { dbTypes } from '../../base-repo/constants/tools-constants.js';
import { PostgresAdapter } from './adapters/postgres-adapter.js';
import { SqliteAdapter } from './adapters/sqlite-adapter.js';
import { createError } from '../../fd-toolbox/errors/errors.js';
import { setDbSchema } from './db-schema.js';
import { transactionCantBeCreated } from '../../base-repo/app-constants/internal-system-constants.js';

var __defProp=Object['defineProperty'],__name=(_0x31d3b2,_0x5d4e7a)=>__defProp(_0x31d3b2,'name',{'value':_0x5d4e7a,'configurable':!![]});let isAppDbInitialized=![];function initializeDb(_0x320e62){setDbSchema(_0x320e62),getDbAdapter()['init'](_0x320e62);}__name(initializeDb,'initializeDb');function initAppDb(_0x1f586){if(isAppDbInitialized)return;initializeDb(_0x1f586),isAppDbInitialized=!![];}__name(initAppDb,'initAppDb');function getDb(){return getDbAdapter()['getDb']();}__name(getDb,'getDb');function createDynamicSelect(_0x23b571,_0x4d179b){return getDbAdapter()['createDynamicSelect'](_0x23b571,_0x4d179b);}__name(createDynamicSelect,'createDynamicSelect');function getQueryConfig(_0x1862c8){return getDbAdapter()['getQueryConfig'](_0x1862c8);}__name(getQueryConfig,'getQueryConfig');function dbLike(_0x21cea8,_0x2447a6){return getDbAdapter()['getLikeExpression'](_0x21cea8,_0x2447a6);}__name(dbLike,'dbLike');function dbExtractDate(_0x3f0c3e){return getDbAdapter()['getExtractDateExpression'](_0x3f0c3e);}__name(dbExtractDate,'dbExtractDate');function dbLeftJoin(_0xd1962,_0x3153ab,_0xdd50da){return getDbAdapter()['leftJoin'](_0xd1962,_0x3153ab,_0xdd50da);}__name(dbLeftJoin,'dbLeftJoin');function dbInnerJoin(_0x111720,_0x378eeb,_0x396758){return getDbAdapter()['innerJoin'](_0x111720,_0x378eeb,_0x396758);}__name(dbInnerJoin,'dbInnerJoin');function dbOrderBy(_0x1a103a,..._0x30756b){return getDbAdapter()['orderBy'](_0x1a103a,..._0x30756b);}__name(dbOrderBy,'dbOrderBy');function dbGroupBy(_0x1284b3,..._0x6fdedc){return getDbAdapter()['groupBy'](_0x1284b3,..._0x6fdedc);}__name(dbGroupBy,'dbGroupBy');function dbHaving(_0x451c30,_0x41a048){return getDbAdapter()['having'](_0x451c30,_0x41a048);}__name(dbHaving,'dbHaving');function dbLimit(_0x351429,_0x1a9597){return getDbAdapter()['limit'](_0x351429,_0x1a9597);}__name(dbLimit,'dbLimit');function dbOffset(_0x5d1f68,_0x1f00d9){return getDbAdapter()['offset'](_0x5d1f68,_0x1f00d9);}__name(dbOffset,'dbOffset');function dbWhere(_0x4157f2,_0x3c1ec1){return getDbAdapter()['where'](_0x4157f2,_0x3c1ec1);}__name(dbWhere,'dbWhere');function dbInsert(_0x15f03f){return getDbAdapter()['insert'](_0x15f03f);}__name(dbInsert,'dbInsert');function dbUpdate(_0x42627f){return getDbAdapter()['update'](_0x42627f);}__name(dbUpdate,'dbUpdate');function dbDelete(_0x2f8332){return getDbAdapter()['delete'](_0x2f8332);}__name(dbDelete,'dbDelete');async function createDb(){await getDbAdapter()['createDatabaseIfNotExists']();}__name(createDb,'createDb');async function dropDb(){await getDbAdapter()['dropDatabase']();}__name(dropDb,'dropDb');async function emptyAllTables(){await getDbAdapter()['emptyAllTables']();}__name(emptyAllTables,'emptyAllTables');async function migrate(){await getDbAdapter()['runMigrations']();}__name(migrate,'migrate');async function generateMigrations(_0x1e7a21){await getDbAdapter()['generateMigrations'](_0x1e7a21);}__name(generateMigrations,'generateMigrations');async function beginUnsafeTransactionOnlyForTest(){if(isTest())await getDbAdapter()['beginUnsafeTransaction']();else throw apiInternalServerError(transactionCantBeCreated);}__name(beginUnsafeTransactionOnlyForTest,'beginUnsafeTransactionOnlyForTest');async function rollbackUnsafeTransactionOnlyForTest(){if(isTest())await getDbAdapter()['rollbackUnsafeTransaction']();else throw apiInternalServerError(transactionCantBeCreated);}__name(rollbackUnsafeTransactionOnlyForTest,'rollbackUnsafeTransactionOnlyForTest');let dbAdapter;function getDbAdapter(){return dbAdapter??=createDbAdapter(getDbType(),getDbConnectionString()),dbAdapter;}__name(getDbAdapter,'getDbAdapter');function createDbAdapter(_0x24d6d1,_0xfd97cb){if(_0x24d6d1===dbTypes['postgres'])return new PostgresAdapter(_0xfd97cb);else {if(_0x24d6d1===dbTypes['sqlite'])return new SqliteAdapter(_0xfd97cb);else throw createError('Unsupported\x20DB\x20type:\x20'+_0x24d6d1);}}__name(createDbAdapter,'createDbAdapter');

export { beginUnsafeTransactionOnlyForTest, createDb, createDynamicSelect, dbDelete, dbExtractDate, dbGroupBy, dbHaving, dbInnerJoin, dbInsert, dbLeftJoin, dbLike, dbLimit, dbOffset, dbOrderBy, dbUpdate, dbWhere, dropDb, emptyAllTables, generateMigrations, getDb, getQueryConfig, initAppDb, initializeDb, migrate, rollbackUnsafeTransactionOnlyForTest };
