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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//! API implementation for the DSNP Graph SDK using Neon
//! This crate provides a bridge between the DSNP graph sdk and Node.js.
//! It is intended to be used as a dependency in the `@projectlibertylabs/graph-sdk` npm package.
use crate::helper::*;
use dsnp_graph_config::{Config, ConnectionType, DsnpUserId, GraphKeyType, PrivacyType};
use dsnp_graph_core::{
	api::{
		api::{GraphAPI, GraphState},
		api_types::{Action, ActionOptions, DsnpKeys, ImportBundle},
	},
	dsnp::dsnp_types::DsnpPublicKey,
	util::transactional_hashmap::Transactional,
};
use neon::prelude::*;
use once_cell::sync::Lazy;
use std::{
	collections::HashMap,
	sync::{Arc, Mutex},
};

/// Global counter for graph state ids
static NEXT_GRAPH_STATE_ID: Lazy<Mutex<usize>> = Lazy::new(|| Mutex::new(0));

/// Collection of GraphStates
#[allow(clippy::vec_box)]
static GRAPH_STATES: Lazy<Mutex<HashMap<usize, Arc<Mutex<GraphState>>>>> =
	Lazy::new(|| Mutex::new(HashMap::new()));

/// Neon implementation of print_hello_graph function
pub fn print_hello_graph(mut cx: FunctionContext) -> JsResult<JsString> {
	println!("Hello, Graph!");
	Ok(cx.string("Hello, Graph!"))
}

/// Get graph config from the environment
/// # Arguments
/// * `cx` - Neon FunctionContext
/// * `env` - Neon Environment object
/// # Returns
/// * `JsResult<JsObject>` - Neon JsObject containing the graph config
pub fn get_graph_config(mut cx: FunctionContext) -> JsResult<JsObject> {
	let environment_obj = cx.argument::<JsObject>(0)?;

	let environment = unsafe { environment_from_js(&mut cx, environment_obj) }?;
	let config: &Config = environment.get_config();

	// Convert Config to JsObject
	let config_js = config_to_js(&mut cx, config)?;

	Ok(config_js)
}

/// Function to get SchemaId for given ConnectionType and PrivacyType
/// # Arguments
/// * `cx` - Neon FunctionContext
/// * `connection_type` - ConnectionType string
/// * `privacy_type` - PrivacyType string
/// # Returns
/// * `JsResult<JsNumber>` - Neon JsNumber containing the SchemaId
/// # Errors
/// * Throws a Neon error if the SchemaId cannot be found
pub fn get_schema_id_from_config(mut cx: FunctionContext) -> JsResult<JsNumber> {
	let environment_obj = cx.argument::<JsObject>(0)?;
	let environment = unsafe { environment_from_js(&mut cx, environment_obj) }?;

	let connection_type = cx.argument::<JsString>(1)?;
	let connection_type = connection_type.value(&mut cx);

	let privacy_type = cx.argument::<JsString>(2)?;
	let privacy_type = privacy_type.value(&mut cx);

	let privacy_type: PrivacyType = match privacy_type.as_str() {
		"public" => PrivacyType::Public,
		"private" => PrivacyType::Private,
		_ => return cx.throw_error("Invalid privacy type"),
	};

	let connection_type = match connection_type.as_str() {
		"follow" => ConnectionType::Follow(privacy_type),
		"friendship" => ConnectionType::Friendship(privacy_type),
		_ => return cx.throw_error("Invalid connection type"),
	};

	let config: &Config = environment.get_config();
	let schema_id = config.get_schema_id_from_connection_type(connection_type);
	if schema_id.is_none() {
		return cx.throw_error("SchemaId not found");
	}
	let schema_id = schema_id.unwrap();

	Ok(cx.number(schema_id as f64))
}

