/** * Global FHIR version context provider. * * Initialized once per generation run via {@link initVersionContext}. * All emitters, parsers, and generators call {@link ctx} (or the typed * helpers below) instead of hard-coding version-specific imports. * * This is intentionally a singleton; generation is single-threaded. */ import type { FhirRules, FhirVersionContext, FhirVersionSlug } from './types.js'; import { resolveVersionContext, DEFAULT_FHIR_VERSION } from './types.js'; let _current: FhirVersionContext | null = null; /** * Resolve and set the global version context. * Must be called before any generation work begins. */ export async function initVersionContext( slug: FhirVersionSlug = DEFAULT_FHIR_VERSION, ): Promise { _current = await resolveVersionContext(slug); return _current; } /** * Return the active version context. * Throws if {@link initVersionContext} has not been called. */ export function ctx(): FhirVersionContext { if (!_current) { throw new Error( 'FHIR version context not initialised. Call initVersionContext() first.', ); } return _current; } /** Shorthand – the rules object from the active context. */ export function getRules(): FhirRules { return ctx().rules; } /** The `fhir/` import specifier for generated output code. */ export function fhirModuleSpecifier(): string { return `fhir/${ctx().versionSlug}`; } /** FHIR version slug (e.g. 'r4', 'r4b') */ export function versionSlug(): FhirVersionSlug { return ctx().versionSlug; } /** * FHIRPath model slug for the active version. * * `fhirpath` only ships `fhir-context/` models for dstu2, stu3, r4 and r5 — * there is no r4b model. R4B is a maintenance release of R4 whose FHIRPath * type model is unchanged, so R4B validators load the r4 model. * * Emitted as `fhirpath/fhir-context//index.js`; the trailing filename is * required because fhirpath >= 5 declares an `exports` map and extensionless * deep paths no longer resolve. */ export function fhirpathModelSlug(): string { return ctx().versionSlug === 'r4b' ? 'r4' : ctx().versionSlug; } /** Full semver version (e.g. '4.0.1', '4.3.0') */ export function versionString(): string { return ctx().version; } /** * Reset the provider (for testing). * @internal */ export function resetVersionContext(): void { _current = null; }