ezmedicationinput 0.1.43 → 0.1.44

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 (46) hide show
  1. package/README.md +4 -1
  2. package/dist/advice-rules.json +772 -0
  3. package/dist/advice-terminology.json +104 -0
  4. package/dist/advice.d.ts +16 -0
  5. package/dist/advice.js +1375 -0
  6. package/dist/event-trigger.d.ts +14 -0
  7. package/dist/event-trigger.js +501 -0
  8. package/dist/fhir-translations.d.ts +5 -0
  9. package/dist/fhir-translations.js +117 -0
  10. package/dist/fhir.d.ts +6 -4
  11. package/dist/fhir.js +566 -134
  12. package/dist/format.d.ts +4 -2
  13. package/dist/format.js +515 -218
  14. package/dist/i18n.d.ts +2 -2
  15. package/dist/i18n.js +641 -199
  16. package/dist/index.d.ts +0 -1
  17. package/dist/index.js +219 -168
  18. package/dist/internal-types.d.ts +5 -5
  19. package/dist/ir.d.ts +4 -0
  20. package/dist/ir.js +178 -0
  21. package/dist/lexer/lex.d.ts +2 -0
  22. package/dist/lexer/lex.js +401 -0
  23. package/dist/lexer/meaning.d.ts +71 -0
  24. package/dist/lexer/meaning.js +619 -0
  25. package/dist/lexer/surface.d.ts +2 -0
  26. package/dist/lexer/surface.js +62 -0
  27. package/dist/lexer/token-types.d.ts +36 -0
  28. package/dist/lexer/token-types.js +19 -0
  29. package/dist/maps.d.ts +6 -12
  30. package/dist/maps.js +793 -247
  31. package/dist/parser-state.d.ts +101 -0
  32. package/dist/parser-state.js +441 -0
  33. package/dist/parser.d.ts +7 -7
  34. package/dist/parser.js +3598 -1974
  35. package/dist/prn.d.ts +4 -0
  36. package/dist/prn.js +59 -0
  37. package/dist/schedule.js +230 -32
  38. package/dist/site-phrases.d.ts +35 -0
  39. package/dist/site-phrases.js +344 -0
  40. package/dist/timing-summary.d.ts +13 -3
  41. package/dist/timing-summary.js +7 -7
  42. package/dist/types.d.ts +237 -32
  43. package/dist/types.js +49 -1
  44. package/dist/utils/text.d.ts +3 -0
  45. package/dist/utils/text.js +48 -0
  46. package/package.json +1 -1
package/dist/fhir.js CHANGED
@@ -1,94 +1,310 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.canonicalToFhir = canonicalToFhir;
3
4
  exports.toFhir = toFhir;
4
- exports.internalFromFhir = internalFromFhir;
5
+ exports.canonicalFromFhir = canonicalFromFhir;
6
+ exports.parserStateFromFhir = parserStateFromFhir;
7
+ const advice_1 = require("./advice");
8
+ const fhir_translations_1 = require("./fhir-translations");
5
9
  const format_1 = require("./format");
10
+ const parser_state_1 = require("./parser-state");
11
+ const prn_1 = require("./prn");
6
12
  const maps_1 = require("./maps");
7
13
  const types_1 = require("./types");
8
14
  const object_1 = require("./utils/object");
9
15
  const array_1 = require("./utils/array");
10
16
  const SNOMED_SYSTEM = "http://snomed.info/sct";
