import { CompiledQuery } from "kysely";
import { ConnectionMutex } from "./connection-mutex.js";
import { SqliteWasmConnection } from "./sqlite-wasm-connection.js";
export class SqliteWasmDriver {
    #config;
    #connectionMutex = new ConnectionMutex();
    #db;
    #connection;
    constructor(config) {
        // this.#config = freeze({ ...config })
        this.#config = { ...config };
    }
    async init() {
        this.#db =
            typeof this.#config.database === "function"
                ? await this.#config.database()
                : this.#config.database;
        this.#connection = new SqliteWasmConnection(this.#db);
        if (this.#config.onCreateConnection) {
            await this.#config.onCreateConnection(this.#connection);
        }
    }
    async acquireConnection() {
        // SQLite only has one single connection. We use a mutex here to wait
        // until the single connection has been released.
        await this.#connectionMutex.lock();
        return this.#connection;
    }
    async beginTransaction(connection) {
        await connection.executeQuery(CompiledQuery.raw("begin"));
    }
    async commitTransaction(connection) {
        await connection.executeQuery(CompiledQuery.raw("commit"));
    }
    async rollbackTransaction(connection) {
        await connection.executeQuery(CompiledQuery.raw("rollback"));
    }
    async releaseConnection() {
        this.#connectionMutex.unlock();
    }
    async destroy() {
        this.#db?.close();
    }
}
//# sourceMappingURL=sqlite-wasm-driver.js.map