/// Create a new graph state
/// # Arguments
/// * `cx` - Neon FunctionContext
/// * `env` - Neon Environment object extracted from context
/// # Returns
/// * `JsResult<JsObject>` - Neon JsObject containing the graph state
/// # Errors
/// * Throws a Neon error if the graph state cannot be created
/// # Safety
pub fn initialize_graph_state(mut cx: FunctionContext) -> JsResult<JsNumber> {
	let environment_obj = cx.argument::<JsObject>(0)?;
	let rust_environment = unsafe { environment_from_js(&mut cx, environment_obj) }?;
	let graph_state = GraphState::new(rust_environment);

	// Generate a unique identifier for the graph state
	let graph_state_id = {
		let mut next_id = NEXT_GRAPH_STATE_ID.lock().unwrap();
		let id = *next_id;
		*next_id = next_id.wrapping_add(1);
		id
	};
	{
		let mut states = GRAPH_STATES.lock().unwrap();
		states.insert(graph_state_id, Arc::new(Mutex::new(graph_state)));
	}

	Ok(cx.number(graph_state_id as f64))
}

/// Get total count of graph states
/// # Arguments
/// * `cx` - Neon FunctionContext
/// # Returns
/// * `JsResult<JsNumber>` - Neon JsNumber containing the total count of graph states
/// # Errors
/// * Throws a Neon error
pub fn get_graph_states_count(mut cx: FunctionContext) -> JsResult<JsNumber> {
	let states = GRAPH_STATES.lock().unwrap();
	let states_count = states.len();

	Ok(cx.number(states_count as f64))
}

/// Get graph users count for given graph state
/// # Arguments
/// * `cx` - Neon FunctionContext
/// * `graph_state_id` - Unique identifier for the graph state
/// # Returns
/// * `JsResult<JsNumber>` - Neon JsNumber containing the graph users count
/// # Errors
/// * Throws a Neon error
pub fn get_graph_users_count(mut cx: FunctionContext) -> JsResult<JsNumber> {
	let graph_state_id = cx.argument::<JsNumber>(0)?;
	let graph_state_id = graph_state_id.value(&mut cx) as usize;

	let states = GRAPH_STATES.lock().unwrap();
	let graph_state = states.get(&graph_state_id);
	if graph_state.is_none() {
		return cx.throw_error("Graph state not found");
	}
	let graph_state = graph_state.unwrap();
	let graph_state = graph_state.lock().unwrap();
	let users_count = graph_state.len();

	Ok(cx.number(users_count as f64))
}

/// Check if graph contains user
/// # Arguments
/// * `cx` - Neon FunctionContext
/// * `graph_state_id` - Unique identifier for the graph state
/// * `dsnpt_user_id` - DSNP user id
/// # Returns
/// * `JsResult<JsBoolean>` - Neon JsBoolean
/// # Errors
/// * Throws a Neon error
pub fn contains_user_graph(mut cx: FunctionContext) -> JsResult<JsBoolean> {
	let graph_state_id = cx.argument::<JsNumber>(0)?;
	let graph_state_id = graph_state_id.value(&mut cx) as usize;
	let dsnp_user_id = cx.argument::<JsString>(1)?;
	let dsnp_user_id = match dsnp_user_id.value(&mut cx).parse::<DsnpUserId>() {
		Ok(id) => id,
		Err(_) => return cx.throw_error("Invalid DSNP user id"),
	};

	let states = GRAPH_STATES.lock().unwrap();
	let graph_state = states.get(&graph_state_id);
	if graph_state.is_none() {
		return cx.throw_error("Graph state not found");
	}
	let graph_state = graph_state.unwrap();
	let graph_state = graph_state.lock().unwrap();
	let contains_user = graph_state.contains_user_graph(&dsnp_user_id);

	Ok(cx.boolean(contains_user))
}