11
- function toFhir(internal) {
12
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
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;
13
227
  const dosage = {};
14
228
  const repeat = {};
15
229
  let hasRepeat = false;
16
- if (internal.frequency !== undefined) {
17
- repeat.frequency = internal.frequency;
230
+ const schedule = clause.schedule;
231
+ if ((schedule === null || schedule === void 0 ? void 0 : schedule.frequency) !== undefined) {
232
+ repeat.frequency = schedule.frequency;
18
233
  hasRepeat = true;
19
234
  }
20
- if (internal.count !== undefined) {
21
- repeat.count = internal.count;
235
+ if ((schedule === null || schedule === void 0 ? void 0 : schedule.count) !== undefined) {
236
+ repeat.count = schedule.count;
22
237
  hasRepeat = true;
23
238
  }
24
- if (internal.frequencyMax !== undefined) {
25
- repeat.frequencyMax = internal.frequencyMax;
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;
26
250
  hasRepeat = true;
27
251
  }
28
- if (internal.period !== undefined && internal.periodUnit) {
29
- repeat.period = internal.period;
30
- repeat.periodUnit = internal.periodUnit;
252
+ if ((schedule === null || schedule === void 0 ? void 0 : schedule.period) !== undefined && schedule.periodUnit) {
253
+ repeat.period = schedule.period;
254
+ repeat.periodUnit = schedule.periodUnit;
31
255
  hasRepeat = true;
32
256
  }
33
- if (internal.periodMax !== undefined) {
34
- repeat.periodMax = internal.periodMax;
257
+ if ((schedule === null || schedule === void 0 ? void 0 : schedule.periodMax) !== undefined) {
258
+ repeat.periodMax = schedule.periodMax;
35
259
  hasRepeat = true;
36
260
  }
37
- if (internal.dayOfWeek.length) {
38
- repeat.dayOfWeek = [...internal.dayOfWeek];
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];
39
263
  hasRepeat = true;
40
264
  }
41
- if (internal.when.length) {
42
- repeat.when = [...internal.when];
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];
43
267
  hasRepeat = true;
44
268
  }
45
- if ((_a = internal.timeOfDay) === null || _a === void 0 ? void 0 : _a.length) {
46
- repeat.timeOfDay = [...internal.timeOfDay];
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];
47
271
  hasRepeat = true;
48
272
  }
49
273
  if (hasRepeat) {
50
274
  dosage.timing = { repeat };
51
275
  }
52
- else {
53
- dosage.timing = {};
54
- }
55
- if (internal.timingCode) {
56
- dosage.timing = (_b = dosage.timing) !== null && _b !== void 0 ? _b : {};
276
+ if (schedule === null || schedule === void 0 ? void 0 : schedule.timingCode) {
277
+ dosage.timing = (_d = dosage.timing) !== null && _d !== void 0 ? _d : {};
57
278
  dosage.timing.code = {
58
- coding: [{ code: internal.timingCode }],
59
- text: internal.timingCode
279
+ coding: [{ code: schedule.timingCode }],
280
+ text: schedule.timingCode
60
281
  };
61
282
  }
62
- if (internal.doseRange) {
63
- dosage.doseAndRate = [
64
- {
65
- doseRange: {
66
- low: internal.doseRange.low !== undefined
67
- ? { value: internal.doseRange.low, unit: internal.unit }
68
- : undefined,
69
- high: internal.doseRange.high !== undefined
70
- ? { value: internal.doseRange.high, unit: internal.unit }
71
- : undefined
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
72
289
  }
73
- }
74
- ];
290
+ ];
291
+ }
75
292
  }
76
- else if (internal.dose !== undefined) {
293
+ else if (((_f = clause.dose) === null || _f === void 0 ? void 0 : _f.value) !== undefined) {
77
294
  dosage.doseAndRate = [
78
295
  {
79
296
  doseQuantity: {
80
- value: internal.dose,
81
- unit: internal.unit
297
+ value: clause.dose.value,
298
+ unit: clause.dose.unit
82
299
  }
83
300
  }
84
301
  ];
85
302
  }
86
- // Emit SNOMED-coded routes whenever we have parsed or inferred route data.
87
- if (internal.routeCode || internal.routeText) {
88
- const coding = internal.routeCode ? maps_1.ROUTE_SNOMED[internal.routeCode] : undefined;
89
- const text = (_c = internal.routeText) !== null && _c !== void 0 ? _c : (internal.routeCode ? maps_1.ROUTE_TEXT[internal.routeCode] : undefined);
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);
90
307
  if (coding) {
91
- // Provide both text and coding so human-readable and coded systems align.
92
308
  dosage.route = {
93
309
  text,
94
310
  coding: [
@@ -104,128 +320,332 @@ function toFhir(internal) {
104
320
  dosage.route = { text };
105
321
  }
106
322
  }
107
- if (internal.siteText || ((_d = internal.siteCoding) === null || _d === void 0 ? void 0 : _d.code)) {
108
- const coding = ((_e = internal.siteCoding) === null || _e === void 0 ? void 0 : _e.code)
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)
109
325
  ? [
110
326
  {
111
- system: (_f = internal.siteCoding.system) !== null && _f !== void 0 ? _f : SNOMED_SYSTEM,
112
- code: internal.siteCoding.code,
113
- display: internal.siteCoding.display
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
114
330
  }
115
331
  ]
116
332
  : undefined;
117
333
  dosage.site = {
118
- text: internal.siteText,
334
+ text: (_t = clause.site) === null || _t === void 0 ? void 0 : _t.text,
119
335
  coding
120
336
  };
121
337
  }
122
- if ((_g = internal.additionalInstructions) === null || _g === void 0 ? void 0 : _g.length) {
123
- dosage.additionalInstruction = internal.additionalInstructions.map((instruction) => {
124
- var _a, _b;
125
- return ({
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({
126
358
  text: instruction.text,
127
- coding: ((_a = instruction.coding) === null || _a === void 0 ? void 0 : _a.code)
359
+ coding: ((_4 = instruction.coding) === null || _4 === void 0 ? void 0 : _4.code)
128
360
  ? [
129
361
  {
130
- system: (_b = instruction.coding.system) !== null && _b !== void 0 ? _b : SNOMED_SYSTEM,
362
+ system: (_5 = instruction.coding.system) !== null && _5 !== void 0 ? _5 : SNOMED_SYSTEM,
131
363
  code: instruction.coding.code,
132
364
  display: instruction.coding.display
133
365
  }
134
366
  ]
135
367
  : undefined
136
368
  });
137
- });
369
+ }
138
370
  }
139
- if (internal.asNeeded) {
371
+ if ((_6 = clause.prn) === null || _6 === void 0 ? void 0 : _6.enabled) {
140
372
  dosage.asNeededBoolean = true;
141
- if (internal.asNeededReason || ((_h = internal.asNeededReasonCoding) === null || _h === void 0 ? void 0 : _h.code)) {
142
- const concept = {};
143
- if (internal.asNeededReason) {
144
- concept.text = internal.asNeededReason;
145
- }
146
- if ((_j = internal.asNeededReasonCoding) === null || _j === void 0 ? void 0 : _j.code) {
147
- concept.coding = [
148
- {
149
- system: (_k = internal.asNeededReasonCoding.system) !== null && _k !== void 0 ? _k : SNOMED_SYSTEM,
150
- code: internal.asNeededReasonCoding.code,
151
- display: internal.asNeededReasonCoding.display
152
- }
153
- ];
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);
154
391
  }
155
- dosage.asNeededFor = [concept];
156
392
  }
157
393
  }
158
- const longText = (0, format_1.formatInternal)(internal, "long");
394
+ const longText = textOverride !== null && textOverride !== void 0 ? textOverride : (0, format_1.formatCanonicalClause)(clause, "long");
159
395
  if (longText) {
160
396
  dosage.text = longText;
161
397
  }
398
+ if (clause.patientInstruction) {
399
+ dosage.patientInstruction = clause.patientInstruction;
400
+ }
162
401
  return dosage;
163
402
  }
164
- function internalFromFhir(dosage) {
165
- 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, _13, _14;
166
- const internal = {
167
- input: (_a = dosage.text) !== null && _a !== void 0 ? _a : "",
168
- tokens: [],
169
- consumed: new Set(),
170
- dayOfWeek: ((_c = (_b = dosage.timing) === null || _b === void 0 ? void 0 : _b.repeat) === null || _c === void 0 ? void 0 : _c.dayOfWeek)
171
- ? [...dosage.timing.repeat.dayOfWeek]
172
- : [],
173
- when: ((_e = (_d = dosage.timing) === null || _d === void 0 ? void 0 : _d.repeat) === null || _e === void 0 ? void 0 : _e.when)
174
- ? dosage.timing.repeat.when.filter((value) => (0, array_1.arrayIncludes)((0, object_1.objectValues)(types_1.EventTiming), value))
175
- : [],
176
- timeOfDay: ((_g = (_f = dosage.timing) === null || _f === void 0 ? void 0 : _f.repeat) === null || _g === void 0 ? void 0 : _g.timeOfDay)
177
- ? [...dosage.timing.repeat.timeOfDay]
178
- : [],
179
- warnings: [],
180
- timingCode: (_l = (_k = (_j = (_h = dosage.timing) === null || _h === void 0 ? void 0 : _h.code) === null || _j === void 0 ? void 0 : _j.coding) === null || _k === void 0 ? void 0 : _k[0]) === null || _l === void 0 ? void 0 : _l.code,
181
- count: (_o = (_m = dosage.timing) === null || _m === void 0 ? void 0 : _m.repeat) === null || _o === void 0 ? void 0 : _o.count,
182
- frequency: (_q = (_p = dosage.timing) === null || _p === void 0 ? void 0 : _p.repeat) === null || _q === void 0 ? void 0 : _q.frequency,
183
- frequencyMax: (_s = (_r = dosage.timing) === null || _r === void 0 ? void 0 : _r.repeat) === null || _s === void 0 ? void 0 : _s.frequencyMax,
184
- period: (_u = (_t = dosage.timing) === null || _t === void 0 ? void 0 : _t.repeat) === null || _u === void 0 ? void 0 : _u.period,
185
- periodMax: (_w = (_v = dosage.timing) === null || _v === void 0 ? void 0 : _v.repeat) === null || _w === void 0 ? void 0 : _w.periodMax,
186
- periodUnit: (_y = (_x = dosage.timing) === null || _x === void 0 ? void 0 : _x.repeat) === null || _y === void 0 ? void 0 : _y.periodUnit,
187
- routeText: (_z = dosage.route) === null || _z === void 0 ? void 0 : _z.text,
188
- siteText: (_0 = dosage.site) === null || _0 === void 0 ? void 0 : _0.text,
189
- asNeeded: dosage.asNeededBoolean,
190
- asNeededReason: (_2 = (_1 = dosage.asNeededFor) === null || _1 === void 0 ? void 0 : _1[0]) === null || _2 === void 0 ? void 0 : _2.text,
191
- siteTokenIndices: new Set(),
192
- siteLookups: [],
193
- prnReasonLookups: [],
194
- additionalInstructions: []
195
- };
196
- const routeCoding = (_4 = (_3 = dosage.route) === null || _3 === void 0 ? void 0 : _3.coding) === null || _4 === void 0 ? void 0 : _4.find((code) => code.system === SNOMED_SYSTEM);
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);
197
602
  if (routeCoding === null || routeCoding === void 0 ? void 0 : routeCoding.code) {
198
603
  // Translate SNOMED codings back into the simplified enum for round-trip fidelity.
199
604
  const mapped = maps_1.ROUTE_BY_SNOMED[routeCoding.code];
200
605
  if (mapped) {
201
- internal.routeCode = mapped;
202
- internal.routeText = maps_1.ROUTE_TEXT[mapped];
606
+ state.routeCode = mapped;
607
+ state.routeText = maps_1.ROUTE_TEXT[mapped];
203
608
  }
204
609
  }
205
- const siteCoding = (_6 = (_5 = dosage.site) === null || _5 === void 0 ? void 0 : _5.coding) === null || _6 === void 0 ? void 0 : _6.find((code) => code.system === SNOMED_SYSTEM);
610
+ const siteCoding = selectPreferredSiteCoding(dosage.site);
206
611
  if (siteCoding === null || siteCoding === void 0 ? void 0 : siteCoding.code) {
207
- internal.siteCoding = {
612
+ state.siteCoding = {
208
613
  code: siteCoding.code,
209
614
  display: siteCoding.display,
210
615
  system: siteCoding.system
211
616
  };
617
+ state.siteSource = "text";
618
+ }
619
+ else if ((_7 = dosage.site) === null || _7 === void 0 ? void 0 : _7.text) {
620
+ state.siteSource = "text";
212
621
  }
213
- const reasonCoding = (_9 = (_8 = (_7 = dosage.asNeededFor) === null || _7 === void 0 ? void 0 : _7[0]) === null || _8 === void 0 ? void 0 : _8.coding) === null || _9 === void 0 ? void 0 : _9[0];
214
- if (reasonCoding === null || reasonCoding === void 0 ? void 0 : reasonCoding.code) {
215
- const defaultDef = (0, maps_1.findPrnReasonDefinitionByCoding)((_10 = reasonCoding.system) !== null && _10 !== void 0 ? _10 : SNOMED_SYSTEM, reasonCoding.code);
216
- internal.asNeededReasonCoding = {
217
- code: reasonCoding.code,
218
- display: reasonCoding.display,
219
- system: reasonCoding.system,
220
- i18n: defaultDef === null || defaultDef === void 0 ? void 0 : defaultDef.i18n
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)
221
629
  };
222
630
  }
223
- if ((_11 = dosage.additionalInstruction) === null || _11 === void 0 ? void 0 : _11.length) {
224
- internal.additionalInstructions = dosage.additionalInstruction.map((concept) => {
225
- var _a, _b;
226
- const coding = (_a = concept.coding) === null || _a === void 0 ? void 0 : _a[0];
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);
227
647
  const defaultDef = (coding === null || coding === void 0 ? void 0 : coding.code)
228
- ? (0, maps_1.findAdditionalInstructionDefinitionByCoding)((_b = coding.system) !== null && _b !== void 0 ? _b : SNOMED_SYSTEM, coding.code)
648
+ ? (0, advice_1.findAdditionalInstructionDefinitionByCoding)((_a = coding.system) !== null && _a !== void 0 ? _a : SNOMED_SYSTEM, coding.code)
229
649
  : undefined;
230
650
  return {
231
651
  text: concept.text,
@@ -236,26 +656,38 @@ function internalFromFhir(dosage) {
236
656
  system: coding.system,
237
657
  i18n: defaultDef === null || defaultDef === void 0 ? void 0 : defaultDef.i18n
238
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 })
239
662
  : undefined
240
663
  };
241
664
  });
242
665
  }
243
- const doseAndRate = (_12 = dosage.doseAndRate) === null || _12 === void 0 ? void 0 : _12[0];
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];
244
673
  if (doseAndRate === null || doseAndRate === void 0 ? void 0 : doseAndRate.doseRange) {
245
- const { low, high } = doseAndRate.doseRange;
246
- if ((low === null || low === void 0 ? void 0 : low.value) !== undefined && (high === null || high === void 0 ? void 0 : high.value) !== undefined) {
247
- internal.doseRange = { low: low.value, high: high.value };
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;
248
681
  }
249
- internal.unit = (_14 = (_13 = low === null || low === void 0 ? void 0 : low.unit) !== null && _13 !== void 0 ? _13 : high === null || high === void 0 ? void 0 : high.unit) !== null && _14 !== void 0 ? _14 : internal.unit;
250
682
  }
251
683
  else if (doseAndRate === null || doseAndRate === void 0 ? void 0 : doseAndRate.doseQuantity) {
252
684
  const dose = doseAndRate.doseQuantity;
253
685
  if (dose.value !== undefined) {
254
- internal.dose = dose.value;
686
+ state.dose = dose.value;
255
687
  }
256
688
  if (dose.unit) {
257
- internal.unit = dose.unit;
689
+ state.unit = dose.unit;
258
690
  }
259
691
  }
260
- return internal;
692
+ return state;
261
693
  }