import { a as applyPerfPragmas } from './dual-scope-db-ATrwq0LH.js';
import './paths-2M_UxD_H.js';
import 'node:fs';
import 'node:path';
import { createRequire } from 'node:module';
import 'node:fs/promises';

/**
* Open a SQLite database file as a read-only snapshot, applying the
* canonical pragma SSoT but skipping migrations and singleton management.
*
* ## When to use
*
* - Backup verification (e.g. `migration/checksum.ts`)
* - Atomic / read-side database validation (e.g. `store/atomic.ts`)
* - Short-lived registry reads from a non-CLEO process (e.g. Studio
*   SvelteKit endpoints that read nexus.db for project listings)
*
* Do NOT use for the long-lived role databases — those go through
* {@link openCleoDb} which manages singletons + migrations.
*
* ## Pragma application
*
* When `applyPragmas !== false` (default), the canonical performance pragmas
* are applied via `applyPerfPragmas`. For read-only handles, `enableWal` is
* forced `false` because WAL mode is unsettable on a read-only connection.
*
* ## Lifecycle
*
* The handle is caller-owned — call `handle.close()` when done. Unlike
* {@link openCleoDb}, the snapshot opener does NOT participate in any
* singleton cache, so leaking a snapshot handle leaks a file descriptor.
*
* @task T9685
* @adr ADR-068, ADR-069
*
* @example
* ```typescript
* import { openCleoDbSnapshot } from '@cleocode/core/store/open-cleo-db';
*
* const snap = openCleoDbSnapshot('/path/to/cleo.db');
* try {
*   const rows = snap.db.prepare('SELECT * FROM nexus_project_registry').all();
*   // ...
* } finally {
*   snap.close();
* }
* ```
*/
function openCleoDbSnapshot(path, options = {}) {
	const { readOnly = true, applyPragmas = true } = options;
	const { DatabaseSync: DatabaseSyncCtor } = createRequire(import.meta.url)("node:sqlite");
	const db = new DatabaseSyncCtor(path, { readOnly });
	if (applyPragmas) applyPerfPragmas(db, { enableWal: !readOnly });
	let closed = false;
	return {
		db,
		path,
		close() {
			if (closed) return;
			closed = true;
			try {
				db.close();
			} catch {}
		}
	};
}

export { openCleoDbSnapshot as o };
//# sourceMappingURL=open-cleo-db-Ba2LY1h4.js.map
