ochre-sdk 1.0.13 → 1.0.15

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 (51) hide show
  1. package/dist/constants.d.mts +17 -0
  2. package/dist/constants.mjs +85 -0
  3. package/dist/fetchers/gallery.d.mts +38 -0
  4. package/dist/fetchers/gallery.mjs +91 -0
  5. package/dist/fetchers/item-links.d.mts +32 -0
  6. package/dist/fetchers/item-links.mjs +120 -0
  7. package/dist/fetchers/item.d.mts +74 -0
  8. package/dist/fetchers/item.mjs +146 -0
  9. package/dist/fetchers/set/items.d.mts +48 -0
  10. package/dist/fetchers/set/items.mjs +268 -0
  11. package/dist/fetchers/set/property-values.d.mts +46 -0
  12. package/dist/fetchers/set/property-values.mjs +514 -0
  13. package/dist/fetchers/website.d.mts +25 -0
  14. package/dist/fetchers/website.mjs +38 -0
  15. package/dist/getters.d.mts +193 -0
  16. package/dist/getters.mjs +341 -0
  17. package/dist/helpers.d.mts +18 -0
  18. package/dist/helpers.mjs +33 -0
  19. package/dist/index.d.mts +12 -1971
  20. package/dist/index.mjs +9 -7236
  21. package/dist/parsers/helpers.d.mts +27 -0
  22. package/dist/parsers/helpers.mjs +53 -0
  23. package/dist/parsers/index.d.mts +65 -0
  24. package/dist/parsers/index.mjs +1338 -0
  25. package/dist/parsers/mdx.d.mts +4 -0
  26. package/dist/parsers/mdx.mjs +9 -0
  27. package/dist/parsers/multilingual.d.mts +189 -0
  28. package/dist/parsers/multilingual.mjs +410 -0
  29. package/dist/parsers/string.d.mts +29 -0
  30. package/dist/parsers/string.mjs +445 -0
  31. package/dist/parsers/website/index.d.mts +20 -0
  32. package/dist/parsers/website/index.mjs +1245 -0
  33. package/dist/parsers/website/reader.d.mts +29 -0
  34. package/dist/parsers/website/reader.mjs +75 -0
  35. package/dist/query.d.mts +13 -0
  36. package/dist/query.mjs +827 -0
  37. package/dist/schemas.d.mts +79 -0
  38. package/dist/schemas.mjs +223 -0
  39. package/dist/types/index.d.mts +840 -0
  40. package/dist/types/index.mjs +1 -0
  41. package/dist/types/website.d.mts +501 -0
  42. package/dist/types/website.mjs +1 -0
  43. package/dist/utils.d.mts +34 -0
  44. package/dist/utils.mjs +172 -0
  45. package/dist/xml/metadata.d.mts +5 -0
  46. package/dist/xml/metadata.mjs +30 -0
  47. package/dist/xml/schemas.d.mts +13 -0
  48. package/dist/xml/schemas.mjs +849 -0
  49. package/dist/xml/types.d.mts +901 -0
  50. package/dist/xml/types.mjs +1 -0
  51. package/package.json +19 -17