/// Function to remove user graph from the graph state
/// # Arguments
/// * `cx` - Neon FunctionContext
/// * `graph_state_id` - Unique identifier for the graph state
/// * `dsnp_user_id` - DSNP user id
/// # Returns
/// * `JsResult<JsBoolean>` - Neon JsBoolean
/// # Errors
/// * Throws a Neon error
pub fn remove_user_graph(mut cx: FunctionContext) -> JsResult<JsBoolean> {
	let graph_state_id = cx.argument::<JsNumber>(0)?;
	let graph_state_id = graph_state_id.value(&mut cx) as usize;
	let dsnp_user_id = cx.argument::<JsString>(1)?;
	let dsnp_user_id = match dsnp_user_id.value(&mut cx).parse::<DsnpUserId>() {
		Ok(id) => id,
		Err(_) => return cx.throw_error("Invalid DSNP user id"),
	};

	let mut states = GRAPH_STATES.lock().unwrap();
	let graph_state = states.get_mut(&graph_state_id);
	if graph_state.is_none() {
		return cx.throw_error("Graph state not found");
	}
	let graph_state = graph_state.unwrap();
	let mut graph_state = graph_state.lock().unwrap();
	graph_state.remove_user_graph(&dsnp_user_id);

	Ok(cx.boolean(true))
}

/// Function to import user data
/// # Arguments
/// * `cx` - Neon FunctionContext
/// * `graph_state_id` - Unique identifier for the graph state
/// * `payload` - JSON object for `ImportBundle`
/// # Returns
/// * `JsResult<JsBoolean>` - Neon JsBoolean
/// # Errors
/// * Throws a Neon error
pub fn import_user_data(mut cx: FunctionContext) -> JsResult<JsBoolean> {
	let graph_state_id = cx.argument::<JsNumber>(0)?;
	let graph_state_id = graph_state_id.value(&mut cx) as usize;
	let payload = cx.argument::<JsArray>(1)?;
	let rust_payload: Vec<ImportBundle> = import_bundle_from_js(&mut cx, payload)?;

	let mut states = GRAPH_STATES.lock().unwrap();
	let graph_state = states.get_mut(&graph_state_id);
	if graph_state.is_none() {
		return cx.throw_error("Graph state not found");
	}
	let graph_state = graph_state.unwrap();
	let mut graph_state = graph_state.lock().unwrap();

	let import_result = graph_state.import_users_data(&rust_payload);
	match import_result {
		Ok(_) => Ok(cx.boolean(true)),
		Err(e) => cx.throw_error(e.to_string()),
	}
}

/// Function to export graph updates
/// # Arguments
/// * `cx` - Neon FunctionContext
/// * `graph_state_id` - Unique identifier for the graph state
/// # Returns
/// * `JsResult<JsArray>` - Neon JsArray containing the exported updates
/// # Errors
/// * Throws a Neon error
pub fn export_graph_updates(mut cx: FunctionContext) -> JsResult<JsArray> {
	let graph_state_id = cx.argument::<JsNumber>(0)?;
	let graph_state_id = graph_state_id.value(&mut cx) as usize;

	let mut states = GRAPH_STATES.lock().unwrap();
	let graph_state = states.get_mut(&graph_state_id);
	if graph_state.is_none() {
		return cx.throw_error("Graph state not found");
	}
	let graph_state = graph_state.unwrap();
	let graph_state = graph_state.lock().unwrap();

	let updates = graph_state.export_updates();
	match updates {
		Ok(updates) => {
			let updates_js = updates_to_js(&mut cx, updates)?;
			Ok(updates_js)
		},
		Err(e) => cx.throw_error(e.to_string()),
	}
}

/// Function to export graph updates for a single user graph
/// # Arguments
/// * `cx` - Neon FunctionContext
/// * `graph_state_id` - Unique identifier for the graph state
/// # Returns
/// * `JsResult<JsArray>` - Neon JsArray containing the exported updates
/// # Errors
/// * Throws a Neon error
pub fn export_user_graph_updates(mut cx: FunctionContext) -> JsResult<JsArray> {
	let graph_state_id = cx.argument::<JsNumber>(0)?;
	let graph_state_id = graph_state_id.value(&mut cx) as usize;
	let dsnp_user_id: Handle<'_, JsString> = cx.argument::<JsString>(1)?;
	let dsnp_user_id = match dsnp_user_id.value(&mut cx).parse::<DsnpUserId>() {
		Ok(id) => id,
		Err(_) => return cx.throw_error("Invalid DSNP user id"),
	};

	let mut states = GRAPH_STATES.lock().unwrap();
	let graph_state = states.get_mut(&graph_state_id);
	if graph_state.is_none() {
		return cx.throw_error("Graph state not found");
	}
	let graph_state = graph_state.unwrap();
	let graph_state = graph_state.lock().unwrap();

	let updates = graph_state.export_user_graph_updates(&dsnp_user_id);
	match updates {
		Ok(updates) => {
			let updates_js = updates_to_js(&mut cx, updates)?;
			Ok(updates_js)
		},
		Err(e) => cx.throw_error(e.to_string()),
	}
}

