import { createRequire } from "node:module";
var __require = /* @__PURE__ */ createRequire(import.meta.url);

// src/sqlite/database.ts
import { dirname } from "node:path";
import { mkdirSync, existsSync } from "node:fs";
import { createRequire as createRequire2 } from "node:module";
function ensureDir(path) {
  if (path === ":memory:")
    return;
  const dir = dirname(path);
  if (!existsSync(dir)) {
    mkdirSync(dir, { recursive: true });
  }
}
function isBun() {
  return typeof Bun !== "undefined";
}
function hasNodeSqlite() {
  return typeof globalThis.process?.getBuiltinModule === "function" && globalThis.process.getBuiltinModule("node:sqlite") !== undefined;
}
function hasBetterSqlite3() {
  try {
    const req = createRequire2(import.meta.url);
    req("better-sqlite3");
    return true;
  } catch {
    return false;
  }
}
function openDatabase(path) {
  ensureDir(path);
  if (isBun()) {
    return openBunDatabase(path);
  }
  if (hasNodeSqlite()) {
    return openNodeDatabase(path);
  }
  if (hasBetterSqlite3()) {
    return openBetterSqlite3Database(path);
  }
  throw new Error("No SQLite runtime available. Requires Bun, Node.js 22+ with built-in sqlite, or better-sqlite3.");
}
function openBunDatabase(path) {
  const { Database: BunDatabase } = __require("bun:sqlite");
  const db = new BunDatabase(path);
  return {
    exec(sql) {
      db.exec(sql);
    },
    prepare(sql) {
      const stmt = db.prepare(sql);
      return {
        run(...params) {
          const result = stmt.run(...params);
          return {
            changes: result.changes,
            lastInsertRowid: result.lastInsertRowid
          };
        },
        get(...params) {
          return stmt.get(...params);
        },
        all(...params) {
          return stmt.all(...params);
        }
      };
    },
    close() {
      db.close();
    }
  };
}
function openNodeDatabase(path) {
  const nodeSqlite = globalThis.process.getBuiltinModule("node:sqlite");
  const { DatabaseSync } = nodeSqlite;
  const db = new DatabaseSync(path);
  return {
    exec(sql) {
      db.exec(sql);
    },
    prepare(sql) {
      const stmt = db.prepare(sql);
      return {
        run(...params) {
          const result = stmt.run(...params);
          return {
            changes: result.changes,
            lastInsertRowid: result.lastInsertRowid
          };
        },
        get(...params) {
          return stmt.get(...params);
        },
        all(...params) {
          return stmt.all(...params);
        }
      };
    },
    close() {
      db.close();
    }
  };
}
function openBetterSqlite3Database(path) {
  const req = createRequire2(import.meta.url);
  const BetterSqlite3 = req("better-sqlite3");
  const db = new BetterSqlite3(path);
  return {
    exec(sql) {
      db.exec(sql);
    },
    prepare(sql) {
      const stmt = db.prepare(sql);
      return {
        run(...params) {
          const result = stmt.run(...params);
          return {
            changes: result.changes,
            lastInsertRowid: result.lastInsertRowid
          };
        },
        get(...params) {
          return stmt.get(...params);
        },
        all(...params) {
          return stmt.all(...params);
        }
      };
    },
    close() {
      db.close();
    }
  };
}
export {
  openDatabase
};

//# debugId=96C812BA25A48F8164756E2164756E21
