/**
 * Runtime-agnostic SQLite shim.
 *
 * Uses `bun:sqlite` when running under Bun, `better-sqlite3` under Node.js.
 * Both expose the same subset we need (.exec, .prepare, .close) so the rest
 * of the gateway code never needs to know which engine is active.
 *
 * Drizzle ORM has matching adapters for both backends; `createDrizzle()`
 * returns the correct one.
 */
import * as schema from "./schema.js";
// ── Runtime detection ──────────────────────────────────────────────────────
const isBun = typeof globalThis.Bun !== "undefined";
// ── Factory: open a raw SQLite database ────────────────────────────────────
/**
 * Open a raw SQLite database using whichever native driver is available.
 * The returned handle satisfies `SqliteDatabase` in both runtimes.
 */
export async function openRawSqlite(path) {
    if (isBun) {
        // @ts-ignore — bun:sqlite only exists at runtime under Bun
        const { Database } = await import("bun:sqlite");
        return new Database(path);
    }
    else {
        const mod = await import("better-sqlite3");
        const Database = mod.default;
        return new Database(path);
    }
}
/**
 * Wrap a raw SQLite handle with the matching Drizzle ORM adapter.
 * Returns a fully-typed Drizzle instance regardless of runtime.
 */
export async function createDrizzle(rawDb) {
    if (isBun) {
        const { drizzle } = await import("drizzle-orm/bun-sqlite");
        // BunSQLiteDatabase and BetterSQLite3Database differ in RunResult type
        // but are structurally compatible for all query-builder operations.
        return drizzle(rawDb, { schema });
    }
    else {
        const { drizzle } = await import("drizzle-orm/better-sqlite3");
        return drizzle(rawDb, { schema });
    }
}
//# sourceMappingURL=sqlite-shim.js.map