/// Function to get connections for user from the graph state (getConnectionsForUserGraph)
/// # Arguments
/// * `cx` - Neon FunctionContext
/// * `graph_state_id` - Unique identifier for the graph state
/// * `dsnp_user_id` - DSNP user id
/// # Returns
/// * `JsResult<JsArray>` - Neon JsArray containing the connections which is list of DSNPGraphEdge
/// # Errors
/// * Throws a Neon error
pub fn get_connections_for_user_graph(mut cx: FunctionContext) -> JsResult<JsArray> {
	let graph_state_id = cx.argument::<JsNumber>(0)?;
	let graph_state_id = graph_state_id.value(&mut cx) as usize;
	let dsnp_user_id: Handle<'_, JsString> = cx.argument::<JsString>(1)?;
	let dsnp_user_id = match dsnp_user_id.value(&mut cx).parse::<DsnpUserId>() {
		Ok(id) => id,
		Err(_) => return cx.throw_error("Invalid DSNP user id"),
	};

	let schema_id = cx.argument::<JsNumber>(2)?;
	let schema_id = schema_id.value(&mut cx) as u16;
	let include_pending = cx.argument::<JsBoolean>(3)?;
	let include_pending = include_pending.value(&mut cx);
	let mut states = GRAPH_STATES.lock().unwrap();
	let graph_state = states.get_mut(&graph_state_id);
	if graph_state.is_none() {
		return cx.throw_error("Graph state not found");
	}
	let graph_state = graph_state.unwrap();
	let graph_state = graph_state.lock().unwrap();

	let connections =
		graph_state.get_connections_for_user_graph(&dsnp_user_id, &schema_id, include_pending);
	match connections {
		Ok(connections) => {
			let connections_js = connections_to_js(&mut cx, connections)?;
			Ok(connections_js)
		},
		Err(e) => cx.throw_error(e.to_string()),
	}
}

/// Function to applyActions to the graph state
/// # Arguments
/// * `cx` - Neon FunctionContext
/// * `graph_state_id` - Unique identifier for the graph state
/// * `actions` - JSArray containing the actions to apply
/// # Returns
/// * `JsResult<JsBoolean>` - Neon JsBoolean
/// # Errors
/// * Throws a Neon error
pub fn apply_actions(mut cx: FunctionContext) -> JsResult<JsBoolean> {
	let graph_state_id: Handle<'_, JsNumber> = cx.argument::<JsNumber>(0)?;
	let graph_state_id = graph_state_id.value(&mut cx) as usize;
	let actions: Handle<'_, JsArray> = cx.argument::<JsArray>(1)?;
	let rust_actions: Vec<Action> = actions_from_js(&mut cx, actions)?;
	let mut rust_options: Option<ActionOptions> = None;
	match cx.argument_opt(2) {
		Some(opt_value) => {
			let options: Handle<'_, JsObject> = opt_value.downcast_or_throw(&mut cx)?;
			rust_options = Some(action_options_from_js(&mut cx, options)?);
		},
		None => (),
	};

	let mut states = GRAPH_STATES.lock().unwrap();
	let graph_state = states.get_mut(&graph_state_id);
	if graph_state.is_none() {
		return cx.throw_error("Graph state not found");
	}
	let graph_state = graph_state.unwrap();
	let mut graph_state = graph_state.lock().unwrap();

	let apply_result = graph_state.apply_actions(&rust_actions, &rust_options);
	match apply_result {
		Ok(_) => Ok(cx.boolean(true)),
		Err(e) => cx.throw_error(e.to_string()),
	}
}

