import {
  __esm,
  __export,
  __require
} from "./chunk-8sqjbsgt.js";

// packages/core/src/db/better-sqlite-adapter.ts
var exports_better_sqlite_adapter = {};
__export(exports_better_sqlite_adapter, {
  betterSqliteFactory: () => betterSqliteFactory
});

class BetterSqliteAdapter {
  db;
  isOpen = true;
  constructor(Ctor, path) {
    this.db = new Ctor(path);
  }
  get open() {
    return this.isOpen && this.db.open;
  }
  exec(sql) {
    this.db.exec(sql);
  }
  prepare(sql) {
    const stmt = this.db.prepare(sql);
    return {
      all: (...p) => stmt.all(...p),
      get: (...p) => stmt.get(...p) ?? null,
      run: (...p) => {
        const r = stmt.run(...p);
        return { changes: Number(r.changes), lastInsertRowid: r.lastInsertRowid };
      }
    };
  }
  pragma(statement) {
    const trimmed = statement.trim();
    return this.db.pragma(trimmed);
  }
  transaction(fn) {
    return this.db.transaction(fn);
  }
  loadExtension(path) {
    this.db.loadExtension(path);
  }
  close() {
    if (!this.isOpen)
      return;
    this.db.close();
    this.isOpen = false;
  }
}
var cached = null, betterSqliteFactory;
var init_better_sqlite_adapter = __esm(() => {
  betterSqliteFactory = {
    open: (path) => {
      if (!cached) {
        const mod = __require("better-sqlite3") ?? {};
        const Ctor = mod.default ?? mod;
        if (typeof Ctor !== "function") {
          throw new Error("rig: 'better-sqlite3' is missing. Install it with `npm i better-sqlite3` or run on Bun.");
        }
        cached = { open: (p) => new BetterSqliteAdapter(Ctor, p) };
      }
      return cached.open(path);
    }
  };
});

export { betterSqliteFactory, exports_better_sqlite_adapter, init_better_sqlite_adapter };
