ezmedicationinput 0.1.44 → 0.1.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/README.md +265 -2
  2. package/dist/body-site-grammar.d.ts +39 -0
  3. package/dist/body-site-lookup.d.ts +84 -0
  4. package/dist/body-site-resolution.d.ts +9 -0
  5. package/dist/body-site-spatial.d.ts +6 -0
  6. package/dist/clause-tail-grammar.d.ts +18 -0
  7. package/dist/fhir-translations.d.ts +4 -1
  8. package/dist/fhir.d.ts +9 -1
  9. package/dist/hpsg/chart.d.ts +14 -0
  10. package/dist/hpsg/clause-parser.d.ts +3 -0
  11. package/dist/hpsg/defaults.d.ts +9 -0
  12. package/dist/hpsg/lexical-classes.d.ts +89 -0
  13. package/dist/hpsg/method-lexicon.d.ts +13 -0
  14. package/dist/hpsg/projection.d.ts +12 -0
  15. package/dist/hpsg/rule-context.d.ts +32 -0
  16. package/dist/hpsg/rules/core-rules.d.ts +8 -0
  17. package/dist/hpsg/rules/instruction-rules.d.ts +4 -0
  18. package/dist/hpsg/rules/prn-rules.d.ts +3 -0
  19. package/dist/hpsg/rules/product-route.d.ts +2 -0
  20. package/dist/hpsg/rules/site-rules.d.ts +4 -0
  21. package/dist/hpsg/rules/timing-rules.d.ts +12 -0
  22. package/dist/hpsg/segmenter.d.ts +6 -0
  23. package/dist/hpsg/signature.d.ts +116 -0
  24. package/dist/hpsg/timing-lexicon.d.ts +31 -0
  25. package/dist/hpsg/unification.d.ts +8 -0
  26. package/dist/index.cjs +21052 -0
  27. package/dist/index.d.ts +5 -0
  28. package/dist/index.js +20865 -680
  29. package/dist/maps.d.ts +4 -2
  30. package/dist/parser-state.d.ts +4 -1
  31. package/dist/parser.d.ts +3 -13
  32. package/dist/prn-reason-coding.d.ts +8 -0
  33. package/dist/site-coding.d.ts +5 -0
  34. package/dist/snomed-postcoordination.d.ts +25 -0
  35. package/dist/snomed.d.ts +13 -0
  36. package/dist/types.d.ts +61 -16
  37. package/dist/unit-lexicon.d.ts +4 -0
  38. package/package.json +17 -6
  39. package/dist/advice-rules.json +0 -772
  40. package/dist/advice-terminology.json +0 -104
  41. package/dist/advice.js +0 -1375
  42. package/dist/context.js +0 -50
  43. package/dist/event-trigger.d.ts +0 -14
  44. package/dist/event-trigger.js +0 -501
  45. package/dist/fhir-translations.js +0 -117
  46. package/dist/fhir.js +0 -693
  47. package/dist/format.js +0 -1034
  48. package/dist/i18n.js +0 -1341
  49. package/dist/internal-types.d.ts +0 -60
  50. package/dist/internal-types.js +0 -2
  51. package/dist/ir.js +0 -178
  52. package/dist/lexer/lex.js +0 -401
  53. package/dist/lexer/meaning.js +0 -619
  54. package/dist/lexer/surface.js +0 -62
  55. package/dist/lexer/token-types.js +0 -19
  56. package/dist/maps.js +0 -2226
  57. package/dist/package.json +0 -3
  58. package/dist/parser-state.js +0 -441
  59. package/dist/parser.js +0 -5631
  60. package/dist/prn.js +0 -59
  61. package/dist/safety.js +0 -15
  62. package/dist/schedule.js +0 -1636
  63. package/dist/segment.d.ts +0 -6
  64. package/dist/segment.js +0 -203
  65. package/dist/site-phrases.js +0 -344
  66. package/dist/suggest.js +0 -907
  67. package/dist/timing-summary.js +0 -138
  68. package/dist/types.js +0 -276
  69. package/dist/utils/array.js +0 -11
  70. package/dist/utils/enum.d.ts +0 -2
  71. package/dist/utils/enum.js +0 -7
  72. package/dist/utils/object.js +0 -34
  73. package/dist/utils/strength.js +0 -149
  74. package/dist/utils/text.js +0 -48
  75. package/dist/utils/units.js +0 -82