/// Function to commit pending changes to a graph state
/// # Arguments
/// * `cx` - Neon FunctionContext
/// `graph_state_id` - Unique identifier for the graph state
/// Returns
/// `JsResult<()>` - Empty result status
/// Errors
/// Does not throw
pub fn commit(mut cx: FunctionContext) -> JsResult<JsBoolean> {
	let graph_state_id: Handle<'_, JsNumber> = cx.argument::<JsNumber>(0)?;
	let graph_state_id = graph_state_id.value(&mut cx) as usize;

	let mut states = GRAPH_STATES.lock().unwrap();
	let graph_state = states.get_mut(&graph_state_id);
	if graph_state.is_none() {
		return cx.throw_error("Graph state not found");
	}
	let graph_state = graph_state.unwrap();
	let mut graph_state = graph_state.lock().unwrap();

	graph_state.commit();

	JsResult::Ok(cx.boolean(true))
}

/// Function to rollback pending changes in a graph state
/// # Arguments
/// * `cx` - Neon FunctionContext
/// `graph_state_id` - Unique identifier for the graph state
/// Returns
/// `JsResult<()>` - Empty result status
/// Errors
/// Does not throw
pub fn rollback(mut cx: FunctionContext) -> JsResult<JsBoolean> {
	let graph_state_id: Handle<'_, JsNumber> = cx.argument::<JsNumber>(0)?;
	let graph_state_id = graph_state_id.value(&mut cx) as usize;

	let mut states = GRAPH_STATES.lock().unwrap();
	let graph_state = states.get_mut(&graph_state_id);
	if graph_state.is_none() {
		return cx.throw_error("Graph state not found");
	}
	let graph_state = graph_state.unwrap();
	let mut graph_state = graph_state.lock().unwrap();

	graph_state.rollback();

	JsResult::Ok(cx.boolean(true))
}

/// Function to force calculate graphs for user
/// # Arguments
/// * `cx` - Neon FunctionContext
/// * `graph_state_id` - Unique identifier for the graph state
/// * `dsnp_user_id` - DSNP user id
/// # Returns
/// * `JsResult<JsObject>` - Neon JsObject containing the exported updates
/// # Errors
/// * Throws a Neon error
pub fn force_calculate_graphs(mut cx: FunctionContext) -> JsResult<JsArray> {
	let graph_state_id = cx.argument::<JsNumber>(0)?;
	let graph_state_id = graph_state_id.value(&mut cx) as usize;
	let dsnp_user_id = cx.argument::<JsString>(1)?;
	let dsnp_user_id = match dsnp_user_id.value(&mut cx).parse::<DsnpUserId>() {
		Ok(id) => id,
		Err(_) => return cx.throw_error("Invalid DSNP user id"),
	};

	let mut states = GRAPH_STATES.lock().unwrap();
	let graph_state = states.get_mut(&graph_state_id);
	if graph_state.is_none() {
		return cx.throw_error("Graph state not found");
	}
	let graph_state = graph_state.unwrap();
	let graph_state = graph_state.lock().unwrap();

	let update = graph_state.force_recalculate_graphs(&dsnp_user_id);
	match update {
		Ok(update) => {
			let update_js = updates_to_js(&mut cx, update)?;
			Ok(update_js)
		},
		Err(e) => cx.throw_error(e.to_string()),
	}
}

/// Function to get connections for user from the graph state (getConnectionsWithoutKeys)
/// # Arguments
/// * `cx` - Neon FunctionContext
/// * `graph_state_id` - Unique identifier for the graph state
/// # Returns
/// * `JsResult<JsArray>` - Neon JsArray containing the connections which is list of DSNPGraphEdge
/// # Errors
/// * Throws a Neon error
pub fn get_connections_without_keys(mut cx: FunctionContext) -> JsResult<JsArray> {
	let graph_state_id = cx.argument::<JsNumber>(0)?;
	let graph_state_id = graph_state_id.value(&mut cx) as usize;

	let mut states = GRAPH_STATES.lock().unwrap();
	let graph_state = states.get_mut(&graph_state_id);
	if graph_state.is_none() {
		return cx.throw_error("Graph state not found");
	}
	let graph_state = graph_state.unwrap();
	let graph_state = graph_state.lock().unwrap();

	let connections = graph_state.get_connections_without_keys();
	match connections {
		Ok(connections) => {
			let connections_js = cx.empty_array();
			for (i, connection) in connections.iter().enumerate() {
				let connection_js_string: Handle<'_, JsString> = cx.string(connection.to_string());
				connections_js.set(&mut cx, i as u32, connection_js_string)?;
			}
			Ok(connections_js)
		},
		Err(e) => cx.throw_error(e.to_string()),
	}
}

