1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
use crate::{add_value, compact_iri, compact_property, MetaError, Options};
use contextual::WithContext;
use json_ld_context_processing::{
	Options as ProcessingOptions, Process, ProcessMeta, ProcessingMode,
};
use json_ld_core::{
	object::node::TypeEntry, Container, ContainerKind, Context, ContextLoader, Id, Loader, Node,
	Term, Type,
};
use json_ld_syntax::{Entry, Keyword};
use locspan::{Meta, Stripped};
use mown::Mown;
use rdf_types::VocabularyMut;
use std::hash::Hash;

fn optional_string<M: Clone>(s: Option<Meta<String, M>>, meta: &M) -> json_syntax::MetaValue<M> {
	s.map(Meta::cast)
		.unwrap_or_else(|| Meta(json_syntax::Value::Null, meta.clone()))
}

/// Compact the given indexed node.
pub async fn compact_indexed_node_with<I, B, M, C, N, L>(
	vocabulary: &mut N,
	Meta(node, meta): Meta<&Node<I, B, M>, &M>,
	index: Option<&Entry<String, M>>,
	mut active_context: &Context<I, B, C, M>,
	type_scoped_context: &Context<I, B, C, M>,
	active_property: Option<Meta<&str, &M>>,
	loader: &mut L,
	options: Options,
) -> Result<json_syntax::MetaValue<M>, MetaError<M, L::ContextError>>
where
	N: Send + Sync + VocabularyMut<Iri = I, BlankId = B>,
	I: Clone + Hash + Eq + Send + Sync,
	B: Clone + Hash + Eq + Send + Sync,
	M: Clone + Send + Sync,
	C: ProcessMeta<I, B, M>,
	L: Loader<I, M> + ContextLoader<I, M> + Send + Sync,
	L::Context: Into<C>,
{
	// If active context has a previous context, the active context is not propagated.
	// If element does not contain an @value entry, and element does not consist of
	// a single @id entry, set active context to previous context from active context,
	// as the scope of a term-scoped context does not apply when processing new node objects.
	if !(node.is_empty() && node.id().is_some()) {
		// does not consist of a single @id entry
		if let Some(previous_context) = active_context.previous_context() {
			active_context = previous_context
		}
	}

	// If the term definition for active property in active context has a local context:
	// FIXME https://github.com/w3c/json-ld-api/issues/502
	//       Seems that the term definition should be looked up in `type_scoped_context`.
	let mut active_context = Mown::Borrowed(active_context);
	if let Some(active_property) = active_property {
		if let Some(active_property_definition) = type_scoped_context.get(*active_property) {
			if let Some(local_context) = active_property_definition.context() {
				active_context = Mown::Owned(
					local_context
						.process_with(
							vocabulary,
							active_context.as_ref(),
							loader,
							active_property_definition.base_url().cloned(),
							ProcessingOptions::from(options).with_override(),
						)
						.await
						.map_err(Meta::cast)?
						.into_processed(),
				)
			}
		}
	}

	// let inside_reverse = active_property == Some("@reverse");
	let mut result = json_syntax::Object::default();

	if !node.types().is_empty() {
		// If element has an @type entry, create a new array compacted types initialized by
		// transforming each expanded type of that entry into its compacted form by IRI
		// compacting expanded type. Then, for each term in compacted types ordered
		// lexicographically:
		let mut compacted_types = Vec::new();
		for ty in node.types() {
			let compacted_ty = compact_iri(
				vocabulary,
				type_scoped_context,
				ty.clone().map(Id::into_term).borrow(),
				true,
				false,
				options,
			)
			.map_err(Meta::cast)?;
			compacted_types.push(compacted_ty)
		}

		compacted_types.sort_by(|a, b| a.as_ref().unwrap().cmp(b.as_ref().unwrap()));

		for term in &compacted_types {
			if let Some(term_definition) = type_scoped_context.get(term.as_ref().unwrap().as_str())
			{
				if let Some(local_context) = term_definition.context() {
					let processing_options = ProcessingOptions::from(options).without_propagation();
					active_context = Mown::Owned(
						local_context
							.process_with(
								vocabulary,
								active_context.as_ref(),
								loader,
								term_definition.base_url().cloned(),
								processing_options,
							)
							.await
							.map_err(Meta::cast)?
							.into_processed(),
					)
				}
			}
		}
	}

	// For each key expanded property and value expanded value in element, ordered
	// lexicographically by expanded property if ordered is true:
	let mut expanded_entries: Vec<_> = node.properties().iter().collect();
	if options.ordered {
		let vocabulary: &N = vocabulary;
		expanded_entries.sort_by(|(a, _), (b, _)| {
			(**a)
				.with(vocabulary)
				.as_str()
				.cmp((**b).with(vocabulary).as_str())
		})
	}

	// If expanded property is @id:
	if let Some(id_entry) = node.id_entry() {
		let id = id_entry.value.clone().map(Id::into_term);

		if node.is_empty() {
			// This captures step 7:
			// If element has an @value or @id entry and the result of using the
			// Value Compaction algorithm, passing active context, active property,
			// and element as value is a scalar, or the term definition for active property
			// has a type mapping of @json, return that result.
			//
			// in the Value Compaction Algorithm, step 7:
			// If value has an @id entry and has no other entries other than @index:
			//
			// If the type mapping of active property is set to @id,
			// set result to the result of IRI compacting the value associated with the
			// @id entry using false for vocab.
			let type_mapping = match active_property {
				Some(prop) => match active_context.get(*prop) {
					Some(def) => def.typ(),
					None => None,
				},
				None => None,
			};

			if type_mapping == Some(&Type::Id) {
				let compacted_value = compact_iri(
					vocabulary,
					active_context.as_ref(),
					id.borrow(),
					false,
					false,
					options,
				)
				.map_err(Meta::cast)?;
				return Ok(optional_string(compacted_value, id.metadata()));
			}

			// Otherwise, if the type mapping of active property is set to @vocab,
			// set result to the result of IRI compacting the value associated with the @id entry.
			if type_mapping == Some(&Type::Vocab) {
				let compacted_value = compact_iri(
					vocabulary,
					active_context.as_ref(),
					id.borrow(),
					true,
					false,
					options,
				)
				.map_err(Meta::cast)?;
				return Ok(optional_string(compacted_value, id.metadata()));
			}
		}

		// If expanded value is a string, then initialize compacted value by IRI
		// compacting expanded value with vocab set to false.
		let compacted_value = compact_iri(
			vocabulary,
			active_context.as_ref(),
			id.borrow(),
			false,
			false,
			options,
		)
		.map_err(Meta::cast)?;

		// Initialize alias by IRI compacting expanded property.
		let alias = compact_iri(
			vocabulary,
			active_context.as_ref(),
			Meta(&Term::Keyword(Keyword::Id), &id_entry.key_metadata),
			true,
			false,
			options,
		)
		.map_err(Meta::cast)?;

		// Add an entry alias to result whose value is set to compacted value and continue
		// to the next expanded property.
		if let Some(key) = alias {
			result.insert(key.cast(), optional_string(compacted_value, id.metadata()));
		}
	}

	compact_types(
		vocabulary,
		&mut result,
		node.type_entry(),
		active_context.as_ref(),
		type_scoped_context,
		options,
	)?;

	// If expanded property is @reverse:
	if let Some(reverse_properties) = node.reverse_properties_entry() {
		if !reverse_properties.is_empty() {
			// Initialize compacted value to the result of using this algorithm recursively,
			// passing active context, @reverse for active property,
			// expanded value for element, and the compactArrays and ordered flags.
			let active_property = "@reverse";
			if let Some(active_property_definition) = active_context.get(active_property) {
				if let Some(local_context) = active_property_definition.context() {
					active_context = Mown::Owned(
						local_context
							.value
							.process_with(
								vocabulary,
								active_context.as_ref(),
								loader,
								active_property_definition.base_url().cloned(),
								ProcessingOptions::from(options).with_override(),
							)
							.await
							.map_err(Meta::cast)?
							.into_processed(),
					)
				}
			}

			let mut reverse_result = json_syntax::Object::default();
			for (expanded_property, expanded_value) in reverse_properties.iter() {
				compact_property(
					vocabulary,
					&mut reverse_result,
					expanded_property.cloned().cast(),
					expanded_value.iter().map(Stripped::as_ref),
					active_context.as_ref(),
					loader,
					true,
					options,
				)
				.await?;
			}

			// For each property and value in compacted value:
			let mut reverse_map = json_syntax::Object::default();
			for (property, mapped_value) in reverse_result.iter_mut() {
				let mut value = Meta(json_syntax::Value::Null, mapped_value.metadata().clone());
				std::mem::swap(&mut value, &mut *mapped_value);

				// If the term definition for property in the active context indicates that
				// property is a reverse property
				if let Some(term_definition) = active_context.get(property.as_str()) {
					if term_definition.reverse_property() {
						// Initialize as array to true if the container mapping for property in
						// the active context includes @set, otherwise the negation of compactArrays.
						let as_array = term_definition.container().contains(ContainerKind::Set)
							|| !options.compact_arrays;

						// Use add value to add value to the property entry in result using as array.
						add_value(
							&mut result,
							property.borrow().map(AsRef::as_ref),
							value,
							as_array,
						);
						continue;
					}
				}

				reverse_map.insert(property.clone().cast(), value);
			}

			if !reverse_map.is_empty() {
				// Initialize alias by IRI compacting @reverse.
				let alias = compact_iri(
					vocabulary,
					active_context.as_ref(),
					Meta(
						&Term::Keyword(Keyword::Reverse),
						&reverse_properties.key_metadata,
					),
					true,
					false,
					options,
				)
				.map_err(Meta::cast)?;

				// Set the value of the alias entry of result to compacted value.
				result.insert(
					alias.unwrap().cast(),
					Meta(reverse_map.into(), reverse_properties.metadata().clone()),
				);
			}
		}
	}

	// If expanded property is @index and active property has a container mapping in
	// active context that includes @index,
	if let Some(index_entry) = index {
		let mut index_container = false;
		if let Some(active_property) = active_property {
			if let Some(active_property_definition) = active_context.get(*active_property) {
				if active_property_definition
					.container()
					.contains(ContainerKind::Index)
				{
					// then the compacted result will be inside of an @index container,
					// drop the @index entry by continuing to the next expanded property.
					index_container = true;
				}
			}
		}

		if !index_container {
			// Initialize alias by IRI compacting expanded property.
			let alias = compact_iri(
				vocabulary,
				active_context.as_ref(),
				Meta(&Term::Keyword(Keyword::Index), &index_entry.key_metadata),
				true,
				false,
				options,
			)
			.map_err(Meta::cast)?;

			// Add an entry alias to result whose value is set to expanded value and continue with the next expanded property.
			result.insert(alias.unwrap().cast(), index_entry.value.clone().cast());
		}
	}

	if let Some(graph_entry) = node.graph_entry() {
		compact_property(
			vocabulary,
			&mut result,
			Meta(
				Term::Keyword(Keyword::Graph),
				graph_entry.key_metadata.clone(),
			),
			graph_entry.value.iter().map(Stripped::as_ref),
			active_context.as_ref(),
			loader,
			false,
			options,
		)
		.await?
	}

	for (expanded_property, expanded_value) in expanded_entries {
		compact_property(
			vocabulary,
			&mut result,
			expanded_property.cloned().cast(),
			expanded_value.iter().map(Stripped::as_ref),
			active_context.as_ref(),
			loader,
			false,
			options,
		)
		.await?
	}

	if let Some(included_entry) = node.included_entry() {
		compact_property(
			vocabulary,
			&mut result,
			Meta(
				Term::Keyword(Keyword::Included),
				included_entry.key_metadata.clone(),
			),
			included_entry.value.iter().map(Stripped::as_ref),
			active_context.as_ref(),
			loader,
			false,
			options,
		)
		.await?
	}

	Ok(Meta(result.into(), meta.clone()))
}