@@ -0,0 +1,1338 @@
1
+ import { DEFAULT_LANGUAGES } from "../constants.mjs";
2
+ import { MultilingualString } from "./multilingual.mjs";
3
+ import { getXMLSourceIndex } from "../xml/metadata.mjs";
4
+ import { parseXMLString, transformPermanentIdentificationUrl } from "./string.mjs";
5
+ import { getParserOptions, multilingualFromText, parseContentLike, parseContentLikeText, parseLicense, parseRequiredContentLike, parseStringLike } from "./helpers.mjs";
6
+ //#region src/parsers/index.ts
7
+ function isXMLContextGroup(context) {
8
+ return "context" in context;
9
+ }
10
+ function isXMLContextItem(context) {
11
+ return "project" in context;
12
+ }
13
+ const SET_ITEM_CATEGORIES = ["tree", ...[
14
+ "bibliography",
15
+ "concept",
16
+ "spatialUnit",
17
+ "period",
18
+ "person",
19
+ "propertyVariable",
20
+ "propertyValue",
21
+ "resource",
22
+ "text",
23
+ "set"
24
+ ]];
25
+ const HEADING_ITEM_CATEGORIES = [
26
+ "person",
27
+ "propertyVariable",
28
+ "propertyValue",
29
+ "resource",
30
+ "text",
31
+ "set"
32
+ ];
33
+ const CONTEXT_CATEGORY_MAPPINGS = [
34
+ {
35
+ raw: "bibliography",
36
+ parsed: "bibliography"
37
+ },
38
+ {
39
+ raw: "concept",
40
+ parsed: "concept"
41
+ },
42
+ {
43
+ raw: "spatialUnit",
44
+ parsed: "spatialUnit"
45
+ },
46
+ {
47
+ raw: "period",
48
+ parsed: "period"
49
+ },
50
+ {
51
+ raw: "propertyVariable",
52
+ parsed: "propertyVariable"
53
+ },
54
+ {
55
+ raw: "variable",
56
+ parsed: "propertyVariable"
57
+ },
58
+ {
59
+ raw: "propertyValue",
60
+ parsed: "propertyValue"
61
+ },
62
+ {
63
+ raw: "value",
64
+ parsed: "propertyValue"
65
+ },
66
+ {
67
+ raw: "resource",
68
+ parsed: "resource"
69
+ },
70
+ {
71
+ raw: "text",
72
+ parsed: "text"
73
+ }
74
+ ];
75
+ const PROPERTY_DATA_TYPES = [
76
+ "string",
77
+ "coordinate",
78
+ "IDREF",
79
+ "date",
80
+ "dateTime",
81
+ "integer",
82
+ "decimal",
83
+ "time",
84
+ "boolean"
85
+ ];
86
+ function parseIdentification(rawIdentification, options) {
87
+ return {
88
+ label: parseRequiredContentLike(rawIdentification.label, options),
89
+ abbreviation: parseContentLike(rawIdentification.abbreviation, options),
90
+ code: parseStringLike(rawIdentification.code),
91
+ email: parseStringLike(rawIdentification.email),
92
+ website: parseStringLike(rawIdentification.website)
93
+ };
94
+ }
95
+ function emptyIdentification(options) {
96
+ return {
97
+ label: MultilingualString.empty(options.languages),
98
+ abbreviation: null,
99
+ code: null,
100
+ email: null,
101
+ website: null
102
+ };
103
+ }
104
+ function parseOptionalIdentification(rawIdentification, options) {
105
+ return rawIdentification == null ? emptyIdentification(options) : parseIdentification(rawIdentification, options);
106
+ }
107
+ function parseContextItem(contextItem) {
108
+ return {
109
+ uuid: contextItem.uuid ?? null,
110
+ publicationDateTime: contextItem.publicationDateTime ?? null,
111
+ index: contextItem.n,
112
+ content: contextItem.payload
113
+ };
114
+ }
115
+ function emptyContextItem() {
116
+ return {
117
+ uuid: null,
118
+ publicationDateTime: null,
119
+ index: 0,
120
+ content: ""
121
+ };
122
+ }
123
+ function parseContext(rawContext) {
124
+ const nodes = [];
125
+ let displayPath = "";
126
+ for (const rawContextOuterItem of rawContext) {
127
+ if (!isXMLContextGroup(rawContextOuterItem)) continue;
128
+ displayPath = displayPath || rawContextOuterItem.displayPath;
129
+ for (const rawContextItem of rawContextOuterItem.context) {
130
+ if (!isXMLContextItem(rawContextItem)) continue;
131
+ const node = {
132
+ tree: rawContextItem.tree[0] == null ? emptyContextItem() : parseContextItem(rawContextItem.tree[0]),
133
+ project: parseContextItem(rawContextItem.project),
134
+ heading: []
135
+ };
136
+ const rawContextValues = rawContextItem;
137
+ for (const heading of rawContextValues.heading ?? []) node.heading.push(parseContextItem(heading));
138
+ for (const { raw, parsed } of CONTEXT_CATEGORY_MAPPINGS) {
139
+ const contextValues = rawContextValues[raw] ?? [];
140
+ for (const contextValue of contextValues) {
141
+ const parsedItems = node[parsed] ?? [];
142
+ parsedItems.push(parseContextItem(contextValue));
143
+ node[parsed] = parsedItems;
144
+ }
145
+ }
146
+ nodes.push(node);
147
+ }
148
+ }
149
+ return {
150
+ nodes,
151
+ displayPath
152
+ };
153
+ }
154
+ function parseEventReference(rawReference, options) {
155
+ if (rawReference == null) return null;
156
+ return {
157
+ uuid: rawReference.uuid,
158
+ label: parseRequiredContentLike(rawReference, options),
159
+ publicationDateTime: rawReference.publicationDateTime ?? null
160
+ };
161
+ }
162
+ function parseEvent(rawEvent, options) {
163
+ const startDate = rawEvent.dateTime ?? null;
164
+ const endDate = rawEvent.endDateTime ?? null;
165
+ return {
166
+ date: startDate == null ? null : endDate == null ? startDate : {
167
+ start: startDate,
168
+ end: endDate
169
+ },
170
+ label: parseRequiredContentLike(rawEvent.label, options),
171
+ comment: parseContentLike(rawEvent.comment, options),
172
+ agent: parseEventReference(rawEvent.agent, options),
173
+ location: parseEventReference(rawEvent.location, options),
174
+ other: rawEvent.other == null ? null : {
175
+ uuid: rawEvent.other.uuid ?? null,
176
+ category: normalizeCategory(rawEvent.other.category),
177
+ label: parseRequiredContentLike(rawEvent.other, options)
178
+ }
179
+ };
180
+ }
181
+ function parseBaseItem(category, rawItem, options) {
182
+ const events = [];
183
+ for (const event of rawItem.events?.event ?? []) events.push(parseEvent(event, options));
184
+ const creators = [];
185
+ for (const creator of rawItem.creators?.creator ?? []) creators.push(parsePerson(creator, options));
186
+ return {
187
+ uuid: rawItem.uuid ?? "",
188
+ category,
189
+ belongsTo: null,
190
+ metadata: null,
191
+ persistentUrl: null,
192
+ publicationDateTime: rawItem.publicationDateTime ?? null,
193
+ context: rawItem.context == null ? null : parseContext(rawItem.context),
194
+ date: rawItem.date instanceof Date ? rawItem.date : null,
195
+ license: parseLicense(rawItem.availability),
196
+ copyright: rawItem.copyright == null ? null : parseContentLike(rawItem.copyright, options),
197
+ watermark: rawItem.watermark == null ? null : parseContentLike(rawItem.watermark, options),
198
+ identification: parseOptionalIdentification(rawItem.identification, options),
199
+ creators,
200
+ description: parseContentLike(rawItem.description, options),
201
+ events
202
+ };
203
+ }
204
+ function normalizeCategory(category) {
205
+ if (category == null) return null;
206
+ if (category === "variable") return "propertyVariable";
207
+ if (category === "value") return "propertyValue";
208
+ for (const knownCategory of SET_ITEM_CATEGORIES) if (category === knownCategory) return knownCategory;
209
+ return null;
210
+ }
211
+ function isHeadingItemCategory(category) {
212
+ return HEADING_ITEM_CATEGORIES.includes(category);
213
+ }
214
+ function pushCategory(categories, category) {
215
+ if (!categories.includes(category)) categories.push(category);
216
+ }
217
+ function getHierarchyEntryCategory(key) {
218
+ switch (key) {
219
+ case "heading":
220
+ case "tree":
221
+ case "bibliography":
222
+ case "concept":
223
+ case "spatialUnit":
224
+ case "period":
225
+ case "person":
226
+ case "resource":
227
+ case "text":
228
+ case "set":
229
+ case "dictionaryUnit": return key;
230
+ case "propertyVariable":
231
+ case "variable": return "propertyVariable";
232
+ case "propertyValue":
233
+ case "value": return "propertyValue";
234
+ default: return null;
235
+ }
236
+ }
237
+ function isRecord(value) {
238
+ return typeof value === "object" && value != null;
239
+ }
240
+ function isResourceWrapper(value) {
241
+ return isRecord(value) && !("uuid" in value) && Array.isArray(value.resource);
242
+ }
243
+ function sourceOrderSort(left, right) {
244
+ const leftIndex = getXMLSourceIndex(left.item);
245
+ const rightIndex = getXMLSourceIndex(right.item);
246
+ if (leftIndex != null && rightIndex != null && leftIndex !== rightIndex) return leftIndex - rightIndex;
247
+ return left.fallbackIndex - right.fallbackIndex;
248
+ }
249
+ function collectHierarchyEntries(hierarchy, categories) {
250
+ const entries = [];
251
+ if (hierarchy == null) return entries;
252
+ let fallbackIndex = 0;
253
+ for (const key of Object.keys(hierarchy)) {
254
+ const category = getHierarchyEntryCategory(key);
255
+ if (category == null || !(categories == null || categories.includes(category))) continue;
256
+ const values = hierarchy[key];
257
+ if (!Array.isArray(values)) continue;
258
+ for (const value of values) {
259
+ if (category === "resource" && isResourceWrapper(value)) {
260
+ for (const resource of value.resource) {
261
+ entries.push({
262
+ category,
263
+ item: resource,
264
+ fallbackIndex
265
+ });
266
+ fallbackIndex += 1;
267
+ }
268
+ continue;
269
+ }
270
+ entries.push({
271
+ category,
272
+ item: value,
273
+ fallbackIndex
274
+ });
275
+ fallbackIndex += 1;
276
+ }
277
+ }
278
+ entries.sort(sourceOrderSort);
279
+ return entries;
280
+ }
281
+ function inferItemCategories(hierarchy) {
282
+ const categories = [];
283
+ if (hierarchy == null) return categories;
284
+ for (const entry of collectHierarchyEntries(hierarchy)) {
285
+ if (entry.category === "heading") {
286
+ for (const category of inferItemCategories(entry.item)) pushCategory(categories, category);
287
+ continue;
288
+ }
289
+ if (entry.category === "dictionaryUnit") continue;
290
+ pushCategory(categories, entry.category);
291
+ }
292
+ return categories;
293
+ }
294
+ function normalizeSetItemCategories(containedItemCategory) {
295
+ if (containedItemCategory == null) return null;
296
+ const categories = typeof containedItemCategory === "string" ? [containedItemCategory] : containedItemCategory;
297
+ const uniqueCategories = [];
298
+ for (const category of categories) if (!uniqueCategories.includes(category)) uniqueCategories.push(category);
299
+ return uniqueCategories;
300
+ }
301
+ function normalizeTreeItemCategory(containedItemCategory) {
302
+ if (containedItemCategory != null && typeof containedItemCategory !== "string") throw new Error("Tree containedItemCategory must be a single category", { cause: containedItemCategory });
303
+ if (containedItemCategory === "tree") throw new Error("Tree containedItemCategory cannot be \"tree\"", { cause: containedItemCategory });
304
+ return containedItemCategory;
305
+ }
306
+ function resolveTreeItemCategory(rawTree, containedItemCategory) {
307
+ const inferredCategories = inferItemCategories(rawTree.items);
308
+ if (inferredCategories.length > 1) throw new Error(`Expected Tree items to contain one category, received ${inferredCategories.join(", ")}`, { cause: inferredCategories });
309
+ const inferredCategory = inferredCategories[0] ?? null;
310
+ if (inferredCategory === "tree") throw new Error("Tree items cannot contain category \"tree\"", { cause: inferredCategory });
311
+ if (containedItemCategory != null && inferredCategory != null && containedItemCategory !== inferredCategory) throw new Error(`Tree containedItemCategory "${containedItemCategory}" does not match XML items category "${inferredCategory}"`, { cause: {
312
+ containedItemCategory,
313
+ inferredCategory
314
+ } });
315
+ return containedItemCategory ?? inferredCategory;
316
+ }
317
+ function parseImage(rawImage, options) {
318
+ if (rawImage == null) return null;
319
+ return {
320
+ publicationDateTime: rawImage.publicationDateTime ?? null,
321
+ identification: rawImage.identification == null ? null : parseIdentification(rawImage.identification, options),
322
+ href: parseHref(rawImage.href),
323
+ htmlImgSrcPrefix: rawImage.htmlImgSrcPrefix ?? null,
324
+ height: rawImage.height ?? null,
325
+ width: rawImage.width ?? null,
326
+ fileSize: rawImage.fileSize ?? null,
327
+ base64: rawImage.payload ?? null
328
+ };
329
+ }
330
+ function parseHref(href) {
331
+ return href == null ? null : transformPermanentIdentificationUrl(href);
332
+ }
333
+ function parseCoordinatesSource(source, options) {
334
+ if (source == null) return null;
335
+ switch (source.context) {
336
+ case "self": return {
337
+ context: "self",
338
+ uuid: source.label.uuid,
339
+ label: parseRequiredContentLike(source.label, options)
340
+ };
341
+ case "related": {
342
+ const value = source.value[0];
343
+ return {
344
+ context: "related",
345
+ uuid: source.label.uuid,
346
+ label: parseRequiredContentLike(source.label, options),
347
+ value: value == null ? MultilingualString.empty(options.languages) : parseRequiredContentLike(value, options)
348
+ };
349
+ }
350
+ case "inherited": return {
351
+ context: "inherited",
352
+ uuid: source.label.uuid,
353
+ label: parseRequiredContentLike(source.label, options),
354
+ item: {
355
+ uuid: source.item.label.uuid ?? source.item.uuid ?? null,
356
+ label: parseRequiredContentLike(source.item.label, options)
357
+ }
358
+ };
359
+ }
360
+ }
361
+ function parseCoordinate(coordinate, options) {
362
+ switch (coordinate.type) {
363
+ case "point": return {
364
+ type: "point",
365
+ latitude: coordinate.latitude,
366
+ longitude: coordinate.longitude,
367
+ altitude: coordinate.altitude ?? null,
368
+ source: parseCoordinatesSource(coordinate.source, options)
369
+ };
370
+ case "plane": return {
371
+ type: "plane",
372
+ minimum: {
373
+ latitude: coordinate.minimum.latitude,
374
+ longitude: coordinate.minimum.longitude
375
+ },
376
+ maximum: {
377
+ latitude: coordinate.maximum.latitude,
378
+ longitude: coordinate.maximum.longitude
379
+ },
380
+ source: parseCoordinatesSource(coordinate.source, options)
381
+ };
382
+ }
383
+ }
384
+ function parseCoordinates(rawCoordinates, options) {
385
+ const coordinates = [];
386
+ for (const coordinate of rawCoordinates?.coord ?? []) coordinates.push(parseCoordinate(coordinate, options));
387
+ return coordinates;
388
+ }
389
+ function parseImageMapArea(area) {
390
+ const coords = [];
391
+ for (const coord of area.coords.split(",")) {
392
+ const parsedCoord = Number(coord.trim());
393
+ coords.push(Number.isNaN(parsedCoord) ? 0 : parsedCoord);
394
+ }
395
+ const shape = area.shape === "rect" ? "rectangle" : area.shape === "circle" ? "circle" : "polygon";
396
+ return {
397
+ uuid: area.uuid,
398
+ publicationDateTime: area.publicationDateTime,
399
+ type: area.type,
400
+ title: area.title,
401
+ slug: area.slug ?? null,
402
+ items: shape === "rectangle" ? [{
403
+ shape,
404
+ coords: [
405
+ coords[0] ?? 0,
406
+ coords[1] ?? 0,
407
+ coords[2] ?? 0,
408
+ coords[3] ?? 0
409
+ ]
410
+ }] : shape === "circle" ? [{
411
+ shape,
412
+ center: {
413
+ x: coords[0] ?? 0,
414
+ y: coords[1] ?? 0
415
+ },
416
+ radius: coords[2] ?? 0
417
+ }] : [{
418
+ shape,
419
+ coords
420
+ }]
421
+ };
422
+ }
423
+ function parseImageMap(rawImageMap) {
424
+ if (rawImageMap == null) return null;
425
+ const areas = [];
426
+ for (const area of rawImageMap.area) areas.push(parseImageMapArea(area));
427
+ return {
428
+ areas,
429
+ width: rawImageMap.width,
430
+ height: rawImageMap.height
431
+ };
432
+ }
433
+ function parseNote(rawNote, options) {
434
+ const authors = [];
435
+ for (const author of rawNote.authors?.author ?? []) authors.push(parsePerson(author, options));
436
+ const content = rawNote.content == null ? multilingualFromText(parseXMLString(rawNote), options) : parseRequiredContentLike(rawNote, options);
437
+ return {
438
+ number: rawNote.noteNo ?? 0,
439
+ title: parseNoteTitle(rawNote, options),
440
+ content,
441
+ authors
442
+ };
443
+ }
444
+ function parseNoteTitle(rawNote, options) {
445
+ if (rawNote.title != null) return multilingualFromText(rawNote.title, options);
446
+ const titleContent = {};
447
+ for (const content of rawNote.content ?? []) {
448
+ if (!options.languages.includes(content.lang) || content.title == null) continue;
449
+ titleContent[content.lang] = content.title;
450
+ }
451
+ if (Object.keys(titleContent).length > 0) return MultilingualString.fromObject(titleContent, options.languages);
452
+ for (const content of rawNote.content ?? []) if (content.lang !== "zxx" && content.title != null) return multilingualFromText(content.title, options);
453
+ return null;
454
+ }
455
+ function parseNotes(rawNotes, options) {
456
+ const notes = [];
457
+ for (const note of rawNotes?.note ?? []) notes.push(parseNote(note, options));
458
+ return notes;
459
+ }
460
+ function parsePropertyDataType(dataType) {
461
+ if (dataType == null || dataType === "") return "string";
462
+ const normalizedDataType = dataType.startsWith("xs:") ? dataType.slice(3) : dataType;
463
+ for (const propertyDataType of PROPERTY_DATA_TYPES) if (normalizedDataType === propertyDataType) return propertyDataType;
464
+ throw new Error(`Invalid property value data type: ${dataType}`, { cause: dataType });
465
+ }
466
+ function parsePropertyValueContent(value, options) {
467
+ const dataType = parsePropertyDataType(value.dataType);
468
+ const rawLabel = value.content != null ? parseRequiredContentLike(value, options) : value.payload != null && value.payload !== "" ? multilingualFromText(value.payload, options) : null;
469
+ const displayText = rawLabel?.getText() ?? value.payload ?? value.slug ?? "";
470
+ const contentText = value.rawValue ?? value.payload ?? displayText;
471
+ const common = {
472
+ hierarchy: {
473
+ isLeaf: value.inherited == null || !value.inherited,
474
+ level: value.i ?? null
475
+ },
476
+ label: rawLabel,
477
+ isUncertain: value.isUncertain === "true",
478
+ category: value.category ?? null,
479
+ type: value.type ?? null,
480
+ uuid: value.uuid == null || value.uuid === "" ? null : value.uuid,
481
+ publicationDateTime: value.publicationDateTime ?? null,
482
+ unit: value.unit ?? null,
483
+ href: parseHref(value.href),
484
+ height: value.height ?? null,
485
+ width: value.width ?? null,
486
+ fileSize: value.fileSize ?? null,
487
+ slug: value.slug ?? null
488
+ };
489
+ switch (dataType) {
490
+ case "integer":
491
+ case "decimal":
492
+ case "time": {
493
+ const numericContent = Number(contentText);
494
+ return {
495
+ ...common,
496
+ dataType,
497
+ content: Number.isNaN(numericContent) ? 0 : numericContent
498
+ };
499
+ }
500
+ case "boolean": return {
501
+ ...common,
502
+ dataType,
503
+ content: contentText === "true"
504
+ };
505
+ case "string":
506
+ case "coordinate":
507
+ case "IDREF":
508
+ case "date":
509
+ case "dateTime": return {
510
+ ...common,
511
+ dataType,
512
+ content: contentText
513
+ };
514
+ }
515
+ }
516
+ function parseProperty(rawProperty, options) {
517
+ const values = [];
518
+ for (const value of rawProperty.value ?? []) values.push(parsePropertyValueContent(value, options));
519
+ const properties = [];
520
+ for (const property of rawProperty.property ?? []) properties.push(parseProperty(property, options));
521
+ return {
522
+ variable: {
523
+ uuid: rawProperty.label.uuid,
524
+ label: parseRequiredContentLike(rawProperty.label, options),
525
+ publicationDateTime: rawProperty.label.publicationDateTime ?? null
526
+ },
527
+ values,
528
+ comment: parseContentLike(rawProperty.comment, options),
529
+ properties
530
+ };
531
+ }
532
+ function parseProperties(rawProperties, options) {
533
+ const properties = [];
534
+ for (const property of rawProperties?.property ?? []) properties.push(parseProperty(property, options));
535
+ return properties;
536
+ }
537
+ function parseSimplifiedProperty(rawProperty, options) {
538
+ const values = [];
539
+ for (const value of rawProperty.value ?? []) values.push(parsePropertyValueContent(value, options));
540
+ const properties = [];
541
+ for (const property of rawProperty.property ?? []) properties.push(parseSimplifiedProperty(property, options));
542
+ return {
543
+ variable: {
544
+ uuid: rawProperty.label.uuid,
545
+ label: parseContentLikeText(rawProperty.label, options),
546
+ publicationDateTime: rawProperty.label.publicationDateTime ?? null
547
+ },
548
+ values,
549
+ comment: parseContentLike(rawProperty.comment, options),
550
+ properties
551
+ };
552
+ }
553
+ function parseSimplifiedProperties(rawProperties, options) {
554
+ const properties = [];
555
+ for (const property of rawProperties?.property ?? []) properties.push(parseSimplifiedProperty(property, options));
556
+ return properties;
557
+ }
558
+ function parseSetItemProperty(rawProperty, options) {
559
+ const property = parseProperty(rawProperty, options);
560
+ return {
561
+ variable: property.variable,
562
+ values: property.values,
563
+ comment: property.comment
564
+ };
565
+ }
566
+ function parseSetItemProperties(rawProperties, options) {
567
+ const properties = [];
568
+ for (const property of rawProperties?.property ?? []) properties.push(parseSetItemProperty(property, options));
569
+ return properties;
570
+ }
571
+ function withSetItemProperties(item, rawProperties, options) {
572
+ return {
573
+ ...item,
574
+ properties: parseSetItemProperties(rawProperties, options)
575
+ };
576
+ }
577
+ function withoutItems(item) {
578
+ const { items: _items, ...itemWithoutItems } = item;
579
+ return itemWithoutItems;
580
+ }
581
+ function parseEmbeddedItemEntry(entry, options) {
582
+ switch (entry.category) {
583
+ case "tree": return parseTree(entry.item, {
584
+ ...options,
585
+ containedItemCategory: normalizeTreeItemCategory(options.containedItemCategory)
586
+ });
587
+ case "bibliography": return parseBibliography(entry.item, options);
588
+ case "concept": return parseConcept(entry.item, options);
589
+ case "spatialUnit": return parseSpatialUnit(entry.item, options);
590
+ case "period": return parsePeriod(entry.item, options);
591
+ case "person": return parsePerson(entry.item, options);
592
+ case "propertyVariable": return parsePropertyVariable(entry.item, options);
593
+ case "propertyValue": return parsePropertyValue(entry.item, options);
594
+ case "resource": return parseResource(entry.item, options);
595
+ case "text": return parseText(entry.item, options);
596
+ case "set": return parseSet(entry.item, options);
597
+ case "dictionaryUnit":
598
+ case "heading": return null;
599
+ }
600
+ }
601
+ function parseItemHierarchy(hierarchy, options, categories) {
602
+ const items = [];
603
+ if (hierarchy == null) return items;
604
+ for (const entry of collectHierarchyEntries(hierarchy, categories)) {
605
+ const item = parseEmbeddedItemEntry(entry, options);
606
+ if (item != null) items.push(item);
607
+ }
608
+ return items;
609
+ }
610
+ function parseSetItemHierarchy(hierarchy, options, categories) {
611
+ const items = [];
612
+ if (hierarchy == null) return items;
613
+ for (const entry of collectHierarchyEntries(hierarchy, categories)) switch (entry.category) {
614
+ case "tree":
615
+ items.push(parseSetTree(entry.item, options));
616
+ break;
617
+ case "bibliography":
618
+ items.push(parseSetBibliography(entry.item, options));
619
+ break;
620
+ case "concept":
621
+ items.push(parseSetConcept(entry.item, options));
622
+ break;
623
+ case "spatialUnit":
624
+ items.push(parseSetSpatialUnit(entry.item, options));
625
+ break;
626
+ case "period":
627
+ items.push(parseSetPeriod(entry.item, options));
628
+ break;
629
+ case "person": {
630
+ const person = entry.item;
631
+ items.push(withSetItemProperties(parsePerson(person, options), person.properties, options));
632
+ break;
633
+ }
634
+ case "propertyVariable":
635
+ items.push(parsePropertyVariable(entry.item, options));
636
+ break;
637
+ case "propertyValue": {
638
+ const propertyValue = entry.item;
639
+ items.push(withSetItemProperties(parsePropertyValue(propertyValue, options), propertyValue.properties, options));
640
+ break;
641
+ }
642
+ case "resource":
643
+ items.push(parseSetResource(entry.item, options));
644
+ break;
645
+ case "text":
646
+ items.push(parseText(entry.item, options));
647
+ break;
648
+ case "set":
649
+ items.push(parseSetSet(entry.item, options));
650
+ break;
651
+ case "dictionaryUnit":
652
+ case "heading": break;
653
+ }
654
+ return items;
655
+ }
656
+ function normalizeTreeLinkItemsCategory(type) {
657
+ const category = normalizeCategory(type);
658
+ if (category == null || category === "tree") return null;
659
+ return category;
660
+ }
661
+ function normalizeSetLinkItemsCategory(type) {
662
+ const category = normalizeCategory(type);
663
+ return category == null ? null : [category];
664
+ }
665
+ function parseBaseItemLink(category, rawItem, options) {
666
+ return {
667
+ uuid: rawItem.uuid ?? "",
668
+ category,
669
+ publicationDateTime: rawItem.publicationDateTime ?? null,
670
+ context: rawItem.context == null ? null : parseContext(rawItem.context),
671
+ date: rawItem.date instanceof Date ? rawItem.date : null,
672
+ identification: parseOptionalIdentification(rawItem.identification, options),
673
+ description: parseContentLike(rawItem.description, options)
674
+ };
675
+ }
676
+ function parseBibliographySourceDocument(sourceDocument) {
677
+ if (sourceDocument == null) return null;
678
+ return {
679
+ uuid: sourceDocument.uuid,
680
+ content: sourceDocument.payload,
681
+ href: parseHref(sourceDocument.href),
682
+ publicationDateTime: sourceDocument.publicationDateTime ?? null
683
+ };
684
+ }
685
+ function parseBibliographyStartDate(startDate) {
686
+ if (startDate == null) return null;
687
+ return new Date(startDate.year ?? 0, Math.max((startDate.month ?? 0) - 1, 0), startDate.day ?? 0);
688
+ }
689
+ function parseBibliographyEntryInfo(entryInfo) {
690
+ if (entryInfo == null || entryInfo.payload == null && entryInfo.startIssue == null && entryInfo.startVolume == null && entryInfo.startPage == null && entryInfo.endPage == null) return null;
691
+ return {
692
+ content: entryInfo.payload ?? null,
693
+ startIssue: entryInfo.startIssue ?? "",
694
+ startVolume: entryInfo.startVolume ?? "",
695
+ startPage: entryInfo.startPage ?? "",
696
+ endPage: entryInfo.endPage ?? ""
697
+ };
698
+ }
699
+ function firstItemLink(rawLinks, options) {
700
+ const link = parseLinks(rawLinks, options)[0];
701
+ return link == null ? null : link;
702
+ }
703
+ function parsePersonItemLinks(rawPersons, options) {
704
+ const people = [];
705
+ for (const person of rawPersons ?? []) people.push(parsePersonItemLink(person, options));
706
+ return people;
707
+ }
708
+ function parsePeriodItemLinks(rawPeriods, options) {
709
+ const periods = [];
710
+ for (const period of rawPeriods ?? []) periods.push(parsePeriodItemLink(period, options));
711
+ return periods;
712
+ }
713
+ function parseTreeItemLink(rawTree, options) {
714
+ return {
715
+ ...parseBaseItemLink("tree", rawTree, options),
716
+ type: rawTree.type ?? null,
717
+ containedItemCategory: normalizeTreeLinkItemsCategory(rawTree.type)
718
+ };
719
+ }
720
+ function parseSetItemLink(rawSet, options) {
721
+ return {
722
+ ...parseBaseItemLink("set", rawSet, options),
723
+ type: rawSet.type ?? null,
724
+ containedItemCategories: normalizeSetLinkItemsCategory(rawSet.type)
725
+ };
726
+ }
727
+ function parseBibliographyItemLink(rawBibliography, options) {
728
+ return {
729
+ ...parseBaseItemLink("bibliography", rawBibliography, options),
730
+ type: rawBibliography.type ?? null,
731
+ zoteroId: rawBibliography.zoteroId ?? null,
732
+ citationDetails: rawBibliography.citationDetails ?? null,
733
+ citationFormat: parseContentLike(rawBibliography.citationFormat, options),
734
+ citationFormatSpan: parseStringLike(rawBibliography.citationFormatSpan),
735
+ referenceFormatDiv: parseStringLike(rawBibliography.referenceFormatDiv),
736
+ image: parseImage(rawBibliography.image, options),
737
+ sourceDocument: parseBibliographySourceDocument(rawBibliography.sourceDocument),
738
+ publicationInfo: rawBibliography.publicationInfo == null ? null : {
739
+ publishers: parsePersonItemLinks(rawBibliography.publicationInfo.publishers == null ? void 0 : "publisher" in rawBibliography.publicationInfo.publishers ? rawBibliography.publicationInfo.publishers.publisher : rawBibliography.publicationInfo.publishers.publishers.person, options),
740
+ startDate: parseBibliographyStartDate(rawBibliography.publicationInfo.startDate)
741
+ },
742
+ entryInfo: parseBibliographyEntryInfo(rawBibliography.entryInfo),
743
+ source: firstItemLink(rawBibliography.source, options),
744
+ authors: parsePersonItemLinks(rawBibliography.authors?.person, options),
745
+ periods: parsePeriodItemLinks(rawBibliography.periods?.period, options),
746
+ properties: parseProperties(rawBibliography.properties, options)
747
+ };
748
+ }
749
+ function parseConceptItemLink(rawConcept, options) {
750
+ return {
751
+ ...parseBaseItemLink("concept", rawConcept, options),
752
+ image: parseImage(rawConcept.image, options),
753
+ coordinates: parseCoordinates(rawConcept.coordinates, options)
754
+ };
755
+ }
756
+ function parseSpatialUnitItemLink(rawSpatialUnit, options) {
757
+ return {
758
+ ...parseBaseItemLink("spatialUnit", rawSpatialUnit, options),
759
+ image: parseImage(rawSpatialUnit.image, options),
760
+ coordinates: parseCoordinates(rawSpatialUnit.coordinates, options)
761
+ };
762
+ }
763
+ function parsePeriodItemLink(rawPeriod, options) {
764
+ return {
765
+ ...parseBaseItemLink("period", rawPeriod, options),
766
+ type: rawPeriod.type ?? null,
767
+ coordinates: parseCoordinates(rawPeriod.coordinates, options)
768
+ };
769
+ }
770
+ function parsePersonItemLink(rawPerson, options) {
771
+ return {
772
+ ...parseBaseItemLink("person", rawPerson, options),
773
+ type: rawPerson.type ?? null,
774
+ coordinates: parseCoordinates(rawPerson.coordinates, options)
775
+ };
776
+ }
777
+ function parsePropertyVariableItemLink(rawPropertyVariable, options) {
778
+ return {
779
+ ...parseBaseItemLink("propertyVariable", rawPropertyVariable, options),
780
+ type: rawPropertyVariable.type ?? null,
781
+ coordinates: parseCoordinates(rawPropertyVariable.coordinates, options)
782
+ };
783
+ }
784
+ function parsePropertyValueItemLink(rawPropertyValue, options) {
785
+ return {
786
+ ...parseBaseItemLink("propertyValue", rawPropertyValue, options),
787
+ coordinates: parseCoordinates(rawPropertyValue.coordinates, options)
788
+ };
789
+ }
790
+ function parseResourceItemLink(rawResource, options) {
791
+ return {
792
+ ...parseBaseItemLink("resource", rawResource, options),
793
+ type: rawResource.type ?? null,
794
+ href: parseHref(rawResource.href),
795
+ fileFormat: rawResource.fileFormat ?? null,
796
+ fileSize: rawResource.fileSize ?? null,
797
+ isInline: rawResource.rend === "inline",
798
+ isPrimary: rawResource.isPrimary ?? false,
799
+ height: rawResource.height ?? null,
800
+ width: rawResource.width ?? null,
801
+ image: parseImage(rawResource.image, options),
802
+ coordinates: parseCoordinates(rawResource.coordinates, options)
803
+ };
804
+ }
805
+ function parseTextItemLink(rawText, options) {
806
+ return {
807
+ ...parseBaseItemLink("text", rawText, options),
808
+ type: rawText.type ?? null,
809
+ text: rawText.text ?? null,
810
+ language: rawText.language ?? null,
811
+ image: parseImage(rawText.image, options),
812
+ coordinates: parseCoordinates(rawText.coordinates, options)
813
+ };
814
+ }
815
+ function parseDictionaryUnitItemLink(rawDictionaryUnit, options) {
816
+ return parseBaseItemLink("dictionaryUnit", rawDictionaryUnit, options);
817
+ }
818
+ function parseLinks(rawLinks, options) {
819
+ const links = [];
820
+ if (rawLinks == null) return links;
821
+ const hierarchy = rawLinks;
822
+ for (const entry of collectHierarchyEntries(hierarchy)) switch (entry.category) {
823
+ case "tree":
824
+ links.push(parseTreeItemLink(entry.item, options));
825
+ break;
826
+ case "bibliography":
827
+ links.push(parseBibliographyItemLink(entry.item, options));
828
+ break;
829
+ case "concept":
830
+ links.push(parseConceptItemLink(entry.item, options));
831
+ break;
832
+ case "spatialUnit":
833
+ links.push(parseSpatialUnitItemLink(entry.item, options));
834
+ break;
835
+ case "period":
836
+ links.push(parsePeriodItemLink(entry.item, options));
837
+ break;
838
+ case "person":
839
+ links.push(parsePersonItemLink(entry.item, options));
840
+ break;
841
+ case "propertyVariable":
842
+ links.push(parsePropertyVariableItemLink(entry.item, options));
843
+ break;
844
+ case "propertyValue":
845
+ links.push(parsePropertyValueItemLink(entry.item, options));
846
+ break;
847
+ case "resource":
848
+ links.push(parseResourceItemLink(entry.item, options));
849
+ break;
850
+ case "text":
851
+ links.push(parseTextItemLink(entry.item, options));
852
+ break;
853
+ case "set":
854
+ links.push(parseSetItemLink(entry.item, options));
855
+ break;
856
+ case "dictionaryUnit":
857
+ links.push(parseDictionaryUnitItemLink(entry.item, options));
858
+ break;
859
+ case "heading": break;
860
+ }
861
+ return links;
862
+ }
863
+ function parseReverseLinks(rawLinks, options) {
864
+ const links = [];
865
+ const rawLinksToParse = rawLinks == null ? [] : Array.isArray(rawLinks) ? rawLinks : [rawLinks];
866
+ for (const rawLink of rawLinksToParse) links.push(...parseLinks(rawLink, options));
867
+ return links;
868
+ }
869
+ function parsePeriodList(rawPeriods, options) {
870
+ const periods = [];
871
+ for (const period of rawPeriods?.period ?? []) periods.push(parsePeriod(period, options));
872
+ return periods;
873
+ }
874
+ function parseBibliographyList(rawBibliographies, options) {
875
+ const bibliographies = [];
876
+ for (const bibliography of rawBibliographies?.bibliography ?? []) bibliographies.push(parseBibliography(bibliography, options));
877
+ return bibliographies;
878
+ }
879
+ function parsePersonList(rawPersons, options) {
880
+ const persons = [];
881
+ for (const person of rawPersons ?? []) persons.push(parsePerson(person, options));
882
+ return persons;
883
+ }
884
+ function parseInterpretation(rawInterpretation, options) {
885
+ return {
886
+ number: rawInterpretation.interpretationNo,
887
+ date: rawInterpretation.date ?? null,
888
+ observers: parsePersonList(rawInterpretation.observers?.observer, options),
889
+ periods: parsePeriodList(rawInterpretation.periods, options),
890
+ links: parseLinks(rawInterpretation.links, options),
891
+ notes: parseNotes(rawInterpretation.notes, options),
892
+ properties: parseProperties(rawInterpretation.properties, options),
893
+ bibliographies: parseBibliographyList(rawInterpretation.bibliographies, options)
894
+ };
895
+ }
896
+ function parseObservation(rawObservation, options) {
897
+ return {
898
+ number: rawObservation.observationNo,
899
+ date: rawObservation.date ?? null,
900
+ observers: parsePersonList(rawObservation.observers?.observer, options),
901
+ periods: parsePeriodList(rawObservation.periods, options),
902
+ links: parseLinks(rawObservation.links, options),
903
+ notes: parseNotes(rawObservation.notes, options),
904
+ properties: parseProperties(rawObservation.properties, options),
905
+ bibliographies: parseBibliographyList(rawObservation.bibliographies, options)
906
+ };
907
+ }
908
+ function parseSection(rawSection, options) {
909
+ return {
910
+ uuid: rawSection.uuid,
911
+ publicationDateTime: rawSection.publicationDateTime ?? null,
912
+ identification: parseIdentification(rawSection.identification, options),
913
+ project: rawSection.project == null ? null : { identification: parseIdentification(rawSection.project.identification, options) }
914
+ };
915
+ }
916
+ function parseSections(rawSections, options) {
917
+ const sections = [];
918
+ if (rawSections == null || !("translation" in rawSections) && !("phonemic" in rawSections)) return sections;
919
+ for (const translation of rawSections.translation ?? []) for (const section of translation.section) sections.push(parseSection(section, options));
920
+ for (const phonemic of rawSections.phonemic ?? []) for (const section of phonemic.section) sections.push(parseSection(section, options));
921
+ return sections;
922
+ }
923
+ function parseHeading(rawHeading, containedItemCategory, options) {
924
+ const headings = [];
925
+ for (const heading of rawHeading.heading ?? []) headings.push(parseHeading(heading, containedItemCategory, options));
926
+ return {
927
+ name: rawHeading.name,
928
+ headings,
929
+ items: parseItemHierarchy(rawHeading, options, [containedItemCategory])
930
+ };
931
+ }
932
+ function parseTree(rawTree, options) {
933
+ const childOptions = getParserOptions(options);
934
+ const containedItemCategory = resolveTreeItemCategory(rawTree, normalizeTreeItemCategory(options.containedItemCategory));
935
+ const items = [];
936
+ if (containedItemCategory != null) {
937
+ const itemCategories = [containedItemCategory];
938
+ if (isHeadingItemCategory(containedItemCategory)) itemCategories.push("heading");
939
+ for (const entry of collectHierarchyEntries(rawTree.items, itemCategories)) {
940
+ if (entry.category === "heading") {
941
+ if (!isHeadingItemCategory(containedItemCategory)) continue;
942
+ items.push(parseHeading(entry.item, containedItemCategory, childOptions));
943
+ continue;
944
+ }
945
+ const item = parseEmbeddedItemEntry(entry, childOptions);
946
+ if (item != null) items.push(item);
947
+ }
948
+ }
949
+ return {
950
+ ...parseBaseItem("tree", rawTree, childOptions),
951
+ type: rawTree.type ?? null,
952
+ containedItemCategory,
953
+ links: parseLinks(rawTree.links, childOptions),
954
+ notes: parseNotes(rawTree.notes, childOptions),
955
+ properties: parseProperties(rawTree.properties, childOptions),
956
+ bibliographies: parseBibliographyList(rawTree.bibliographies, childOptions),
957
+ items
958
+ };
959
+ }
960
+ function parseSet(rawSet, options) {
961
+ const childOptions = getParserOptions(options);
962
+ const containedItemCategories = normalizeSetItemCategories(options.containedItemCategory) ?? inferItemCategories(rawSet.items);
963
+ return {
964
+ ...parseBaseItem("set", rawSet, childOptions),
965
+ containedItemCategories,
966
+ isTabularStructure: rawSet.tabularStructure ?? false,
967
+ isSuppressingBlanks: rawSet.suppressBlanks ?? false,
968
+ links: parseLinks(rawSet.links, childOptions),
969
+ notes: parseNotes(rawSet.notes, childOptions),
970
+ properties: parseProperties(rawSet.properties, childOptions),
971
+ items: parseSetItemHierarchy(rawSet.items, childOptions, containedItemCategories)
972
+ };
973
+ }
974
+ function parseSetBibliography(rawBibliography, options) {
975
+ return withoutItems(withSetItemProperties(parseBibliography(rawBibliography, options), rawBibliography.properties, options));
976
+ }
977
+ function parseSetConcept(rawConcept, options) {
978
+ return {
979
+ ...parseBaseItem("concept", rawConcept, options),
980
+ image: parseImage(rawConcept.image, options),
981
+ coordinates: parseCoordinates(rawConcept.coordinates, options),
982
+ properties: parseSetItemProperties(rawConcept.properties, options)
983
+ };
984
+ }
985
+ function parseSpatialUnitMapData(mapData) {
986
+ if (mapData == null) return null;
987
+ return { geoJSON: {
988
+ multiPolygon: mapData.geoJSON.multiPolygon.payload,
989
+ EPSG: mapData.geoJSON.EPSG
990
+ } };
991
+ }
992
+ function parseSetSpatialUnit(rawSpatialUnit, options) {
993
+ return {
994
+ ...parseBaseItem("spatialUnit", rawSpatialUnit, options),
995
+ image: parseImage(rawSpatialUnit.image, options),
996
+ coordinates: parseCoordinates(rawSpatialUnit.coordinates, options),
997
+ mapData: parseSpatialUnitMapData(rawSpatialUnit.mapData),
998
+ properties: parseSetItemProperties(rawSpatialUnit.properties, options),
999
+ bibliographies: parseBibliographyList(rawSpatialUnit.bibliographies, options)
1000
+ };
1001
+ }
1002
+ function parseSetPeriod(rawPeriod, options) {
1003
+ return withoutItems(withSetItemProperties(parsePeriod(rawPeriod, options), rawPeriod.properties, options));
1004
+ }
1005
+ function parseSetResource(rawResource, options) {
1006
+ return withoutItems(withSetItemProperties(parseResource(rawResource, options), rawResource.properties, options));
1007
+ }
1008
+ function parseSetTree(rawTree, options) {
1009
+ return withoutItems(withSetItemProperties(parseTree(rawTree, options), rawTree.properties, options));
1010
+ }
1011
+ function parseSetSet(rawSet, options) {
1012
+ return withoutItems(withSetItemProperties(parseSet(rawSet, options), rawSet.properties, options));
1013
+ }
1014
+ function parseBibliography(rawBibliography, options) {
1015
+ const sourceItems = rawBibliography.source == null ? [] : parseLinks(rawBibliography.source, options);
1016
+ const bibliographies = parseBibliographyList(rawBibliography.bibliographies, options);
1017
+ const items = [];
1018
+ for (const bibliography of rawBibliography.bibliography ?? []) items.push(parseBibliography(bibliography, options));
1019
+ const baseBibliography = {
1020
+ ...parseBaseItem("bibliography", rawBibliography, options),
1021
+ citationDetails: rawBibliography.citationDetails ?? null,
1022
+ citationFormat: parseContentLike(rawBibliography.citationFormat, options),
1023
+ citationFormatSpan: parseStringLike(rawBibliography.citationFormatSpan),
1024
+ referenceFormatDiv: parseStringLike(rawBibliography.referenceFormatDiv),
1025
+ image: parseImage(rawBibliography.image, options),
1026
+ sourceDocument: parseBibliographySourceDocument(rawBibliography.sourceDocument),
1027
+ publicationInfo: rawBibliography.publicationInfo == null ? null : {
1028
+ publishers: parsePersonList(rawBibliography.publicationInfo.publishers == null ? void 0 : "publisher" in rawBibliography.publicationInfo.publishers ? rawBibliography.publicationInfo.publishers.publisher : rawBibliography.publicationInfo.publishers.publishers.person, options),
1029
+ startDate: parseBibliographyStartDate(rawBibliography.publicationInfo.startDate)
1030
+ },
1031
+ entryInfo: parseBibliographyEntryInfo(rawBibliography.entryInfo),
1032
+ source: sourceItems[0] == null ? null : sourceItems[0],
1033
+ authors: parsePersonList(rawBibliography.authors?.person, options),
1034
+ periods: parsePeriodList(rawBibliography.periods, options),
1035
+ links: parseLinks(rawBibliography.links, options),
1036
+ notes: parseNotes(rawBibliography.notes, options),
1037
+ properties: parseProperties(rawBibliography.properties, options),
1038
+ bibliographies,
1039
+ items
1040
+ };
1041
+ if (rawBibliography.type === "zotero" && rawBibliography.zoteroId != null) return {
1042
+ ...baseBibliography,
1043
+ type: "zotero",
1044
+ zoteroId: rawBibliography.zoteroId
1045
+ };
1046
+ return {
1047
+ ...baseBibliography,
1048
+ type: rawBibliography.type ?? null
1049
+ };
1050
+ }
1051
+ function parseConcept(rawConcept, options) {
1052
+ const interpretations = [];
1053
+ for (const interpretation of rawConcept.interpretations?.interpretation ?? []) interpretations.push(parseInterpretation(interpretation, options));
1054
+ const items = [];
1055
+ for (const concept of rawConcept.concept ?? []) items.push(parseConcept(concept, options));
1056
+ return {
1057
+ ...parseBaseItem("concept", rawConcept, options),
1058
+ image: parseImage(rawConcept.image, options),
1059
+ interpretations,
1060
+ coordinates: parseCoordinates(rawConcept.coordinates, options),
1061
+ items
1062
+ };
1063
+ }
1064
+ function parseSpatialUnit(rawSpatialUnit, options) {
1065
+ const observations = [];
1066
+ for (const observation of rawSpatialUnit.observations?.observation ?? []) observations.push(parseObservation(observation, options));
1067
+ const items = [];
1068
+ for (const spatialUnit of rawSpatialUnit.spatialUnit ?? []) items.push(parseSpatialUnit(spatialUnit, options));
1069
+ return {
1070
+ ...parseBaseItem("spatialUnit", rawSpatialUnit, options),
1071
+ image: parseImage(rawSpatialUnit.image, options),
1072
+ coordinates: parseCoordinates(rawSpatialUnit.coordinates, options),
1073
+ mapData: parseSpatialUnitMapData(rawSpatialUnit.mapData),
1074
+ observations,
1075
+ bibliographies: parseBibliographyList(rawSpatialUnit.bibliographies, options),
1076
+ items
1077
+ };
1078
+ }
1079
+ function parsePeriod(rawPeriod, options) {
1080
+ const items = [];
1081
+ for (const period of rawPeriod.period ?? []) items.push(parsePeriod(period, options));
1082
+ return {
1083
+ ...parseBaseItem("period", rawPeriod, options),
1084
+ type: rawPeriod.type ?? null,
1085
+ coordinates: parseCoordinates(rawPeriod.coordinates, options),
1086
+ links: parseLinks(rawPeriod.links, options),
1087
+ notes: parseNotes(rawPeriod.notes, options),
1088
+ properties: parseProperties(rawPeriod.properties, options),
1089
+ bibliographies: parseBibliographyList(rawPeriod.bibliographies, options),
1090
+ items
1091
+ };
1092
+ }
1093
+ function parsePerson(rawPerson, options) {
1094
+ return {
1095
+ ...parseBaseItem("person", rawPerson, options),
1096
+ type: rawPerson.type ?? "",
1097
+ image: parseImage(rawPerson.image, options),
1098
+ address: rawPerson.address == null ? null : {
1099
+ country: parseStringLike(rawPerson.address.country),
1100
+ city: parseStringLike(rawPerson.address.city),
1101
+ state: parseStringLike(rawPerson.address.state),
1102
+ postalCode: parseStringLike(rawPerson.address.postalCode)
1103
+ },
1104
+ coordinates: parseCoordinates(rawPerson.coordinates, options),
1105
+ content: rawPerson.content == null ? null : parseRequiredContentLike(rawPerson, options),
1106
+ periods: parsePeriodList(rawPerson.periods, options),
1107
+ links: parseLinks(rawPerson.links, options),
1108
+ notes: parseNotes(rawPerson.notes, options),
1109
+ properties: parseProperties(rawPerson.properties, options)
1110
+ };
1111
+ }
1112
+ function parsePropertyVariable(rawPropertyVariable, options) {
1113
+ return {
1114
+ ...parseBaseItem("propertyVariable", rawPropertyVariable, options),
1115
+ type: rawPropertyVariable.type ?? null,
1116
+ coordinates: parseCoordinates(rawPropertyVariable.coordinates, options),
1117
+ links: parseLinks(rawPropertyVariable.links, options),
1118
+ notes: parseNotes(rawPropertyVariable.notes, options),
1119
+ bibliographies: parseBibliographyList(rawPropertyVariable.bibliographies, options)
1120
+ };
1121
+ }
1122
+ function parsePropertyValue(rawPropertyValue, options) {
1123
+ return {
1124
+ ...parseBaseItem("propertyValue", rawPropertyValue, options),
1125
+ coordinates: parseCoordinates(rawPropertyValue.coordinates, options),
1126
+ links: parseLinks(rawPropertyValue.links, options),
1127
+ notes: parseNotes(rawPropertyValue.notes, options),
1128
+ properties: parseProperties(rawPropertyValue.properties, options),
1129
+ bibliographies: parseBibliographyList(rawPropertyValue.bibliographies, options)
1130
+ };
1131
+ }
1132
+ function parseResource(rawResource, options) {
1133
+ const items = [];
1134
+ for (const resource of rawResource.resource ?? []) items.push(parseResource(resource, options));
1135
+ return {
1136
+ ...parseBaseItem("resource", rawResource, options),
1137
+ type: rawResource.type ?? "",
1138
+ href: parseHref(rawResource.href),
1139
+ fileFormat: rawResource.fileFormat ?? null,
1140
+ fileSize: rawResource.fileSize ?? null,
1141
+ isInline: rawResource.rend === "inline",
1142
+ height: rawResource.height ?? null,
1143
+ width: rawResource.width ?? null,
1144
+ image: parseImage(rawResource.image, options),
1145
+ document: parseContentLike(rawResource.document, options),
1146
+ imageMap: parseImageMap(rawResource.imagemap),
1147
+ coordinates: parseCoordinates(rawResource.coordinates, options),
1148
+ periods: parsePeriodList(rawResource.periods, options),
1149
+ links: parseLinks(rawResource.links, options),
1150
+ reverseLinks: parseReverseLinks(rawResource.reverseLinks, options),
1151
+ notes: parseNotes(rawResource.notes, options),
1152
+ properties: parseProperties(rawResource.properties, options),
1153
+ bibliographies: parseBibliographyList(rawResource.bibliographies, options),
1154
+ items
1155
+ };
1156
+ }
1157
+ function parseText(rawText, options) {
1158
+ const editions = [];
1159
+ for (const edition of rawText.editions?.edition ?? []) editions.push(parsePerson(edition, options));
1160
+ for (const editor of rawText.editions?.editor ?? []) editions.push(parsePerson(editor, options));
1161
+ for (const publisher of rawText.editions?.publisher ?? []) editions.push(parsePerson(publisher, options));
1162
+ return {
1163
+ ...parseBaseItem("text", rawText, options),
1164
+ type: rawText.type ?? "",
1165
+ text: rawText.text ?? null,
1166
+ language: rawText.language ?? null,
1167
+ image: parseImage(rawText.image, options),
1168
+ coordinates: parseCoordinates(rawText.coordinates, options),
1169
+ links: parseLinks(rawText.links, options),
1170
+ reverseLinks: parseReverseLinks(rawText.reverseLinks, options),
1171
+ notes: parseNotes(rawText.notes, options),
1172
+ sections: parseSections(rawText.sections, options),
1173
+ periods: parsePeriodList(rawText.periods, options),
1174
+ creators: parsePersonList(rawText.creators?.creator, options),
1175
+ editions
1176
+ };
1177
+ }
1178
+ function parseMetadataLanguages(rawOchre) {
1179
+ const languages = [];
1180
+ for (const language of rawOchre.metadata.language ?? []) {
1181
+ const parsedLanguage = parseStringLike(language);
1182
+ if (parsedLanguage != null) languages.push(parsedLanguage);
1183
+ }
1184
+ if (languages.length > 0) return languages;
1185
+ if (rawOchre.languages != null) {
1186
+ for (const language of rawOchre.languages.split(";")) if (language !== "") languages.push(language);
1187
+ }
1188
+ return languages.length > 0 ? languages : [...DEFAULT_LANGUAGES];
1189
+ }
1190
+ function resolveLanguages(requestedLanguages, metadataLanguages) {
1191
+ if (requestedLanguages.length === 0) return metadataLanguages;
1192
+ const unsupportedLanguages = [];
1193
+ for (const requestedLanguage of requestedLanguages) if (!metadataLanguages.some((metadataLanguage) => metadataLanguage.toLocaleLowerCase("en-US") === requestedLanguage.toLocaleLowerCase("en-US"))) unsupportedLanguages.push(requestedLanguage);
1194
+ if (unsupportedLanguages.length > 0) throw new Error(`The following language(s) are not supported by the dataset: ${unsupportedLanguages.toSorted((a, b) => a.localeCompare(b, "en-US")).join(", ")}. Available languages: ${metadataLanguages.toSorted((a, b) => a.localeCompare(b, "en-US")).join(", ")}`, { cause: unsupportedLanguages });
1195
+ return requestedLanguages;
1196
+ }
1197
+ function resolveDefaultLanguage(rawOchre, languages) {
1198
+ for (const language of rawOchre.metadata.language ?? []) {
1199
+ const parsedLanguage = parseStringLike(language);
1200
+ if (parsedLanguage != null && language.default === "true" && languages.includes(parsedLanguage)) return parsedLanguage;
1201
+ }
1202
+ for (const language of languages) if (language === DEFAULT_LANGUAGES[0]) return language;
1203
+ const firstLanguage = languages[0];
1204
+ if (firstLanguage == null) throw new Error("Default language not found", { cause: languages });
1205
+ return firstLanguage;
1206
+ }
1207
+ function parseMetadataPublisher(rawPublisher) {
1208
+ return parseStringLike(Array.isArray(rawPublisher) ? rawPublisher[0] : rawPublisher) ?? "";
1209
+ }
1210
+ function parseMetadata(rawOchre, options, defaultLanguage) {
1211
+ const metadataOptions = options;
1212
+ const rawMetadata = rawOchre.metadata;
1213
+ return {
1214
+ dataset: parseStringLike(rawMetadata.dataset) ?? "",
1215
+ description: parseStringLike(rawMetadata.description) ?? "",
1216
+ publisher: parseMetadataPublisher(rawMetadata.publisher),
1217
+ identifier: transformPermanentIdentificationUrl(parseStringLike(rawMetadata.identifier) ?? ""),
1218
+ project: rawMetadata.project == null ? null : {
1219
+ uuid: rawMetadata.project.uuid ?? rawOchre.uuidBelongsTo,
1220
+ identification: parseIdentification(rawMetadata.project.identification, metadataOptions),
1221
+ website: parseStringLike(rawMetadata.project.identification.website),
1222
+ dateFormat: rawMetadata.project.dateFormat ?? null,
1223
+ page: rawMetadata.project.page ?? null
1224
+ },
1225
+ collection: rawMetadata.collection == null ? null : {
1226
+ uuid: rawMetadata.collection.uuid,
1227
+ identification: parseIdentification(rawMetadata.collection.identification, metadataOptions),
1228
+ page: rawMetadata.collection.page
1229
+ },
1230
+ publication: rawMetadata.publication == null ? null : {
1231
+ uuid: rawMetadata.publication.uuid,
1232
+ identification: parseIdentification(rawMetadata.publication.identification, metadataOptions),
1233
+ page: rawMetadata.publication.page
1234
+ },
1235
+ item: rawMetadata.item == null ? null : {
1236
+ identification: parseIdentification(rawMetadata.item.identification, metadataOptions),
1237
+ category: normalizeCategory(rawMetadata.item.category) ?? rawMetadata.item.category,
1238
+ type: rawMetadata.item.type,
1239
+ maxLength: rawMetadata.item.maxLength ?? null
1240
+ },
1241
+ defaultLanguage,
1242
+ languages: options.languages
1243
+ };
1244
+ }
1245
+ function inferTopLevelCategory(rawOchre) {
1246
+ for (const category of SET_ITEM_CATEGORIES) if (category in rawOchre) return category;
1247
+ if ("variable" in rawOchre) return "propertyVariable";
1248
+ if ("value" in rawOchre) return "propertyValue";
1249
+ throw new Error("Could not infer OCHRE item category", { cause: rawOchre });
1250
+ }
1251
+ function getSingleTopLevelRawItem(items, category) {
1252
+ if (items == null || items.length === 0) throw new Error(`${category} not found`, { cause: items });
1253
+ if (items.length > 1) throw new Error(`Expected one ${category}, received ${items.length}`, { cause: items });
1254
+ return items[0];
1255
+ }
1256
+ function parseTopLevelItem(rawOchre, category, options) {
1257
+ switch (category) {
1258
+ case "tree": return parseTree(getSingleTopLevelRawItem("tree" in rawOchre ? rawOchre.tree : null, "tree"), {
1259
+ ...options,
1260
+ containedItemCategory: normalizeTreeItemCategory(options.containedItemCategory)
1261
+ });
1262
+ case "bibliography": return parseBibliography(getSingleTopLevelRawItem("bibliography" in rawOchre ? rawOchre.bibliography : null, "bibliography"), options);
1263
+ case "concept": return parseConcept(getSingleTopLevelRawItem("concept" in rawOchre ? rawOchre.concept : null, "concept"), options);
1264
+ case "spatialUnit": return parseSpatialUnit(getSingleTopLevelRawItem("spatialUnit" in rawOchre ? rawOchre.spatialUnit : null, "spatial unit"), options);
1265
+ case "period": return parsePeriod(getSingleTopLevelRawItem("period" in rawOchre ? rawOchre.period : null, "period"), options);
1266
+ case "person": return parsePerson(getSingleTopLevelRawItem("person" in rawOchre ? rawOchre.person : null, "person"), options);
1267
+ case "propertyVariable": return parsePropertyVariable(getSingleTopLevelRawItem("propertyVariable" in rawOchre ? rawOchre.propertyVariable : "variable" in rawOchre ? rawOchre.variable : null, "property variable"), options);
1268
+ case "propertyValue": return parsePropertyValue(getSingleTopLevelRawItem("propertyValue" in rawOchre ? rawOchre.propertyValue : "value" in rawOchre ? rawOchre.value : null, "property value"), options);
1269
+ case "resource": return parseResource(getSingleTopLevelRawItem("resource" in rawOchre ? rawOchre.resource : null, "resource"), options);
1270
+ case "text": return parseText(getSingleTopLevelRawItem("text" in rawOchre ? rawOchre.text : null, "text"), options);
1271
+ case "set": return parseSet(getSingleTopLevelRawItem("set" in rawOchre ? rawOchre.set : null, "set"), options);
1272
+ }
1273
+ }
1274
+ function parseLinkedItems(rawItems, options) {
1275
+ const parserOptions = {
1276
+ languages: options.languages,
1277
+ containedItemCategory: options.containedItemCategory
1278
+ };
1279
+ const items = [];
1280
+ for (const entry of collectHierarchyEntries(rawItems)) {
1281
+ const item = parseEmbeddedItemEntry(entry, parserOptions);
1282
+ if (item != null) items.push(item);
1283
+ }
1284
+ return items;
1285
+ }
1286
+ function parseSetItems(rawItems, options) {
1287
+ return parseSetItemHierarchy(rawItems, { languages: options.languages }, normalizeSetItemCategories(options.containedItemCategories) ?? void 0);
1288
+ }
1289
+ function parseGallery(rawData, options) {
1290
+ const gallery = rawData.result.ochre.gallery;
1291
+ const resources = [];
1292
+ for (const resource of gallery.resource ?? []) resources.push(parseResource(resource, options));
1293
+ return {
1294
+ identification: parseIdentification(gallery.item.identification, options),
1295
+ projectIdentification: parseIdentification(gallery.project.identification, options),
1296
+ resources,
1297
+ maxLength: gallery.maxLength
1298
+ };
1299
+ }
1300
+ function parseItem(rawData, options) {
1301
+ const rawOchre = rawData.result.ochre;
1302
+ const metadataLanguages = parseMetadataLanguages(rawOchre);
1303
+ const languagesToUse = resolveLanguages(options.languages, metadataLanguages);
1304
+ const parserOptions = {
1305
+ languages: languagesToUse,
1306
+ containedItemCategory: options.containedItemCategory
1307
+ };
1308
+ const category = options.category ?? normalizeCategory(rawOchre.metadata.item?.category) ?? inferTopLevelCategory(rawOchre);
1309
+ const defaultLanguage = resolveDefaultLanguage(rawOchre, languagesToUse);
1310
+ const belongsTo = {
1311
+ uuid: rawOchre.uuidBelongsTo,
1312
+ abbreviation: rawOchre.belongsTo
1313
+ };
1314
+ const metadata = parseMetadata(rawOchre, parserOptions, defaultLanguage);
1315
+ const persistentUrl = parseHref(rawOchre.persistentUrl);
1316
+ const item = parseTopLevelItem(rawOchre, category, parserOptions);
1317
+ if (category === "resource") {
1318
+ const rawResource = getSingleTopLevelRawItem("resource" in rawOchre ? rawOchre.resource : null, "resource");
1319
+ return {
1320
+ ...item,
1321
+ belongsTo,
1322
+ metadata,
1323
+ persistentUrl,
1324
+ view: options.parseResourceView?.(rawResource.view, {
1325
+ belongsTo,
1326
+ metadata
1327
+ }) ?? null
1328
+ };
1329
+ }
1330
+ return {
1331
+ ...item,
1332
+ belongsTo,
1333
+ metadata,
1334
+ persistentUrl
1335
+ };
1336
+ }
1337
+ //#endregion
1338
+ export { getParserOptions, parseBibliographyList, parseGallery, parseIdentification, parseItem, parseLinkedItems, parseLinks, parseMetadata, parseMetadataLanguages, parseNotes, parsePersonList, parseProperties, parseSetItems, parseSimplifiedProperties, parseStringLike, resolveDefaultLanguage, resolveLanguages };