/// Function to get one sided private friendship connections for user from the graph state (getOneSidedPrivateFriendshipConnections)
/// # Arguments
/// * `cx` - Neon FunctionContext
/// * `graph_state_id` - Unique identifier for the graph state
/// * `dsnp_user_id` - DSNP user id
/// # Returns
/// * `JsResult<JsArray>` - Neon JsArray containing the connections which is list of DSNPGraphEdge
/// # Errors
/// * Throws a Neon error
pub fn get_one_sided_private_friendship_connections(mut cx: FunctionContext) -> JsResult<JsArray> {
	let graph_state_id = cx.argument::<JsNumber>(0)?;
	let graph_state_id = graph_state_id.value(&mut cx) as usize;
	let dsnp_user_id = cx.argument::<JsString>(1)?;
	let dsnp_user_id = match dsnp_user_id.value(&mut cx).parse::<DsnpUserId>() {
		Ok(id) => id,
		Err(_) => return cx.throw_error("Invalid DSNP user id"),
	};

	let mut states = GRAPH_STATES.lock().unwrap();
	let graph_state = states.get_mut(&graph_state_id);
	if graph_state.is_none() {
		return cx.throw_error("Graph state not found");
	}
	let graph_state = graph_state.unwrap();
	let graph_state = graph_state.lock().unwrap();

	let connections = graph_state.get_one_sided_private_friendship_connections(&dsnp_user_id);
	match connections {
		Ok(connections) => {
			let connections_js = connections_to_js(&mut cx, connections)?;
			Ok(connections_js)
		},
		Err(e) => cx.throw_error(e.to_string()),
	}
}

/// Function to get public keys for user from the graph state (getPublicKeys)
/// # Arguments
/// * `cx` - Neon FunctionContext
/// * `graph_state_id` - Unique identifier for the graph state
/// * `dsnp_user_id` - DSNP user id
/// # Returns
/// * `JsResult<JsArray>` - Neon JsArray containing the public keys which is list of DSNPGraphEdge
/// # Errors
/// * Throws a Neon error
pub fn get_public_keys(mut cx: FunctionContext) -> JsResult<JsArray> {
	let graph_state_id = cx.argument::<JsNumber>(0)?;
	let graph_state_id = graph_state_id.value(&mut cx) as usize;
	let dsnp_user_id = cx.argument::<JsString>(1)?;
	let dsnp_user_id = match dsnp_user_id.value(&mut cx).parse::<DsnpUserId>() {
		Ok(id) => id,
		Err(_) => return cx.throw_error("Invalid DSNP user id"),
	};

	let mut states = GRAPH_STATES.lock().unwrap();
	let graph_state = states.get_mut(&graph_state_id);
	if graph_state.is_none() {
		return cx.throw_error("Graph state not found");
	}
	let graph_state = graph_state.unwrap();
	let graph_state = graph_state.lock().unwrap();

	let public_keys = graph_state.get_public_keys(&dsnp_user_id);
	match public_keys {
		Ok(keys) => {
			let public_keys_js = public_keys_to_js(&mut cx, keys)?;
			Ok(public_keys_js)
		},
		Err(e) => cx.throw_error(e.to_string()),
	}
}