package/dist/fhir.js DELETED
@@ -1,693 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.canonicalToFhir = canonicalToFhir;
4
- exports.toFhir = toFhir;
5
- exports.canonicalFromFhir = canonicalFromFhir;
6
- exports.parserStateFromFhir = parserStateFromFhir;
7
- const advice_1 = require("./advice");
8
- const fhir_translations_1 = require("./fhir-translations");
9
- const format_1 = require("./format");
10
- const parser_state_1 = require("./parser-state");
11
- const prn_1 = require("./prn");
12
- const maps_1 = require("./maps");
13
- const types_1 = require("./types");
14
- const object_1 = require("./utils/object");
15
- const array_1 = require("./utils/array");
16
- const SNOMED_SYSTEM = "http://snomed.info/sct";
17
- const UCUM_SYSTEM = "http://unitsofmeasure.org";
18
- function createEmptyCanonicalClause(rawText) {
19
- return {
20
- kind: "administration",
21
- rawText,
22
- raw: {
23
- start: 0,
24
- end: rawText.length,
25
- text: rawText
26
- },
27
- leftovers: [],
28
- evidence: [],
29
- confidence: 1
30
- };
31
- }
32
- function selectFirstCodingWithCode(concept) {
33
- var _a;
34
- if (!((_a = concept === null || concept === void 0 ? void 0 : concept.coding) === null || _a === void 0 ? void 0 : _a.length)) {
35
- return undefined;
36
- }
37
- for (const coding of concept.coding) {
38
- if (coding.code) {
39
- return coding;
40
- }
41
- }
42
- return undefined;
43
- }
44
- function selectPreferredSiteCoding(site) {
45
- var _a;
46
- if (!((_a = site === null || site === void 0 ? void 0 : site.coding) === null || _a === void 0 ? void 0 : _a.length)) {
47
- return undefined;
48
- }
49
- for (const coding of site.coding) {
50
- if (coding.system === SNOMED_SYSTEM && coding.code) {
51
- return coding;
52
- }
53
- }
54
- return selectFirstCodingWithCode(site);
55
- }
56
- function buildFhirDoseRange(range, unit) {
57
- const fhirRange = {};
58
- if (range.low !== undefined) {
59
- fhirRange.low = {
60
- value: range.low,
61
- unit
62
- };
63
- }
64
- if (range.high !== undefined) {
65
- fhirRange.high = {
66
- value: range.high,
67
- unit
68
- };
69
- }
70
- if (!fhirRange.low && !fhirRange.high) {
71
- return undefined;
72
- }
73
- return fhirRange;
74
- }
75
- function describeDurationUnit(unit, value) {
76
- const plural = value !== 1;
77
- switch (unit) {
78
- case types_1.FhirPeriodUnit.Second:
79
- return plural ? "seconds" : "second";
80
- case types_1.FhirPeriodUnit.Minute:
81
- return plural ? "minutes" : "minute";
82
- case types_1.FhirPeriodUnit.Hour:
83
- return plural ? "hours" : "hour";
84
- case types_1.FhirPeriodUnit.Day:
85
- return plural ? "days" : "day";
86
- case types_1.FhirPeriodUnit.Week:
87
- return plural ? "weeks" : "week";
88
- case types_1.FhirPeriodUnit.Month:
89
- return plural ? "months" : "month";
90
- case types_1.FhirPeriodUnit.Year:
91
- return plural ? "years" : "year";
92
- default:
93
- return unit;
94
- }
95
- }
96
- function buildFhirDurationQuantity(value, unit) {
97
- return {
98
- value,
99
- unit: describeDurationUnit(unit, value),
100
- system: UCUM_SYSTEM,
101
- code: unit
102
- };
103
- }
104
- function buildFhirBoundsRange(low, high, unit) {
105
- return {
106
- low: buildFhirDurationQuantity(low, unit),
107
- high: buildFhirDurationQuantity(high, unit)
108
- };
109
- }
110
- function parseFhirDurationUnit(quantity) {
111
- var _a, _b, _c;
112
- const candidate = (_b = (_a = quantity === null || quantity === void 0 ? void 0 : quantity.code) === null || _a === void 0 ? void 0 : _a.trim().toLowerCase()) !== null && _b !== void 0 ? _b : (_c = quantity === null || quantity === void 0 ? void 0 : quantity.unit) === null || _c === void 0 ? void 0 : _c.trim().toLowerCase();
113
- switch (candidate) {
114
- case "s":
115
- case "sec":
116
- case "second":
117
- case "seconds":
118
- return types_1.FhirPeriodUnit.Second;
119
- case "min":
120
- case "mins":
121
- case "minute":
122
- case "minutes":
123
- return types_1.FhirPeriodUnit.Minute;
124
- case "h":
125
- case "hr":
126
- case "hrs":
127
- case "hour":
128
- case "hours":
129
- return types_1.FhirPeriodUnit.Hour;
130
- case "d":
131
- case "day":
132
- case "days":
133
- return types_1.FhirPeriodUnit.Day;
134
- case "wk":
135
- case "wks":
136
- case "week":
137
- case "weeks":
138
- return types_1.FhirPeriodUnit.Week;
139
- case "mo":
140
- case "month":
141
- case "months":
142
- return types_1.FhirPeriodUnit.Month;
143
- case "a":
144
- case "yr":
145
- case "yrs":
146
- case "year":
147
- case "years":
148
- return types_1.FhirPeriodUnit.Year;
149
- default:
150
- return undefined;
151
- }
152
- }
153
- function extractCanonicalTimingBounds(repeat) {
154
- var _a;
155
- if (!repeat) {
156
- return {};
157
- }
158
- if (((_a = repeat.boundsDuration) === null || _a === void 0 ? void 0 : _a.value) !== undefined) {
159
- const durationUnit = parseFhirDurationUnit(repeat.boundsDuration);
160
- if (!durationUnit) {
161
- return {};
162
- }
163
- return {
164
- duration: repeat.boundsDuration.value,
165
- durationUnit
166
- };
167
- }
168
- if (!repeat.boundsRange) {
169
- return {};
170
- }
171
- const low = repeat.boundsRange.low;
172
- const high = repeat.boundsRange.high;
173
- const lowUnit = parseFhirDurationUnit(low);
174
- const highUnit = parseFhirDurationUnit(high);
175
- const durationUnit = lowUnit !== null && lowUnit !== void 0 ? lowUnit : highUnit;
176
- if (!durationUnit) {
177
- return {};
178
- }
179
- let warning;
180
- if (lowUnit && highUnit && lowUnit !== highUnit) {
181
- warning = `FHIR timing boundsRange low/high units differ (${lowUnit} vs ${highUnit}); preserved numeric bounds using ${durationUnit}.`;
182
- }
183
- return {
184
- duration: low === null || low === void 0 ? void 0 : low.value,
185
- durationMax: high === null || high === void 0 ? void 0 : high.value,
186
- durationUnit,
187
- warning
188
- };
189
- }
190
- function extractCanonicalDoseRange(range) {
191
- var _a, _b, _c, _d;
192
- const canonicalRange = {};
193
- const lowUnit = (_a = range.low) === null || _a === void 0 ? void 0 : _a.unit;
194
- const highUnit = (_b = range.high) === null || _b === void 0 ? void 0 : _b.unit;
195
- if (((_c = range.low) === null || _c === void 0 ? void 0 : _c.value) !== undefined) {
196
- canonicalRange.low = range.low.value;
197
- }
198
- if (((_d = range.high) === null || _d === void 0 ? void 0 : _d.value) !== undefined) {
199
- canonicalRange.high = range.high.value;
200
- }
201
- if (canonicalRange.low === undefined && canonicalRange.high === undefined) {
202
- return {};
203
- }
204
- const unit = lowUnit !== null && lowUnit !== void 0 ? lowUnit : highUnit;
205
- let warning;
206
- if (lowUnit && highUnit && lowUnit !== highUnit) {
207
- warning = `FHIR doseRange low/high units differ (${lowUnit} vs ${highUnit}); preserved numeric bounds using ${unit}.`;
208
- }
209
- return {
210
- range: canonicalRange,
211
- unit,
212
- warning
213
- };
214
- }
215
- function appendWarning(warnings, warning) {
216
- if (!warning) {
217
- return warnings;
218
- }
219
- if (!warnings) {
220
- return [warning];
221
- }
222
- warnings.push(warning);
223
- return warnings;
224
- }
225
- function canonicalToFhir(clause, textOverride) {
226
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9;
227
- const dosage = {};
228
- const repeat = {};
229
- let hasRepeat = false;
230
- const schedule = clause.schedule;
231
- if ((schedule === null || schedule === void 0 ? void 0 : schedule.frequency) !== undefined) {
232
- repeat.frequency = schedule.frequency;
233
- hasRepeat = true;
234
- }
235
- if ((schedule === null || schedule === void 0 ? void 0 : schedule.count) !== undefined) {
236
- repeat.count = schedule.count;
237
- hasRepeat = true;
238
- }
239
- if ((schedule === null || schedule === void 0 ? void 0 : schedule.duration) !== undefined && schedule.durationUnit) {
240
- if (schedule.durationMax !== undefined && schedule.durationMax !== schedule.duration) {
241
- repeat.boundsRange = buildFhirBoundsRange(schedule.duration, schedule.durationMax, schedule.durationUnit);
242
- }
243
- else {
244
- repeat.boundsDuration = buildFhirDurationQuantity(schedule.duration, schedule.durationUnit);
245
- }
246
- hasRepeat = true;
247
- }
248
- if ((schedule === null || schedule === void 0 ? void 0 : schedule.frequencyMax) !== undefined) {
249
- repeat.frequencyMax = schedule.frequencyMax;
250
- hasRepeat = true;
251
- }
252
- if ((schedule === null || schedule === void 0 ? void 0 : schedule.period) !== undefined && schedule.periodUnit) {
253
- repeat.period = schedule.period;
254
- repeat.periodUnit = schedule.periodUnit;
255
- hasRepeat = true;
256
- }
257
- if ((schedule === null || schedule === void 0 ? void 0 : schedule.periodMax) !== undefined) {
258
- repeat.periodMax = schedule.periodMax;
259
- hasRepeat = true;
260
- }
261
- if ((_a = schedule === null || schedule === void 0 ? void 0 : schedule.dayOfWeek) === null || _a === void 0 ? void 0 : _a.length) {
262
- repeat.dayOfWeek = [...schedule.dayOfWeek];
263
- hasRepeat = true;
264
- }
265
- if ((_b = schedule === null || schedule === void 0 ? void 0 : schedule.when) === null || _b === void 0 ? void 0 : _b.length) {
266
- repeat.when = [...schedule.when];
267
- hasRepeat = true;
268
- }
269
- if ((_c = schedule === null || schedule === void 0 ? void 0 : schedule.timeOfDay) === null || _c === void 0 ? void 0 : _c.length) {
270
- repeat.timeOfDay = [...schedule.timeOfDay];
271
- hasRepeat = true;
272
- }
273
- if (hasRepeat) {
274
- dosage.timing = { repeat };
275
- }
276
- if (schedule === null || schedule === void 0 ? void 0 : schedule.timingCode) {
277
- dosage.timing = (_d = dosage.timing) !== null && _d !== void 0 ? _d : {};
278
- dosage.timing.code = {
279
- coding: [{ code: schedule.timingCode }],
280
- text: schedule.timingCode
281
- };
282
- }
283
- if ((_e = clause.dose) === null || _e === void 0 ? void 0 : _e.range) {
284
- const doseRange = buildFhirDoseRange(clause.dose.range, clause.dose.unit);
285
- if (doseRange) {
286
- dosage.doseAndRate = [
287
- {
288
- doseRange
289
- }
290
- ];
291
- }
292
- }
293
- else if (((_f = clause.dose) === null || _f === void 0 ? void 0 : _f.value) !== undefined) {
294
- dosage.doseAndRate = [
295
- {
296
- doseQuantity: {
297
- value: clause.dose.value,
298
- unit: clause.dose.unit
299
- }
300
- }
301
- ];
302
- }
303
- if (((_g = clause.route) === null || _g === void 0 ? void 0 : _g.code) || ((_h = clause.route) === null || _h === void 0 ? void 0 : _h.text)) {
304
- const routeCode = (_j = clause.route) === null || _j === void 0 ? void 0 : _j.code;
305
- const coding = routeCode ? maps_1.ROUTE_SNOMED[routeCode] : undefined;
306
- const text = (_l = (_k = clause.route) === null || _k === void 0 ? void 0 : _k.text) !== null && _l !== void 0 ? _l : (routeCode ? maps_1.ROUTE_TEXT[routeCode] : undefined);
307
- if (coding) {
308
- dosage.route = {
309
- text,
310
- coding: [
311
- {
312
- system: SNOMED_SYSTEM,
313
- code: coding.code,
314
- display: coding.display
315
- }
316
- ]
317
- };
318
- }
319
- else if (text) {
320
- dosage.route = { text };
321
- }
322
- }
323
- if (((_m = clause.site) === null || _m === void 0 ? void 0 : _m.text) || ((_p = (_o = clause.site) === null || _o === void 0 ? void 0 : _o.coding) === null || _p === void 0 ? void 0 : _p.code)) {
324
- const coding = ((_r = (_q = clause.site) === null || _q === void 0 ? void 0 : _q.coding) === null || _r === void 0 ? void 0 : _r.code)
325
- ? [
326
- {
327
- system: (_s = clause.site.coding.system) !== null && _s !== void 0 ? _s : SNOMED_SYSTEM,
328
- code: clause.site.coding.code,
329
- display: clause.site.coding.display
330
- }
331
- ]
332
- : undefined;
333
- dosage.site = {
334
- text: (_t = clause.site) === null || _t === void 0 ? void 0 : _t.text,
335
- coding
336
- };
337
- }
338
- if (((_u = clause.method) === null || _u === void 0 ? void 0 : _u.text) || ((_v = clause.method) === null || _v === void 0 ? void 0 : _v._text) || ((_x = (_w = clause.method) === null || _w === void 0 ? void 0 : _w.coding) === null || _x === void 0 ? void 0 : _x.code)) {
339
- dosage.method = {
340
- text: (_y = clause.method) === null || _y === void 0 ? void 0 : _y.text,
341
- _text: (0, fhir_translations_1.clonePrimitiveElement)((_z = clause.method) === null || _z === void 0 ? void 0 : _z._text),
342
- coding: ((_1 = (_0 = clause.method) === null || _0 === void 0 ? void 0 : _0.coding) === null || _1 === void 0 ? void 0 : _1.code)
343
- ? [
344
- {
345
- system: (_2 = clause.method.coding.system) !== null && _2 !== void 0 ? _2 : SNOMED_SYSTEM,
346
- code: clause.method.coding.code,
347
- display: clause.method.coding.display,
348
- _display: (0, fhir_translations_1.clonePrimitiveElement)(clause.method.coding._display)
349
- }
350
- ]
351
- : undefined
352
- };
353
- }
354
- if ((_3 = clause.additionalInstructions) === null || _3 === void 0 ? void 0 : _3.length) {
355
- dosage.additionalInstruction = [];
356
- for (const instruction of clause.additionalInstructions) {
357
- dosage.additionalInstruction.push({
358
- text: instruction.text,
359
- coding: ((_4 = instruction.coding) === null || _4 === void 0 ? void 0 : _4.code)
360
- ? [
361
- {
362
- system: (_5 = instruction.coding.system) !== null && _5 !== void 0 ? _5 : SNOMED_SYSTEM,
363
- code: instruction.coding.code,
364
- display: instruction.coding.display
365
- }
366
- ]
367
- : undefined
368
- });
369
- }
370
- }
371
- if ((_6 = clause.prn) === null || _6 === void 0 ? void 0 : _6.enabled) {
372
- dosage.asNeededBoolean = true;
373
- const reasons = ((_7 = clause.prn.reasons) === null || _7 === void 0 ? void 0 : _7.length) ? clause.prn.reasons : clause.prn.reason ? [clause.prn.reason] : [];
374
- if (reasons.length) {
375
- dosage.asNeededFor = [];
376
- for (const reason of reasons) {
377
- const concept = {};
378
- if (reason.text) {
379
- concept.text = reason.text;
380
- }
381
- if ((_8 = reason.coding) === null || _8 === void 0 ? void 0 : _8.code) {
382
- concept.coding = [
383
- {
384
- system: (_9 = reason.coding.system) !== null && _9 !== void 0 ? _9 : SNOMED_SYSTEM,
385
- code: reason.coding.code,
386
- display: reason.coding.display
387
- }
388
- ];
389
- }
390
- dosage.asNeededFor.push(concept);
391
- }
392
- }
393
- }
394
- const longText = textOverride !== null && textOverride !== void 0 ? textOverride : (0, format_1.formatCanonicalClause)(clause, "long");
395
- if (longText) {
396
- dosage.text = longText;
397
- }
398
- if (clause.patientInstruction) {
399
- dosage.patientInstruction = clause.patientInstruction;
400
- }
401
- return dosage;
402
- }
403
- function toFhir(state) {
404
- var _a;
405
- const clauses = state.clauses;
406
- const clause = (_a = clauses[0]) !== null && _a !== void 0 ? _a : createEmptyCanonicalClause(state.input);
407
- return canonicalToFhir(clause);
408
- }
409
- function canonicalFromFhir(dosage) {
410
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9;
411
- const rawText = (_a = dosage.text) !== null && _a !== void 0 ? _a : "";
412
- const clause = createEmptyCanonicalClause(rawText);
413
- let routeCode;
414
- const routeCoding = (_c = (_b = dosage.route) === null || _b === void 0 ? void 0 : _b.coding) === null || _c === void 0 ? void 0 : _c.find((code) => code.system === SNOMED_SYSTEM);
415
- if (routeCoding === null || routeCoding === void 0 ? void 0 : routeCoding.code) {
416
- routeCode = maps_1.ROUTE_BY_SNOMED[routeCoding.code];
417
- }
418
- if (routeCode || ((_d = dosage.route) === null || _d === void 0 ? void 0 : _d.text)) {
419
- clause.route = {
420
- code: routeCode,
421
- text: (_f = (_e = dosage.route) === null || _e === void 0 ? void 0 : _e.text) !== null && _f !== void 0 ? _f : (routeCode ? maps_1.ROUTE_TEXT[routeCode] : undefined)
422
- };
423
- }
424
- const siteCoding = selectPreferredSiteCoding(dosage.site);
425
- if (((_g = dosage.site) === null || _g === void 0 ? void 0 : _g.text) || (siteCoding === null || siteCoding === void 0 ? void 0 : siteCoding.code)) {
426
- clause.site = {
427
- text: (_h = dosage.site) === null || _h === void 0 ? void 0 : _h.text,
428
- coding: (siteCoding === null || siteCoding === void 0 ? void 0 : siteCoding.code)
429
- ? {
430
- code: siteCoding.code,
431
- display: siteCoding.display,
432
- system: siteCoding.system
433
- }
434
- : undefined,
435
- source: "text"
436
- };
437
- }
438
- const methodCoding = selectFirstCodingWithCode(dosage.method);
439
- if (((_j = dosage.method) === null || _j === void 0 ? void 0 : _j.text) || ((_k = dosage.method) === null || _k === void 0 ? void 0 : _k._text) || (methodCoding === null || methodCoding === void 0 ? void 0 : methodCoding.code)) {
440
- clause.method = {
441
- text: (_l = dosage.method) === null || _l === void 0 ? void 0 : _l.text,
442
- _text: (0, fhir_translations_1.clonePrimitiveElement)((_m = dosage.method) === null || _m === void 0 ? void 0 : _m._text),
443
- coding: (methodCoding === null || methodCoding === void 0 ? void 0 : methodCoding.code)
444
- ? {
445
- code: methodCoding.code,
446
- display: methodCoding.display,
447
- system: methodCoding.system,
448
- _display: (0, fhir_translations_1.clonePrimitiveElement)(methodCoding._display)
449
- }
450
- : undefined
451
- };
452
- }
453
- const repeat = (_o = dosage.timing) === null || _o === void 0 ? void 0 : _o.repeat;
454
- const timingBounds = extractCanonicalTimingBounds(repeat);
455
- if (((_s = (_r = (_q = (_p = dosage.timing) === null || _p === void 0 ? void 0 : _p.code) === null || _q === void 0 ? void 0 : _q.coding) === null || _r === void 0 ? void 0 : _r[0]) === null || _s === void 0 ? void 0 : _s.code) ||
456
- (repeat === null || repeat === void 0 ? void 0 : repeat.count) !== undefined ||
457
- (repeat === null || repeat === void 0 ? void 0 : repeat.boundsDuration) ||
458
- (repeat === null || repeat === void 0 ? void 0 : repeat.boundsRange) ||
459
- (repeat === null || repeat === void 0 ? void 0 : repeat.frequency) !== undefined ||
460
- (repeat === null || repeat === void 0 ? void 0 : repeat.frequencyMax) !== undefined ||
461
- (repeat === null || repeat === void 0 ? void 0 : repeat.period) !== undefined ||
462
- (repeat === null || repeat === void 0 ? void 0 : repeat.periodMax) !== undefined ||
463
- (repeat === null || repeat === void 0 ? void 0 : repeat.periodUnit) ||
464
- ((_t = repeat === null || repeat === void 0 ? void 0 : repeat.dayOfWeek) === null || _t === void 0 ? void 0 : _t.length) ||
465
- ((_u = repeat === null || repeat === void 0 ? void 0 : repeat.when) === null || _u === void 0 ? void 0 : _u.length) ||
466
- ((_v = repeat === null || repeat === void 0 ? void 0 : repeat.timeOfDay) === null || _v === void 0 ? void 0 : _v.length)) {
467
- clause.schedule = {
468
- timingCode: (_z = (_y = (_x = (_w = dosage.timing) === null || _w === void 0 ? void 0 : _w.code) === null || _x === void 0 ? void 0 : _x.coding) === null || _y === void 0 ? void 0 : _y[0]) === null || _z === void 0 ? void 0 : _z.code,
469
- count: repeat === null || repeat === void 0 ? void 0 : repeat.count,
470
- duration: timingBounds.duration,
471
- durationMax: timingBounds.durationMax,
472
- durationUnit: timingBounds.durationUnit,
473
- frequency: repeat === null || repeat === void 0 ? void 0 : repeat.frequency,
474
- frequencyMax: repeat === null || repeat === void 0 ? void 0 : repeat.frequencyMax,
475
- period: repeat === null || repeat === void 0 ? void 0 : repeat.period,
476
- periodMax: repeat === null || repeat === void 0 ? void 0 : repeat.periodMax,
477
- periodUnit: repeat === null || repeat === void 0 ? void 0 : repeat.periodUnit,
478
- dayOfWeek: (repeat === null || repeat === void 0 ? void 0 : repeat.dayOfWeek) ? [...repeat.dayOfWeek] : undefined,
479
- when: (repeat === null || repeat === void 0 ? void 0 : repeat.when) ? [...repeat.when] : undefined,
480
- timeOfDay: (repeat === null || repeat === void 0 ? void 0 : repeat.timeOfDay) ? [...repeat.timeOfDay] : undefined
481
- };
482
- clause.warnings = appendWarning(clause.warnings, timingBounds.warning);
483
- }
484
- const doseAndRate = (_0 = dosage.doseAndRate) === null || _0 === void 0 ? void 0 : _0[0];
485
- if (doseAndRate === null || doseAndRate === void 0 ? void 0 : doseAndRate.doseRange) {
486
- const extracted = extractCanonicalDoseRange(doseAndRate.doseRange);
487
- if (extracted.range) {
488
- clause.dose = {
489
- range: extracted.range,
490
- unit: extracted.unit
491
- };
492
- clause.warnings = appendWarning(clause.warnings, extracted.warning);
493
- }
494
- }
495
- else if (((_1 = doseAndRate === null || doseAndRate === void 0 ? void 0 : doseAndRate.doseQuantity) === null || _1 === void 0 ? void 0 : _1.value) !== undefined) {
496
- clause.dose = {
497
- value: doseAndRate.doseQuantity.value,
498
- unit: doseAndRate.doseQuantity.unit
499
- };
500
- }
501
- const prnReasons = ((_2 = dosage.asNeededFor) === null || _2 === void 0 ? void 0 : _2.length)
502
- ? dosage.asNeededFor.map((concept) => {
503
- var _a;
504
- const coding = (_a = concept.coding) === null || _a === void 0 ? void 0 : _a.find((code) => Boolean(code.code));
505
- return {
506
- text: concept.text,
507
- coding: (coding === null || coding === void 0 ? void 0 : coding.code)
508
- ? {
509
- code: coding.code,
510
- display: coding.display,
511
- system: coding.system
512
- }
513
- : undefined
514
- };
515
- })
516
- : undefined;
517
- const primaryReason = prnReasons === null || prnReasons === void 0 ? void 0 : prnReasons[0];
518
- if (dosage.asNeededBoolean || (primaryReason === null || primaryReason === void 0 ? void 0 : primaryReason.text) || ((_3 = primaryReason === null || primaryReason === void 0 ? void 0 : primaryReason.coding) === null || _3 === void 0 ? void 0 : _3.code)) {
519
- clause.prn = {
520
- enabled: Boolean(dosage.asNeededBoolean || (primaryReason === null || primaryReason === void 0 ? void 0 : primaryReason.text) || ((_4 = primaryReason === null || primaryReason === void 0 ? void 0 : primaryReason.coding) === null || _4 === void 0 ? void 0 : _4.code)),
521
- reason: (prnReasons === null || prnReasons === void 0 ? void 0 : prnReasons.length) === 1
522
- ? primaryReason
523
- : (prnReasons === null || prnReasons === void 0 ? void 0 : prnReasons.length)
524
- ? { text: (0, prn_1.joinCanonicalPrnReasonTexts)(prnReasons) }
525
- : undefined,
526
- reasons: (prnReasons === null || prnReasons === void 0 ? void 0 : prnReasons.length) ? prnReasons : undefined
527
- };
528
- }
529
- if ((_5 = dosage.additionalInstruction) === null || _5 === void 0 ? void 0 : _5.length) {
530
- clause.additionalInstructions = [];
531
- for (const instruction of dosage.additionalInstruction) {
532
- const coding = (_6 = instruction.coding) === null || _6 === void 0 ? void 0 : _6.find((code) => Boolean(code.code));
533
- clause.additionalInstructions.push({
534
- text: instruction.text,
535
- coding: (coding === null || coding === void 0 ? void 0 : coding.code)
536
- ? {
537
- code: coding.code,
538
- display: coding.display,
539
- system: coding.system
540
- }
541
- : undefined,
542
- frames: (coding === null || coding === void 0 ? void 0 : coding.code)
543
- ? (0, advice_1.buildAdditionalInstructionFramesFromCoding)((_7 = coding.system) !== null && _7 !== void 0 ? _7 : SNOMED_SYSTEM, coding.code, (_9 = (_8 = instruction.text) !== null && _8 !== void 0 ? _8 : coding.display) !== null && _9 !== void 0 ? _9 : "", clause.raw)
544
- : undefined
545
- });
546
- }
547
- }
548
- if (dosage.patientInstruction) {
549
- clause.patientInstruction = dosage.patientInstruction;
550
- }
551
- return clause;
552
- }
553
- function parserStateFromFhir(dosage) {
554
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12;
555
- const state = new parser_state_1.ParserState((_a = dosage.text) !== null && _a !== void 0 ? _a : "", []);
556
- const timingBounds = extractCanonicalTimingBounds((_b = dosage.timing) === null || _b === void 0 ? void 0 : _b.repeat);
557
- state.timeOfDay = ((_d = (_c = dosage.timing) === null || _c === void 0 ? void 0 : _c.repeat) === null || _d === void 0 ? void 0 : _d.timeOfDay)
558
- ? [...dosage.timing.repeat.timeOfDay]
559
- : [];
560
- state.timingCode = (_h = (_g = (_f = (_e = dosage.timing) === null || _e === void 0 ? void 0 : _e.code) === null || _f === void 0 ? void 0 : _f.coding) === null || _g === void 0 ? void 0 : _g[0]) === null || _h === void 0 ? void 0 : _h.code;
561
- state.count = (_k = (_j = dosage.timing) === null || _j === void 0 ? void 0 : _j.repeat) === null || _k === void 0 ? void 0 : _k.count;
562
- state.duration = timingBounds.duration;
563
- state.durationMax = timingBounds.durationMax;
564
- state.durationUnit = timingBounds.durationUnit;
565
- state.frequency = (_m = (_l = dosage.timing) === null || _l === void 0 ? void 0 : _l.repeat) === null || _m === void 0 ? void 0 : _m.frequency;
566
- state.frequencyMax = (_p = (_o = dosage.timing) === null || _o === void 0 ? void 0 : _o.repeat) === null || _p === void 0 ? void 0 : _p.frequencyMax;
567
- state.period = (_r = (_q = dosage.timing) === null || _q === void 0 ? void 0 : _q.repeat) === null || _r === void 0 ? void 0 : _r.period;
568
- state.periodMax = (_t = (_s = dosage.timing) === null || _s === void 0 ? void 0 : _s.repeat) === null || _t === void 0 ? void 0 : _t.periodMax;
569
- state.periodUnit = (_v = (_u = dosage.timing) === null || _u === void 0 ? void 0 : _u.repeat) === null || _v === void 0 ? void 0 : _v.periodUnit;
570
- state.routeText = (_w = dosage.route) === null || _w === void 0 ? void 0 : _w.text;
571
- state.siteText = (_x = dosage.site) === null || _x === void 0 ? void 0 : _x.text;
572
- state.methodText = (_y = dosage.method) === null || _y === void 0 ? void 0 : _y.text;
573
- state.methodTextElement = (0, fhir_translations_1.clonePrimitiveElement)((_z = dosage.method) === null || _z === void 0 ? void 0 : _z._text);
574
- state.patientInstruction = dosage.patientInstruction;
575
- state.asNeeded = dosage.asNeededBoolean;
576
- if ((_0 = dosage.asNeededFor) === null || _0 === void 0 ? void 0 : _0.length) {
577
- const prnReasons = dosage.asNeededFor.map((concept) => {
578
- const coding = selectFirstCodingWithCode(concept);
579
- return {
580
- text: concept.text,
581
- coding: (coding === null || coding === void 0 ? void 0 : coding.code)
582
- ? {
583
- code: coding.code,
584
- display: coding.display,
585
- system: coding.system,
586
- _display: (0, fhir_translations_1.clonePrimitiveElement)(coding._display)
587
- }
588
- : undefined
589
- };
590
- });
591
- state.asNeededReasons = prnReasons;
592
- state.asNeededReason = (0, prn_1.joinCanonicalPrnReasonTexts)(prnReasons);
593
- }
594
- if ((_2 = (_1 = dosage.timing) === null || _1 === void 0 ? void 0 : _1.repeat) === null || _2 === void 0 ? void 0 : _2.dayOfWeek) {
595
- state.dayOfWeek.push(...dosage.timing.repeat.dayOfWeek);
596
- }
597
- if ((_4 = (_3 = dosage.timing) === null || _3 === void 0 ? void 0 : _3.repeat) === null || _4 === void 0 ? void 0 : _4.when) {
598
- const whenValues = dosage.timing.repeat.when.filter((value) => (0, array_1.arrayIncludes)((0, object_1.objectValues)(types_1.EventTiming), value));
599
- state.when.push(...whenValues);
600
- }
601
- const routeCoding = (_6 = (_5 = dosage.route) === null || _5 === void 0 ? void 0 : _5.coding) === null || _6 === void 0 ? void 0 : _6.find((code) => code.system === SNOMED_SYSTEM);
602
- if (routeCoding === null || routeCoding === void 0 ? void 0 : routeCoding.code) {
603
- // Translate SNOMED codings back into the simplified enum for round-trip fidelity.
604
- const mapped = maps_1.ROUTE_BY_SNOMED[routeCoding.code];
605
- if (mapped) {
606
- state.routeCode = mapped;
607
- state.routeText = maps_1.ROUTE_TEXT[mapped];
608
- }
609
- }
610
- const siteCoding = selectPreferredSiteCoding(dosage.site);
611
- if (siteCoding === null || siteCoding === void 0 ? void 0 : siteCoding.code) {
612
- state.siteCoding = {
613
- code: siteCoding.code,
614
- display: siteCoding.display,
615
- system: siteCoding.system
616
- };
617
- state.siteSource = "text";
618
- }
619
- else if ((_7 = dosage.site) === null || _7 === void 0 ? void 0 : _7.text) {
620
- state.siteSource = "text";
621
- }
622
- const methodCoding = selectFirstCodingWithCode(dosage.method);
623
- if (methodCoding === null || methodCoding === void 0 ? void 0 : methodCoding.code) {
624
- state.methodCoding = {
625
- code: methodCoding.code,
626
- display: methodCoding.display,
627
- system: methodCoding.system,
628
- _display: (0, fhir_translations_1.clonePrimitiveElement)(methodCoding._display)
629
- };
630
- }
631
- if (((_8 = dosage.asNeededFor) === null || _8 === void 0 ? void 0 : _8.length) === 1) {
632
- const reasonCoding = selectFirstCodingWithCode(dosage.asNeededFor[0]);
633
- if (reasonCoding === null || reasonCoding === void 0 ? void 0 : reasonCoding.code) {
634
- const defaultDef = (0, maps_1.findPrnReasonDefinitionByCoding)((_9 = reasonCoding.system) !== null && _9 !== void 0 ? _9 : SNOMED_SYSTEM, reasonCoding.code);
635
- state.asNeededReasonCoding = {
636
- code: reasonCoding.code,
637
- display: reasonCoding.display,
638
- system: reasonCoding.system,
639
- i18n: defaultDef === null || defaultDef === void 0 ? void 0 : defaultDef.i18n
640
- };
641
- }
642
- }
643
- if ((_10 = dosage.additionalInstruction) === null || _10 === void 0 ? void 0 : _10.length) {
644
- state.additionalInstructions = dosage.additionalInstruction.map((concept) => {
645
- var _a, _b, _c, _d, _e, _f;
646
- const coding = selectFirstCodingWithCode(concept);
647
- const defaultDef = (coding === null || coding === void 0 ? void 0 : coding.code)
648
- ? (0, advice_1.findAdditionalInstructionDefinitionByCoding)((_a = coding.system) !== null && _a !== void 0 ? _a : SNOMED_SYSTEM, coding.code)
649
- : undefined;
650
- return {
651
- text: concept.text,
652
- coding: (coding === null || coding === void 0 ? void 0 : coding.code)
653
- ? {
654
- code: coding.code,
655
- display: coding.display,
656
- system: coding.system,
657
- i18n: defaultDef === null || defaultDef === void 0 ? void 0 : defaultDef.i18n
658
- }
659
- : undefined,
660
- frames: (coding === null || coding === void 0 ? void 0 : coding.code)
661
- ? (0, advice_1.buildAdditionalInstructionFramesFromCoding)((_b = coding.system) !== null && _b !== void 0 ? _b : SNOMED_SYSTEM, coding.code, (_d = (_c = concept.text) !== null && _c !== void 0 ? _c : coding.display) !== null && _d !== void 0 ? _d : "", { start: 0, end: ((_f = (_e = concept.text) !== null && _e !== void 0 ? _e : coding.display) !== null && _f !== void 0 ? _f : "").length })
662
- : undefined
663
- };
664
- });
665
- }
666
- if (timingBounds.warning) {
667
- const nextWarnings = appendWarning(state.warnings, timingBounds.warning);
668
- if (nextWarnings) {
669
- state.warnings = nextWarnings;
670
- }
671
- }
672
- const doseAndRate = (_11 = dosage.doseAndRate) === null || _11 === void 0 ? void 0 : _11[0];
673
- if (doseAndRate === null || doseAndRate === void 0 ? void 0 : doseAndRate.doseRange) {
674
- const extracted = extractCanonicalDoseRange(doseAndRate.doseRange);
675
- if (extracted.range) {
676
- state.primaryClause.dose = {
677
- range: extracted.range,
678
- unit: extracted.unit
679
- };
680
- state.warnings = (_12 = appendWarning(state.warnings, extracted.warning)) !== null && _12 !== void 0 ? _12 : state.warnings;
681
- }
682
- }
683
- else if (doseAndRate === null || doseAndRate === void 0 ? void 0 : doseAndRate.doseQuantity) {
684
- const dose = doseAndRate.doseQuantity;
685
- if (dose.value !== undefined) {
686
- state.dose = dose.value;
687
- }
688
- if (dose.unit) {
689
- state.unit = dose.unit;
690
- }
691
- }
692
- return state;
693
- }