import BetterSqlite3 from "better-sqlite3";
import { Kysely, SqliteDialect } from "kysely";

//#region src/database/connection.ts
var EmDashDatabaseError = class extends Error {
	constructor(message, cause) {
		super(message);
		this.cause = cause;
		this.name = "EmDashDatabaseError";
	}
};
/**
* Creates a Kysely database instance
* Supports:
* - file:./path/to/db.sqlite (local SQLite)
* - :memory: (in-memory SQLite for testing)
* - libsql://... (Turso/libSQL with auth token) - TODO
*/
function createDatabase(config) {
	try {
		if (config.url.startsWith("file:") || config.url === ":memory:") {
			const sqlite = new BetterSqlite3(config.url === ":memory:" ? ":memory:" : config.url.replace("file:", ""));
			sqlite.pragma("journal_mode = WAL");
			sqlite.pragma("foreign_keys = ON");
			return new Kysely({ dialect: new SqliteDialect({ database: sqlite }) });
		}
		if (config.url.startsWith("libsql:")) {
			if (!config.authToken) throw new EmDashDatabaseError("Auth token required for remote libSQL database");
			throw new EmDashDatabaseError("LibSQL not yet implemented");
		}
		throw new EmDashDatabaseError(`Unsupported database URL scheme: ${config.url}`);
	} catch (error) {
		if (error instanceof EmDashDatabaseError) throw error;
		throw new EmDashDatabaseError("Failed to create database", error);
	}
}

//#endregion
export { createDatabase as n, EmDashDatabaseError as t };
//# sourceMappingURL=connection-B4zVnQIa.mjs.map