import { createRequire } from "node:module";
const nodeRequire = createRequire(import.meta.url);
let DatabaseCtor;
let hasNativeSqlite = false;
try {
    // Suppress the ExperimentalWarning emitted by node:sqlite on Node 22+.
    const originalEmit = process.emit.bind(process);
    process.emit = function (event, ...args) {
        if (event === "warning" &&
            args[0] != null &&
            typeof args[0].name === "string" &&
            args[0].name === "ExperimentalWarning" &&
            typeof args[0].message === "string" &&
            args[0].message.includes("SQLite")) {
            return false;
        }
        return originalEmit(event, ...args);
    };
    const nativeModule = nodeRequire("node:sqlite");
    process.emit = originalEmit;
    DatabaseCtor = nativeModule.DatabaseSync;
    hasNativeSqlite = true;
}
catch {
    DatabaseCtor = nodeRequire("better-sqlite3");
}
function normalizeParams(params) {
    if (params.length === 1 && Array.isArray(params[0])) {
        return params[0];
    }
    return params;
}
class SqliteStatementImpl {
    statement;
    constructor(statement) {
        this.statement = statement;
    }
    run(...params) {
        return this.statement.run(...normalizeParams(params));
    }
    get(...params) {
        return this.statement.get(...normalizeParams(params));
    }
    all(...params) {
        return this.statement.all(...normalizeParams(params));
    }
}
class SqliteDatabaseImpl {
    db;
    constructor(path) {
        this.db = new DatabaseCtor(path);
    }
    prepare(sql) {
        return new SqliteStatementImpl(this.db.prepare(sql));
    }
    exec(sql) {
        this.db.exec(sql);
    }
    pragma(command) {
        if (typeof this.db.pragma === "function") {
            return this.db.pragma(command);
        }
        this.db.exec(`PRAGMA ${command}`);
        return [];
    }
    transaction(fn) {
        if (typeof this.db.transaction === "function" && !hasNativeSqlite) {
            return this.db.transaction(fn);
        }
        return (...args) => {
            this.db.exec("BEGIN");
            try {
                const result = fn(...args);
                this.db.exec("COMMIT");
                return result;
            }
            catch (error) {
                try {
                    this.db.exec("ROLLBACK");
                }
                catch {
                    // Ignore rollback errors during unwind.
                }
                throw error;
            }
        };
    }
    close() {
        this.db.close();
    }
}
export default class Database extends SqliteDatabaseImpl {
}
//# sourceMappingURL=sqlite_driver.js.map