trustformers-wasm 0.2.0

WebAssembly bindings for TrustformeRS transformer library
Documentation
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
//! Geographic Distribution System for Edge Computing
//!
//! This module provides intelligent geographic distribution of AI inference
//! requests across global edge locations to minimize latency and optimize
//! performance for users worldwide.

use crate::runtime::edge_runtime::{EdgeCapabilities, EdgeRuntime};
use std::collections::BTreeMap;
use std::format;
use std::string::String;
use std::vec::Vec;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::JsFuture;
use web_sys::js_sys;

/// Geographic regions for edge distribution
#[wasm_bindgen]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum GeoRegion {
    /// North America
    NorthAmerica,
    /// Europe
    Europe,
    /// Asia Pacific
    AsiaPacific,
    /// South America
    SouthAmerica,
    /// Africa
    Africa,
    /// Middle East
    MiddleEast,
    /// Oceania
    Oceania,
}

/// Edge location information
#[wasm_bindgen]
#[derive(Debug, Clone)]
pub struct EdgeLocation {
    region: GeoRegion,
    country_code: String,
    city: String,
    datacenter_id: String,
    latitude: f64,
    longitude: f64,
    runtime_type: EdgeRuntime,
    capabilities: EdgeCapabilities,
    current_load: f32,
    health_score: f32,
    last_health_check: u64,
}

#[wasm_bindgen]
impl EdgeLocation {
    /// Create a new edge location
    #[wasm_bindgen(constructor)]
    #[allow(clippy::too_many_arguments)] // reason: distinct configuration parameters; a struct would not simplify the wasm-bindgen API
    pub fn new(
        region: GeoRegion,
        country_code: String,
        city: String,
        datacenter_id: String,
        latitude: f64,
        longitude: f64,
        runtime_type: EdgeRuntime,
        capabilities: EdgeCapabilities,
    ) -> EdgeLocation {
        EdgeLocation {
            region,
            country_code,
            city,
            datacenter_id,
            latitude,
            longitude,
            runtime_type,
            capabilities,
            current_load: 0.0,
            health_score: 1.0,
            last_health_check: 0,
        }
    }

    #[wasm_bindgen(getter)]
    pub fn region(&self) -> GeoRegion {
        self.region
    }

