import { getLogger } from "./logger.js";

//#region src/utils/database-pool.ts
const logger = getLogger("database-pool");
let _importPromise;
async function getAttachDatabasePool() {
	if (_importPromise) return _importPromise;
	_importPromise = (async () => {
		if (!process.env.VERCEL) return void 0;
		try {
			return (await import("@vercel/functions")).attachDatabasePool;
		} catch (e) {
			logger.warn({ error: e }, "Failed to import @vercel/functions, database pool attachment unavailable");
			return;
		}
	})();
	return _importPromise;
}
async function tryAttachDatabasePool(dbPool) {
	const attachDatabasePool = await getAttachDatabasePool();
	if (!attachDatabasePool) return;
	try {
		attachDatabasePool(dbPool);
	} catch (e) {
		logger.warn({ error: e }, "Failed to attach database pool to Vercel Fluid Compute");
	}
}
/**
* Reset internal cache. Exposed only for testing.
*/
function _resetAttachDatabasePoolCache() {
	_importPromise = void 0;
}

//#endregion
export { _resetAttachDatabasePoolCache, tryAttachDatabasePool };