import { RstGeneratorError } from '../../Generator/RstGeneratorError.ts' import type { RstGeneratorState } from '../../Generator/RstGeneratorState.ts' import type { RstSubstitutionDef } from '../../RstNode/ExplicitMarkup/SubstitutionDef.ts' import type { RstSubstitutionRef } from '../../RstNode/Inline/SubstitutionRef.ts' export class SubstitutionResolver { private readonly _substitutionDefs: ReadonlyMap private readonly _substitutionDefsBackup: ReadonlyMap constructor( substitutionDefs: ReadonlyArray, ) { this._substitutionDefs = new Map(substitutionDefs.map((def) => [def.needle, def])) this._substitutionDefsBackup = new Map(substitutionDefs.map((def) => [def.needle.toLowerCase(), def])) } resolveSubstitution(generatorState: RstGeneratorState, substitutionRef: RstSubstitutionRef): RstSubstitutionDef { const def = this._substitutionDefs.get(substitutionRef.textContent) ?? this._substitutionDefsBackup.get(substitutionRef.textContent) if (!def) { throw new RstGeneratorError(generatorState, substitutionRef, 'Failed to resolveSubstitution') } return def } }