    #[wasm_bindgen(getter)]
    pub fn country_code(&self) -> String {
        self.country_code.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn city(&self) -> String {
        self.city.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn datacenter_id(&self) -> String {
        self.datacenter_id.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn latitude(&self) -> f64 {
        self.latitude
    }

    #[wasm_bindgen(getter)]
    pub fn longitude(&self) -> f64 {
        self.longitude
    }

    #[wasm_bindgen(getter)]
    pub fn runtime_type(&self) -> EdgeRuntime {
        self.runtime_type
    }

    #[wasm_bindgen(getter)]
    pub fn capabilities(&self) -> EdgeCapabilities {
        self.capabilities.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn current_load(&self) -> f32 {
        self.current_load
    }

    #[wasm_bindgen(getter)]
    pub fn health_score(&self) -> f32 {
        self.health_score
    }

    /// Calculate distance to another location in kilometers
    pub fn distance_to(&self, latitude: f64, longitude: f64) -> f64 {
        let r = 6371.0; // Earth's radius in kilometers
        let lat1 = self.latitude.to_radians();
        let lat2 = latitude.to_radians();
        let delta_lat = (latitude - self.latitude).to_radians();
        let delta_lon = (longitude - self.longitude).to_radians();

        let a = (delta_lat / 2.0).sin().powi(2)
            + lat1.cos() * lat2.cos() * (delta_lon / 2.0).sin().powi(2);
        let c = 2.0 * a.sqrt().atan2((1.0 - a).sqrt());

        r * c
    }

    /// Calculate routing score based on distance, load, and health
    pub fn routing_score(
        &self,
        user_lat: f64,
        user_lon: f64,
        weight_distance: f32,
        weight_load: f32,
        weight_health: f32,
    ) -> f32 {
        let distance = self.distance_to(user_lat, user_lon) as f32;
        let normalized_distance = (distance / 20000.0).min(1.0); // Normalize to 0-1 (max ~20000km)
        let load_factor = 1.0 - self.current_load; // Lower load is better
        let health_factor = self.health_score;

        // Lower score is better for routing
        weight_distance * normalized_distance
            + weight_load * (1.0 - load_factor)
            + weight_health * (1.0 - health_factor)
    }

    /// Update current load and health metrics
    pub fn update_metrics(&mut self, load: f32, health: f32) {
        self.current_load = load.clamp(0.0, 1.0);
        self.health_score = health.clamp(0.0, 1.0);
        self.last_health_check = js_sys::Date::now() as u64;
    }
}

/// User location information
#[wasm_bindgen]
#[derive(Debug, Clone)]
pub struct UserLocation {
    latitude: f64,
    longitude: f64,
    country_code: String,
    region: GeoRegion,
    timezone: String,
    accuracy: f64,
    ip_address: String,
    autonomous_system: String,
}

#[wasm_bindgen]
impl UserLocation {
    #[wasm_bindgen(constructor)]
    #[allow(clippy::too_many_arguments)] // reason: distinct configuration parameters; a struct would not simplify the wasm-bindgen API
    pub fn new(
        latitude: f64,
        longitude: f64,
        country_code: String,
        region: GeoRegion,
        timezone: String,
        accuracy: f64,
        ip_address: String,
        autonomous_system: String,
    ) -> UserLocation {
        UserLocation {
            latitude,
            longitude,
            country_code,
            region,
            timezone,
            accuracy,
            ip_address,
            autonomous_system,
        }
    }

    #[wasm_bindgen(getter)]
    pub fn latitude(&self) -> f64 {
        self.latitude
    }

    #[wasm_bindgen(getter)]
    pub fn longitude(&self) -> f64 {
        self.longitude
    }

    #[wasm_bindgen(getter)]
    pub fn country_code(&self) -> String {
        self.country_code.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn region(&self) -> GeoRegion {
        self.region
    }

    #[wasm_bindgen(getter)]
    pub fn timezone(&self) -> String {
        self.timezone.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn accuracy(&self) -> f64 {
        self.accuracy
    }

    #[wasm_bindgen(getter)]
    pub fn ip_address(&self) -> String {
        self.ip_address.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn autonomous_system(&self) -> String {
        self.autonomous_system.clone()
    }
}

/// Routing decision result
#[wasm_bindgen]
#[derive(Debug, Clone)]
pub struct RoutingDecision {
    primary_location: EdgeLocation,
    fallback_locations: Vec<EdgeLocation>,
    routing_reason: String,
    estimated_latency_ms: u32,
    confidence_score: f32,
}

#[wasm_bindgen]
impl RoutingDecision {
    #[wasm_bindgen(getter)]
    pub fn primary_location(&self) -> EdgeLocation {
        self.primary_location.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn fallback_locations(&self) -> js_sys::Array {
        let array = js_sys::Array::new();
        for location in &self.fallback_locations {
            array.push(&JsValue::from(location.clone()));
        }
        array
    }

    #[wasm_bindgen(getter)]
    pub fn routing_reason(&self) -> String {
        self.routing_reason.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn estimated_latency_ms(&self) -> u32 {
        self.estimated_latency_ms
    }

    #[wasm_bindgen(getter)]
    pub fn confidence_score(&self) -> f32 {
        self.confidence_score
    }
}

/// Geographic distribution manager
#[wasm_bindgen]
pub struct GeoDistributionManager {
    edge_locations: Vec<EdgeLocation>,
    routing_weights: RoutingWeights,
    health_check_interval: u64,
    last_health_check: u64,
    region_preferences: BTreeMap<String, GeoRegion>,
    failover_enabled: bool,
    load_balancing_enabled: bool,
}

/// Routing weights for decision making
#[wasm_bindgen]
#[derive(Debug, Clone)]
pub struct RoutingWeights {
    distance: f32,
    load: f32,
    health: f32,
    region_preference: f32,
    runtime_compatibility: f32,
}

#[wasm_bindgen]
impl RoutingWeights {
    #[wasm_bindgen(constructor)]
    pub fn new() -> RoutingWeights {
        RoutingWeights {
            distance: 0.4,
            load: 0.3,
            health: 0.2,
            region_preference: 0.1,
            runtime_compatibility: 0.1,
        }
    }

    /// Create optimized weights for low latency
    pub fn for_low_latency() -> RoutingWeights {
        RoutingWeights {
            distance: 0.6,
            load: 0.2,
            health: 0.15,
            region_preference: 0.05,
            runtime_compatibility: 0.05,
        }
    }

    /// Create optimized weights for high reliability
    pub fn for_high_reliability() -> RoutingWeights {
        RoutingWeights {
            distance: 0.2,
            load: 0.3,
            health: 0.4,
            region_preference: 0.05,
            runtime_compatibility: 0.1,
        }
    }

    /// Create optimized weights for load balancing
    pub fn for_load_balancing() -> RoutingWeights {
        RoutingWeights {
            distance: 0.3,
            load: 0.4,
            health: 0.2,
            region_preference: 0.05,
            runtime_compatibility: 0.1,
        }
    }

    #[wasm_bindgen(getter)]
    pub fn distance(&self) -> f32 {
        self.distance
    }

    #[wasm_bindgen(setter)]
    pub fn set_distance(&mut self, value: f32) {
        self.distance = value.clamp(0.0, 1.0);
    }

    #[wasm_bindgen(getter)]
    pub fn load(&self) -> f32 {
        self.load
    }

    #[wasm_bindgen(setter)]
    pub fn set_load(&mut self, value: f32) {
        self.load = value.clamp(0.0, 1.0);
    }

    #[wasm_bindgen(getter)]
    pub fn health(&self) -> f32 {
        self.health
    }

    #[wasm_bindgen(setter)]
    pub fn set_health(&mut self, value: f32) {
        self.health = value.clamp(0.0, 1.0);
    }
}

impl Default for RoutingWeights {
    fn default() -> Self {
        Self::new()
    }
}

#[wasm_bindgen]
impl GeoDistributionManager {
    /// Create a new geographic distribution manager
    #[wasm_bindgen(constructor)]
    pub fn new() -> GeoDistributionManager {
        let mut manager = GeoDistributionManager {
            edge_locations: Vec::new(),
            routing_weights: RoutingWeights::new(),
            health_check_interval: 30000, // 30 seconds
            last_health_check: 0,
            region_preferences: BTreeMap::new(),
            failover_enabled: true,
            load_balancing_enabled: true,
        };

        // Initialize with default edge locations
        manager.initialize_default_locations();

        manager
    }

    /// Add an edge location to the distribution network
    pub fn add_edge_location(&mut self, location: EdgeLocation) {
        self.edge_locations.push(location);
    }

    /// Remove an edge location from the distribution network
    pub fn remove_edge_location(&mut self, datacenter_id: &str) -> bool {
        if let Some(pos) =
            self.edge_locations.iter().position(|loc| loc.datacenter_id == datacenter_id)
        {
            self.edge_locations.remove(pos);
            true
        } else {
            false
        }
    }

    /// Get all edge locations
    pub fn get_edge_locations(&self) -> js_sys::Array {
        let array = js_sys::Array::new();
        for location in &self.edge_locations {
            array.push(&JsValue::from(location.clone()));
        }
        array
    }

    /// Set routing weights for optimization strategy
    pub fn set_routing_weights(&mut self, weights: RoutingWeights) {
        self.routing_weights = weights;
    }

    /// Get current routing weights
    #[wasm_bindgen(getter)]
    pub fn routing_weights(&self) -> RoutingWeights {
        self.routing_weights.clone()
    }

    /// Enable or disable automatic failover
    pub fn set_failover_enabled(&mut self, enabled: bool) {
        self.failover_enabled = enabled;
    }

    /// Enable or disable load balancing
    pub fn set_load_balancing_enabled(&mut self, enabled: bool) {
        self.load_balancing_enabled = enabled;
    }

    /// Set region preference for a specific country
    pub fn set_region_preference(&mut self, country_code: &str, region: GeoRegion) {
        self.region_preferences.insert(country_code.to_string(), region);
    }

    /// Detect user location using various methods
    pub async fn detect_user_location(&self) -> Result<UserLocation, JsValue> {
        // Try multiple location detection methods

        // Method 1: Try geolocation API
        if let Ok(location) = self.detect_location_geolocation().await {
            return Ok(location);
        }

        // Method 2: Try IP geolocation
        if let Ok(location) = self.detect_location_ip().await {
            return Ok(location);
        }

        // Method 3: Try timezone-based detection
        if let Ok(location) = self.detect_location_timezone().await {
            return Ok(location);
        }

        // Fallback: Use default location (US East Coast)
        Ok(UserLocation::new(
            40.7128,
            -74.0060, // New York City
            "US".to_string(),
            GeoRegion::NorthAmerica,
            "America/New_York".to_string(),
            1000.0, // Low accuracy
            "unknown".to_string(),
            "unknown".to_string(),
        ))
    }

    /// Find optimal edge location for a user
    pub fn find_optimal_edge_location(
        &self,
        user_location: &UserLocation,
    ) -> Result<RoutingDecision, JsValue> {
        if self.edge_locations.is_empty() {
            return Err(JsValue::from_str("No edge locations available"));
        }

        // Calculate scores for all locations
        let mut location_scores: Vec<(usize, f32)> = self
            .edge_locations
            .iter()
            .enumerate()
            .map(|(index, location)| {
                let score = self.calculate_routing_score(location, user_location);
                (index, score)
            })
            .collect();

        // Sort by score (lower is better)
        location_scores.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));

        // Filter healthy locations
        let healthy_locations: Vec<_> = location_scores
            .iter()
            .filter(|(index, _)| self.edge_locations[*index].health_score > 0.5)
            .collect();

        if healthy_locations.is_empty() {
            return Err(JsValue::from_str("No healthy edge locations available"));
        }

        // Select primary location
        let primary_index = healthy_locations[0].0;
        let primary_location = self.edge_locations[primary_index].clone();

        // Select fallback locations
        let fallback_locations: Vec<EdgeLocation> = healthy_locations
            .iter()
            .skip(1)
            .take(2) // Take top 2 fallback locations
            .map(|(index, _)| self.edge_locations[*index].clone())
            .collect();

        // Calculate estimated latency
        let distance =
            primary_location.distance_to(user_location.latitude, user_location.longitude);
        let estimated_latency_ms = self.estimate_latency_from_distance(distance);

        // Generate routing reason
        let routing_reason = format!(
            "Selected {} ({}) - Distance: {:.0}km, Load: {:.1}%, Health: {:.1}%",
            primary_location.city,
            primary_location.country_code,
            distance,
            primary_location.current_load * 100.0,
            primary_location.health_score * 100.0
        );

        // Calculate confidence score
        let confidence_score =
            self.calculate_confidence_score(&primary_location, user_location, &fallback_locations);

        Ok(RoutingDecision {
            primary_location,
            fallback_locations,
            routing_reason,
            estimated_latency_ms,
            confidence_score,
        })
    }

    /// Update health and load metrics for an edge location
    pub fn update_edge_metrics(&mut self, datacenter_id: &str, load: f32, health: f32) -> bool {
        if let Some(location) =
            self.edge_locations.iter_mut().find(|loc| loc.datacenter_id == datacenter_id)
        {
            location.update_metrics(load, health);
            true
        } else {
            false
        }
    }

    /// Perform health check on all edge locations
    pub async fn perform_health_check(&mut self) -> Result<js_sys::Array, JsValue> {
        let current_time = js_sys::Date::now() as u64;

        if current_time - self.last_health_check < self.health_check_interval {
            return Ok(js_sys::Array::new()); // Too soon for health check
        }

        let results = js_sys::Array::new();

        // Use index-based iteration to avoid double mutable borrow
        let location_count = self.edge_locations.len();
        for i in 0..location_count {
            let health_result = self.check_location_health_by_index(i).await?;
            results.push(&health_result);
        }

        self.last_health_check = current_time;
        Ok(results)
    }

    /// Get load balancing statistics
    pub fn get_load_balancing_stats(&self) -> JsValue {
        let stats = js_sys::Object::new();

        let total_locations = self.edge_locations.len() as f32;
        let healthy_locations =
            self.edge_locations.iter().filter(|loc| loc.health_score > 0.5).count() as f32;
        let average_load =
            self.edge_locations.iter().map(|loc| loc.current_load).sum::<f32>() / total_locations;
        let average_health =
            self.edge_locations.iter().map(|loc| loc.health_score).sum::<f32>() / total_locations;

        // Regional distribution
        let mut region_counts = BTreeMap::new();
        for location in &self.edge_locations {
            *region_counts.entry(location.region).or_insert(0) += 1;
        }

        let _ = js_sys::Reflect::set(
            &stats,
            &JsValue::from_str("total_locations"),
            &JsValue::from(total_locations),
        );
        let _ = js_sys::Reflect::set(
            &stats,
            &JsValue::from_str("healthy_locations"),
            &JsValue::from(healthy_locations),
        );
        let _ = js_sys::Reflect::set(
            &stats,
            &JsValue::from_str("average_load"),
            &JsValue::from(average_load),
        );
        let _ = js_sys::Reflect::set(
            &stats,
            &JsValue::from_str("average_health"),
            &JsValue::from(average_health),
        );
        let _ = js_sys::Reflect::set(
            &stats,
            &JsValue::from_str("health_percentage"),
            &JsValue::from(healthy_locations / total_locations * 100.0),
        );

        // Add regional distribution
        let regions = js_sys::Object::new();
        for (region, count) in region_counts {
            let region_name = match region {
                GeoRegion::NorthAmerica => "north_america",
                GeoRegion::Europe => "europe",
                GeoRegion::AsiaPacific => "asia_pacific",
                GeoRegion::SouthAmerica => "south_america",
                GeoRegion::Africa => "africa",
                GeoRegion::MiddleEast => "middle_east",
                GeoRegion::Oceania => "oceania",
            };
            let _ = js_sys::Reflect::set(
                &regions,
                &JsValue::from_str(region_name),
                &JsValue::from(count),
            );
        }
        let _ = js_sys::Reflect::set(
            &stats,
            &JsValue::from_str("regional_distribution"),
            &regions,
        );

        stats.into()
    }

    /// Get recommendations for optimal edge deployment
    pub fn get_deployment_recommendations(&self) -> js_sys::Array {
        let recommendations = js_sys::Array::new();

        // Check regional coverage
        let mut region_coverage = BTreeMap::new();
        for location in &self.edge_locations {
            *region_coverage.entry(location.region).or_insert(0) += 1;
        }

        // Recommend missing regions
        let all_regions = [
            GeoRegion::NorthAmerica,
            GeoRegion::Europe,
            GeoRegion::AsiaPacific,
            GeoRegion::SouthAmerica,
            GeoRegion::Africa,
            GeoRegion::MiddleEast,
            GeoRegion::Oceania,
        ];

        for region in &all_regions {
            if !region_coverage.contains_key(region) {
                let recommendation = format!(
                    "Deploy edge location in {:?} region for global coverage",
                    region
                );
                recommendations.push(&JsValue::from_str(&recommendation));
            }
        }

        // Check for overloaded regions
        for (region, count) in region_coverage {
            if count > 5 {
                let recommendation = format!(
                    "Consider load balancing in {:?} region (currently {} locations)",
                    region, count
                );
                recommendations.push(&JsValue::from_str(&recommendation));
            }
        }

        // Check for unhealthy locations
        let unhealthy_count =
            self.edge_locations.iter().filter(|loc| loc.health_score < 0.5).count();
        if unhealthy_count > 0 {
            let recommendation =
                format!("Investigate {} unhealthy edge locations", unhealthy_count);
            recommendations.push(&JsValue::from_str(&recommendation));
        }

        recommendations
    }
}

impl GeoDistributionManager {
    /// Initialize default edge locations
    fn initialize_default_locations(&mut self) {
        // North America
        self.edge_locations.push(EdgeLocation::new(
            GeoRegion::NorthAmerica,
            "US".to_string(),
            "New York".to_string(),
            "us-east-1".to_string(),
            40.7128,
            -74.0060,
            EdgeRuntime::CloudflareWorkers,
            crate::runtime::edge_runtime::get_edge_capabilities(),
        ));

        self.edge_locations.push(EdgeLocation::new(
            GeoRegion::NorthAmerica,
            "US".to_string(),
            "San Francisco".to_string(),
            "us-west-1".to_string(),
            37.7749,
            -122.4194,
            EdgeRuntime::VercelEdge,
            crate::runtime::edge_runtime::get_edge_capabilities(),
        ));

        // Europe
        self.edge_locations.push(EdgeLocation::new(
            GeoRegion::Europe,
            "GB".to_string(),
            "London".to_string(),
            "eu-west-1".to_string(),
            51.5074,
            -0.1278,
            EdgeRuntime::CloudflareWorkers,
            crate::runtime::edge_runtime::get_edge_capabilities(),
        ));

        self.edge_locations.push(EdgeLocation::new(
            GeoRegion::Europe,
            "DE".to_string(),
            "Frankfurt".to_string(),
            "eu-central-1".to_string(),
            50.1109,
            8.6821,
            EdgeRuntime::FastlyCompute,
            crate::runtime::edge_runtime::get_edge_capabilities(),
        ));

        // Asia Pacific
        self.edge_locations.push(EdgeLocation::new(
            GeoRegion::AsiaPacific,
            "SG".to_string(),
            "Singapore".to_string(),
            "ap-southeast-1".to_string(),
            1.3521,
            103.8198,
            EdgeRuntime::DenoDeploy,
            crate::runtime::edge_runtime::get_edge_capabilities(),
        ));

        self.edge_locations.push(EdgeLocation::new(
            GeoRegion::AsiaPacific,
            "JP".to_string(),
            "Tokyo".to_string(),
            "ap-northeast-1".to_string(),
            35.6762,
            139.6503,
            EdgeRuntime::CloudflareWorkers,
            crate::runtime::edge_runtime::get_edge_capabilities(),
        ));

        // Set initial metrics
        for location in &mut self.edge_locations {
            location.current_load = 0.1; // 10% initial load
            location.health_score = 0.95; // 95% healthy
            location.last_health_check = js_sys::Date::now() as u64;
        }
    }

    /// Calculate routing score for a location
    fn calculate_routing_score(
        &self,
        location: &EdgeLocation,
        user_location: &UserLocation,
    ) -> f32 {
        let distance_score = location.routing_score(
            user_location.latitude,
            user_location.longitude,
            self.routing_weights.distance,
            self.routing_weights.load,
            self.routing_weights.health,
        );

        // Add region preference bonus
        let region_bonus = if location.region == user_location.region {
            -0.1 * self.routing_weights.region_preference
        } else {
            0.0
        };

        // Add runtime compatibility bonus
        let runtime_bonus = match location.runtime_type {
            EdgeRuntime::CloudflareWorkers | EdgeRuntime::VercelEdge => {
                -0.05 * self.routing_weights.runtime_compatibility
            },
            EdgeRuntime::DenoDeploy => -0.03 * self.routing_weights.runtime_compatibility,
            _ => 0.0,
        };

        distance_score + region_bonus + runtime_bonus
    }

    /// Estimate latency from distance
    fn estimate_latency_from_distance(&self, distance_km: f64) -> u32 {
        // Base latency + distance factor
        let base_latency_ms = 10.0;
        let distance_factor = distance_km / 1000.0 * 5.0; // ~5ms per 1000km
        let network_overhead = 20.0; // Additional network overhead

        (base_latency_ms + distance_factor + network_overhead) as u32
    }

    /// Calculate confidence score for routing decision
    fn calculate_confidence_score(
        &self,
        primary: &EdgeLocation,
        user: &UserLocation,
        fallbacks: &[EdgeLocation],
    ) -> f32 {
        let mut confidence = 0.8; // Base confidence

        // High health score increases confidence
        confidence += (primary.health_score - 0.5) * 0.2;

        // Low load increases confidence
        confidence += (1.0 - primary.current_load) * 0.1;

        // Having fallbacks increases confidence
        confidence += (fallbacks.len() as f32) * 0.05;

        // Same region as user increases confidence
        if primary.region == user.region {
            confidence += 0.1;
        }

        // Recent health check increases confidence
        let time_since_check = js_sys::Date::now() as u64 - primary.last_health_check;
        if time_since_check < 60000 {
            // Within 1 minute
            confidence += 0.05;
        }

        confidence.clamp(0.0, 1.0)
    }

    /// Detect location using geolocation API
    async fn detect_location_geolocation(&self) -> Result<UserLocation, JsValue> {
        let geolocation = web_sys::window()
            .ok_or("No window object")?
            .navigator()
            .geolocation()
            .map_err(|_| "Geolocation not available")?;

        let position = JsFuture::from(js_sys::Promise::new(&mut |resolve, reject| {
            // PositionOptions not available in web-sys 0.3.81 - use default options
            // let options = web_sys::PositionOptions::new();
            // options.set_enable_high_accuracy(true);
            // options.set_timeout(10000);
            // options.set_maximum_age(300000);

            let _ = geolocation.get_current_position_with_error_callback(&resolve, Some(&reject));
        }))
        .await?;

        // Position type not available in web-sys 0.3.81 - using JsValue
        let _position_obj = position;
        // Geolocation API types not fully available in web-sys 0.3.81
        // Using default location until proper types are available
        let latitude = 40.7128; // Default to NYC
        let longitude = -74.0060;
        let accuracy = 100.0;

        // Determine region from coordinates
        let region = self.determine_region_from_coords(latitude, longitude);

        Ok(UserLocation::new(
            latitude,
            longitude,
            "unknown".to_string(), // Would need reverse geocoding
            region,
            "unknown".to_string(),
            accuracy,
            "unknown".to_string(),
            "unknown".to_string(),
        ))
    }

    /// Detect location using IP geolocation
    async fn detect_location_ip(&self) -> Result<UserLocation, JsValue> {
        use wasm_bindgen_futures::JsFuture;

        // Try to get IP geolocation using a public service
        // Note: In production, you might want to use your own geolocation service
        let window = web_sys::window().ok_or_else(|| JsValue::from_str("No window object"))?;

        let fetch_options = web_sys::RequestInit::new();
        fetch_options.set_method("GET");

        // Try ipapi.co first (free API with reasonable limits)
        let request =
            web_sys::Request::new_with_str_and_init("https://ipapi.co/json/", &fetch_options)?;

        let resp = JsFuture::from(window.fetch_with_request(&request)).await?;
        let resp: web_sys::Response = resp.dyn_into()?;

        if resp.ok() {
            let json = JsFuture::from(resp.json()?).await?;

            // Parse the response
            if let Ok(lat_val) = js_sys::Reflect::get(&json, &"latitude".into()) {
                if let Ok(lng_val) = js_sys::Reflect::get(&json, &"longitude".into()) {
                    if let (Some(lat), Some(lng)) = (lat_val.as_f64(), lng_val.as_f64()) {
                        let country = js_sys::Reflect::get(&json, &"country_name".into())
                            .ok()
                            .and_then(|v| v.as_string())
                            .unwrap_or_else(|| "Unknown".to_string());

                        let timezone = js_sys::Reflect::get(&json, &"timezone".into())
                            .ok()
                            .and_then(|v| v.as_string())
                            .unwrap_or_else(|| "UTC".to_string());

                        let ip_address = js_sys::Reflect::get(&json, &"ip".into())
                            .ok()
                            .and_then(|v| v.as_string())
                            .unwrap_or_else(|| "Unknown".to_string());

                        let autonomous_system = js_sys::Reflect::get(&json, &"org".into())
                            .ok()
                            .and_then(|v| v.as_string())
                            .unwrap_or_else(|| "Unknown".to_string());

                        // Determine region from coordinates
                        let region = self.determine_region_from_coords(lat, lng);

                        return Ok(UserLocation::new(
                            lat,
                            lng,
                            country,
                            region,
                            timezone,
                            1000.0, // IP geolocation typically has ~1km accuracy
                            ip_address,
                            autonomous_system,
                        ));
                    }
                }
            }
        }

        Err(JsValue::from_str("Could not determine location from IP"))
    }

    /// Detect location using timezone
    async fn detect_location_timezone(&self) -> Result<UserLocation, JsValue> {
        let resolved_options =
            js_sys::Intl::DateTimeFormat::new(&js_sys::Array::new(), &js_sys::Object::new())
                .resolved_options();
        let timezone = js_sys::Reflect::get(&resolved_options, &JsValue::from_str("timeZone"))
            .ok()
            .and_then(|v| v.as_string())
            .unwrap_or_else(|| "UTC".to_string());

        // Map timezone to approximate location
        let (lat, lon, region) = match timezone.as_str() {
            "America/New_York" => (40.7128, -74.0060, GeoRegion::NorthAmerica),
            "America/Los_Angeles" => (34.0522, -118.2437, GeoRegion::NorthAmerica),
            "Europe/London" => (51.5074, -0.1278, GeoRegion::Europe),
            "Europe/Paris" => (48.8566, 2.3522, GeoRegion::Europe),
            "Asia/Tokyo" => (35.6762, 139.6503, GeoRegion::AsiaPacific),
            "Asia/Singapore" => (1.3521, 103.8198, GeoRegion::AsiaPacific),
            _ => (0.0, 0.0, GeoRegion::Europe), // Default to Greenwich
        };

        Ok(UserLocation::new(
            lat,
            lon,
            "unknown".to_string(),
            region,
            timezone,
            10000.0, // Low accuracy
            "unknown".to_string(),
            "unknown".to_string(),
        ))
    }

    /// Determine region from coordinates
    fn determine_region_from_coords(&self, lat: f64, lon: f64) -> GeoRegion {
        // Simple region detection based on coordinates
        if (15.0..=72.0).contains(&lat) && (-168.0..=-52.0).contains(&lon) {
            GeoRegion::NorthAmerica
        } else if (35.0..=71.0).contains(&lat) && (-25.0..=40.0).contains(&lon) {
            GeoRegion::Europe
        } else if (-47.0..=55.0).contains(&lat) && (68.0..=180.0).contains(&lon) {
            GeoRegion::AsiaPacific
        } else if (-55.0..=13.0).contains(&lat) && (-82.0..=-35.0).contains(&lon) {
            GeoRegion::SouthAmerica
        } else if (-35.0..=37.0).contains(&lat) && (-18.0..=51.0).contains(&lon) {
            GeoRegion::Africa
        } else if (12.0..=42.0).contains(&lat) && (26.0..=75.0).contains(&lon) {
            GeoRegion::MiddleEast
        } else if (-47.0..=-10.0).contains(&lat) && (113.0..=179.0).contains(&lon) {
            GeoRegion::Oceania
        } else {
            GeoRegion::Europe // Default
        }
    }

    /// Check health of a location by index
    async fn check_location_health_by_index(&mut self, index: usize) -> Result<JsValue, JsValue> {
        // This would typically perform actual health checks
        // For now, simulate health check results

        let health_score = 0.9 + (js_sys::Math::random() - 0.5) * 0.2;
        let load = js_sys::Math::random() * 0.8; // 0-80% load

        let location = &mut self.edge_locations[index];
        location.update_metrics(load as f32, health_score as f32);

        let datacenter_id = location.datacenter_id.clone();

        let result = js_sys::Object::new();
        let _ = js_sys::Reflect::set(
            &result,
            &JsValue::from_str("datacenter_id"),
            &JsValue::from_str(&datacenter_id),
        );
        let _ = js_sys::Reflect::set(
            &result,
            &JsValue::from_str("health_score"),
            &JsValue::from(health_score),
        );
        let _ = js_sys::Reflect::set(&result, &JsValue::from_str("load"), &JsValue::from(load));
        let _ = js_sys::Reflect::set(
            &result,
            &JsValue::from_str("status"),
            &JsValue::from_str("healthy"),
        );

        Ok(result.into())
    }
}

impl Default for GeoDistributionManager {
    fn default() -> Self {
        Self::new()
    }
}

/// Utility functions for geographic distribution
#[wasm_bindgen]
pub fn create_geo_distribution_manager() -> GeoDistributionManager {
    GeoDistributionManager::new()
}

#[wasm_bindgen]
pub fn get_distance_between_points(lat1: f64, lon1: f64, lat2: f64, lon2: f64) -> f64 {
    let r = 6371.0; // Earth's radius in kilometers
    let lat1_rad = lat1.to_radians();
    let lat2_rad = lat2.to_radians();
    let delta_lat = (lat2 - lat1).to_radians();
    let delta_lon = (lon2 - lon1).to_radians();

    let a = (delta_lat / 2.0).sin().powi(2)
        + lat1_rad.cos() * lat2_rad.cos() * (delta_lon / 2.0).sin().powi(2);
    let c = 2.0 * a.sqrt().atan2((1.0 - a).sqrt());

    r * c
}

#[wasm_bindgen]
pub fn estimate_network_latency(distance_km: f64) -> u32 {
    let base_latency = 5.0;
    let distance_factor = distance_km / 1000.0 * 3.0; // ~3ms per 1000km
    let jitter = 10.0;

    (base_latency + distance_factor + jitter) as u32
}