/// Compact the given list of types into the given `result` compacted object.
fn compact_types<I, B, M, C, N, E>(
	vocabulary: &mut N,
	result: &mut json_syntax::Object<M>,
	type_entry: Option<&TypeEntry<I, B, M>>,
	active_context: &Context<I, B, C, M>,
	type_scoped_context: &Context<I, B, C, M>,
	options: Options,
) -> Result<(), MetaError<M, E>>
where
	N: VocabularyMut<Iri = I, BlankId = B>,
	I: Clone + Hash + Eq,
	B: Clone + Hash + Eq,
	M: Clone,
{
	// If expanded property is @type:
	if let Some(type_entry) = type_entry {
		let types = &type_entry.value;
		if !types.is_empty() {
			// If expanded value is a string,
			// then initialize compacted value by IRI compacting expanded value using
			// type-scoped context for active context.
			let compacted_value = if types.len() == 1 {
				optional_string(
					compact_iri(
						vocabulary,
						type_scoped_context,
						types[0].clone().map(Id::into_term).borrow(),
						true,
						false,
						options,
					)
					.map_err(Meta::cast)?,
					types[0].metadata(),
				)
			} else {
				// Otherwise, expanded value must be a @type array:
				// Initialize compacted value to an empty array.
				let mut compacted_value = Vec::with_capacity(types.len());

				// For each item expanded type in expanded value:
				for ty in types.iter() {
					let ty = ty.clone().map(Id::into_term);

					// Set term by IRI compacting expanded type using type-scoped context for active context.
					let compacted_ty = compact_iri(
						vocabulary,
						type_scoped_context,
						ty.borrow(),
						true,
						false,
						options,
					)
					.map_err(Meta::cast)?;

					// Append term, to compacted value.
					compacted_value.push(optional_string(compacted_ty, ty.metadata()))
				}

				Meta(
					json_syntax::Value::Array(compacted_value.into_iter().collect()),
					types.metadata().clone(),
				)
			};

			// Initialize alias by IRI compacting expanded property.
			let alias = compact_iri(
				vocabulary,
				active_context,
				Meta(&Term::Keyword(Keyword::Type), &type_entry.key_metadata),
				true,
				false,
				options,
			)
			.map_err(Meta::cast)?
			.unwrap();

			// Initialize as array to true if processing mode is json-ld-1.1 and the
			// container mapping for alias in the active context includes @set,
			// otherwise to the negation of compactArrays.
			let container_mapping = match active_context.get(alias.as_str()) {
				Some(def) => def.container(),
				None => Container::None,
			};
			let as_array = (options.processing_mode == ProcessingMode::JsonLd1_1
				&& container_mapping.contains(ContainerKind::Set))
				|| !options.compact_arrays;

			// Use add value to add compacted value to the alias entry in result using as array.
			add_value(
				result,
				alias.borrow().map(String::as_str),
				compacted_value,
				as_array,
			)
		}
	}

	Ok(())
}