import{logger as n}from"../../tools/notifiers/logger.js";import{envConfig as r}from"../../config/env-config.js";import{SQLITE_DATABASE_MIGRATIONS_FOLDER as o,SQLITE_LOCAL_DATABASE_FOLDER as c,SQLITE_REMOTE_DATABASE_FOLDER as l,SQLITE_DATABASE_NAME as i}from"./constants.js";import{DatabaseConnectionsManager as s}from"./database-connections-manager.js";import{SQLD_REMOTE_DATABASE_URL_NO_DEPLOYMENT_YET_VAR as T}from"../../constants/plugins/catalog-entities.js";class u{static async create(e){return this.#r(e)==="local"?await this.#t(e):await this.#a(e)}static async#t(e){return await s.getLocalConnection({...e,databaseName:i,additionalFolder:c,migrationsFolder:o})}static async#a(e){try{const{syncUrl:t,authToken:a}=this.#e(e);return!t||!a?(r.isDevelopMode||n.warn("Your remote database is not initialized yet, please wait for production deployment."),null):await s.getRemoteConnection({...e,databaseName:i,additionalFolder:l,migrationsFolder:o,syncUrl:t,authToken:a})}catch(t){return n.error("Remote database connection initialization failed",t),null}}static#r(e){if(e.databaseType)return e.databaseType;const{syncUrl:t,authToken:a}=this.#e(e);return t&&a?"remote":"local"}static#e(e){const t=r.SQLD_REMOTE_DATABASE_URL||e.sqldRemoteDatabaseUrl,a=r.SQLD_REMOTE_DATABASE_AUTH_TOKEN||e.sqldRemoteDatabaseAuthToken;return{syncUrl:this.#n(t),authToken:a}}static#n(e){return e===T?void 0:e}}export{u as DatabaseConnectionFactory};
