import { a as __exportAll } from "./runner-K8XK101B.mjs";
import { t as after } from "./after-B1IIdH3Y.mjs";
import { getRequestContext } from "./request-context.mjs";
import { r as requestCached } from "./request-cache-KCNHp_RJ.mjs";
import { n as BylineSchemaRegistry } from "./byline-registry-BOjqDOim.mjs";
import { n as initWithLock, t as createInitLock } from "./init-lock-6b309ZrF.mjs";

//#region src/bylines/field-defs-cache.ts
var field_defs_cache_exports = /* @__PURE__ */ __exportAll({ getBylineFieldDefs: () => getBylineFieldDefs });
const HOLDER_KEY = Symbol.for("emdash:byline-field-defs");
const g = globalThis;
const holder = g[HOLDER_KEY] ?? (() => {
	const h = {
		value: null,
		hasValue: false,
		cachedVersion: -1,
		lock: createInitLock()
	};
	g[HOLDER_KEY] = h;
	return h;
})();
const REQUEST_CACHE_KEY_VERSION = "byline-fields-version";
const REQUEST_CACHE_KEY_DEFS_PREFIX = "byline-field-defs:";
/**
* Reclaim window for the single-flight lock: if an owner holds it past
* this without publishing (e.g. its request was cancelled and the
* anchored fetch hasn't completed yet), the next reader reclaims and
* refetches. `listFields` is a single fast SELECT, so this only needs to
* cover a genuinely slow/stranded query. Mutable solely so tests can
* shorten it; production never changes it.
*/
let reclaimDeadlineMs = 1e4;
/**
* Read the persisted `options.byline_fields_version` counter. Cached for
* the duration of the current request via `requestCached`. Returns `0`
* when the row is missing (matches `BylineSchemaRegistry.getVersion`).
*/
async function getBylineFieldsVersion(db) {
	return requestCached(REQUEST_CACHE_KEY_VERSION, () => new BylineSchemaRegistry(db).getVersion());
}
/**
* Resolve registered byline custom-field definitions. Two-tier cache:
* per-request via `requestCached`, then per-isolate via the global
* holder.
*
* The global holder is bypassed for isolated requests (playground / DO
* preview, which point at a divergent schema) and for dirty versions
* (odd counter — see `BylineSchemaRegistry`'s class JSDoc — indicates
* an in-flight or crashed mutation). Both bypass paths still hit the
* per-request cache, so a single render dedupes within itself.
*
* Always returns an array. Empty = no custom fields registered.
*/
async function getBylineFieldDefs(db) {
	const isolated = getRequestContext()?.dbIsIsolated === true;
	const version = await getBylineFieldsVersion(db);
	const dirty = version % 2 !== 0;
	return requestCached(`${REQUEST_CACHE_KEY_DEFS_PREFIX}${version}`, async () => {
		if (isolated || dirty) return new BylineSchemaRegistry(db).listFields();
		return initWithLock(holder.lock, () => holder.hasValue && holder.cachedVersion === version ? holder.value : null, (isCurrentClaim) => (async () => {
			const defs = await new BylineSchemaRegistry(db).listFields();
			if (isCurrentClaim() && version >= holder.cachedVersion) {
				holder.value = defs;
				holder.hasValue = true;
				holder.cachedVersion = version;
			}
			return defs;
		})(), {
			deadlineMs: reclaimDeadlineMs,
			anchor: (promise) => after(() => promise)
		});
	});
}

//#endregion
export { getBylineFieldDefs as n, field_defs_cache_exports as t };
//# sourceMappingURL=field-defs-cache-WUI88LX5.mjs.map