import{logger as l}from"../../tools/notifiers/logger.js";import{DatabaseClient as p}from"./client.js";import{DatabasePathGenerator as d}from"./database-path-generator.js";import{retryAsyncOperation as h}from"./utils/retry-async-operation.js";class u{async initialize({baseDbDir:a,databaseName:t,additionalFolder:e}){const{databasePath:i}=await d.prepareDatabasePath({baseDir:a,databaseName:t,additionalFolder:e});return{client:await p.init({url:`file:${i}`}),path:i}}}class w{async initialize({removeExisting:a,baseDbDir:t,runWithPragmaWalWriteOptimization:e,databaseName:i,additionalFolder:r,migrationsFolder:c}){l.info("Initializing local database client...");const m=l.startTiming(),{databasePath:s}=await d.prepareDatabasePath({baseDir:t,databaseName:i,additionalFolder:r,removeExisting:a}),o={url:`file:${s}`},n=await p.init(o,{runWithPragmaWalWriteOptimization:e});return await n.migrate(t,c),l.infoTime(m,"Local database client initialized"),{client:n,path:s}}}class D{#t;#a;constructor(a){this.#t=a?.skipLogs??!1,this.#a=this.#t?void 0:l}async initialize({removeExisting:a,baseDbDir:t,syncUrl:e,authToken:i,databaseName:r,additionalFolder:c,migrationsFolder:m}){this.#a?.info("Initializing remote database client...");const s=this.#a?.startTiming();return await h(async()=>{const{databasePath:o}=await d.prepareDatabasePath({baseDir:t,databaseName:r,additionalFolder:c,removeExisting:a}),n=await p.init({url:`file:${o}`,syncUrl:e,authToken:i});return await n.migrate(t,m),this.#a?.infoTime(s??"","Remote database client initialized"),{client:n,path:o}},{maxRetries:3,retryStrategy:"exponential",delay:1e3,logger:this.#a,messages:{retry:"Retrying remote database client initialization",maxRetriesReached:"Max retries reached, remote database client initialization failed"}})}}export{u as DefaultDatabaseInitializationStrategy,w as LocalDatabaseInitializationStrategy,D as RemoteDatabaseInitializationStrategy};