/// Function to deserialize DSNP keys
/// # Arguments
/// * `cx` - Neon FunctionContext
/// * `keys` - DSNP keys
/// # Returns
/// * `JsResult<JsArray>` - Neon JsArray containing the public keys which is list of DSNPGraphEdge
/// # Errors
/// * Throws a Neon error
pub fn deserialize_dsnp_keys(mut cx: FunctionContext) -> JsResult<JsArray> {
	let keys: Handle<'_, JsObject> = cx.argument::<JsObject>(0)?;
	let rust_keys: DsnpKeys = dsnp_keys_from_js(&mut cx, keys)?;
	let deserialized_keys: Vec<DsnpPublicKey> =
		GraphState::deserialize_dsnp_keys(&Some(rust_keys)).unwrap_or_default();
	let keys_js = public_keys_to_js(&mut cx, deserialized_keys)?;

	Ok(keys_js)
}

/// Function to generate X25519 keys and return GraphKeyPair type JsObject
/// # Arguments
/// * `cx` - Neon FunctionContext
/// * `key_type` - GraphKeyType enum
/// # Returns
/// * `JsResult<JsObject>` - Neon JsObject containing the generated keys
/// # Errors
/// * Throws a Neon error
pub fn generate_keypair(mut cx: FunctionContext) -> JsResult<JsObject> {
	let key_type = cx.argument::<JsNumber>(0)?;
	let key_type = key_type.value(&mut cx);
	let keypair = match key_type as u8 {
		0 => GraphState::generate_keypair(GraphKeyType::X25519),
		_ => return cx.throw_error("Unsupported key type"),
	};
	let keypair_js = keypair_to_js(&mut cx, &keypair.unwrap())?;

	Ok(keypair_js)
}

/// Function to free the graph state
/// # Arguments
/// * `cx` - Neon FunctionContext
/// * `graph_state_id` - Unique identifier for the graph state
/// # Returns
/// * `JsResult<JsBoolean>` - Neon JsBoolean
/// # Errors
/// * Throws a Neon error
pub fn free_graph_state(mut cx: FunctionContext) -> JsResult<JsBoolean> {
	let graph_state_id = cx.argument::<JsNumber>(0)?;
	let graph_state_id = graph_state_id.value(&mut cx) as usize;

	let mut states = GRAPH_STATES.lock().unwrap();
	let graph_state = states.remove(&graph_state_id);
	if graph_state.is_none() {
		return cx.throw_error("Graph state not found");
	}
	let graph_state = graph_state.unwrap();
	let graph_state = graph_state.lock().unwrap();
	drop(graph_state);

	Ok(cx.boolean(true))
}

#[neon::main]
fn main(mut cx: ModuleContext) -> NeonResult<()> {
	cx.export_function("printHelloGraph", print_hello_graph)?;
	cx.export_function("getGraphConfig", get_graph_config)?;
	cx.export_function("getSchemaIdFromConfig", get_schema_id_from_config)?;
	cx.export_function("initializeGraphState", initialize_graph_state)?;
	cx.export_function("getGraphStatesCount", get_graph_states_count)?;
	cx.export_function("getGraphUsersCount", get_graph_users_count)?;
	cx.export_function("containsUserGraph", contains_user_graph)?;
	cx.export_function("removeUserGraph", remove_user_graph)?;
	cx.export_function("importUserData", import_user_data)?;
	cx.export_function("exportUpdates", export_graph_updates)?;
	cx.export_function("exportUserGraphUpdates", export_user_graph_updates)?;
	cx.export_function("getConnectionsForUserGraph", get_connections_for_user_graph)?;
	cx.export_function("applyActions", apply_actions)?;
	cx.export_function("commit", commit)?;
	cx.export_function("rollback", rollback)?;
	cx.export_function("forceCalculateGraphs", force_calculate_graphs)?;
	cx.export_function("getConnectionsWithoutKeys", get_connections_without_keys)?;
	cx.export_function(
		"getOneSidedPrivateFriendshipConnections",
		get_one_sided_private_friendship_connections,
	)?;
	cx.export_function("getPublicKeys", get_public_keys)?;
	cx.export_function("deserializeDsnpKeys", deserialize_dsnp_keys)?;
	cx.export_function("generateKeyPair", generate_keypair)?;
	cx.export_function("freeGraphState", free_graph_state)?;
	Ok(())
}