oxigdal-wasm 0.1.4

WebAssembly bindings for OxiGDAL - Browser-based geospatial processing
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
//! Tile management and caching system
//!
//! This module provides comprehensive tile coordinate systems, LRU caching,
//! pyramid management, prefetching, and memory management for WASM geospatial applications.
//!
//! # Overview
//!
//! The tile module implements the core tile management infrastructure for oxigdal-wasm:
//!
//! - **Tile Coordinates**: XYZ tile coordinate system (level, x, y)
//! - **Tile Pyramids**: Multi-resolution tile pyramids for efficient zooming
//! - **LRU Caching**: Least Recently Used cache with configurable size limits
//! - **Prefetching**: Intelligent prefetching strategies for smooth interaction
//! - **Memory Management**: Automatic eviction when memory limits are reached
//!
//! # Tile Coordinate System
//!
//! Tiles are addressed using a standard XYZ coordinate system:
//!
//! ```text
//! Level 0:     [0,0,0]                  (1 tile)
//!
//! Level 1:  [1,0,0] [1,1,0]             (4 tiles)
//!           [1,0,1] [1,1,1]
//!
//! Level 2:  [2,0,0] [2,1,0] [2,2,0] [2,3,0]  (16 tiles)
//!           [2,0,1] [2,1,1] [2,2,1] [2,3,1]
//!           [2,0,2] [2,1,2] [2,2,2] [2,3,2]
//!           [2,0,3] [2,1,3] [2,2,3] [2,3,3]
//! ```
//!
//! Each level has 4^level tiles. The tree structure allows efficient
//! parent/child relationships for multi-resolution rendering.
//!
//! # Tile Pyramid Structure
//!
//! A tile pyramid represents the complete multi-resolution structure:
//!
//! ```rust
//! use oxigdal_wasm::TilePyramid;
//!
//! // Create pyramid for a 4096x2048 image with 256x256 tiles
//! let pyramid = TilePyramid::new(4096, 2048, 256, 256);
//!
//! // Level 0: 16x8 tiles (full resolution)
//! // Level 1: 8x4 tiles (2x downsampled)
//! // Level 2: 4x2 tiles (4x downsampled)
//! // Level 3: 2x1 tiles (8x downsampled)
//! // Level 4: 1x1 tile (16x downsampled)
//!
//! println!("Levels: {}", pyramid.num_levels);
//! println!("Total tiles: {}", pyramid.total_tiles());
//! ```
//!
//! # LRU Cache Implementation
//!
//! The cache uses a Least Recently Used eviction policy:
//!
//! 1. Tiles are stored in a HashMap for O(1) lookups
//! 2. Access order is tracked in a VecDeque
//! 3. When cache is full, oldest tile is evicted
//! 4. Cache hits update the access time
//!
//! ```rust
//! use oxigdal_wasm::{TileCache, TileCoord};
//!
//! let mut cache = TileCache::new(100 * 1024 * 1024); // 100 MB
//!
//! let coord = TileCoord::new(0, 0, 0);
//! let tile_data = vec![0u8; 256 * 256 * 4];
//!
//! // Cache the tile
//! cache.put(coord, tile_data.clone(), 0.0).expect("Put failed");
//!
//! // Retrieve from cache
//! let cached = cache.get(&coord, 1.0).expect("Cache miss");
//!
//! // Check statistics
//! let stats = cache.stats();
//! println!("Hit rate: {:.2}%", stats.hit_rate() * 100.0);
//! ```
//!
//! # Prefetching Strategies
//!
//! Multiple prefetching strategies are supported:
//!
//! ## Neighbors
//! Prefetches 8 immediately adjacent tiles:
//! ```text
//! [NW] [N]  [NE]
//! [W]  [C]  [E]
//! [SW] [S]  [SE]
//! ```
//!
//! ## Radius
//! Prefetches all tiles within a given radius (Euclidean distance).
//!
//! ## Pyramid
//! Prefetches parent tile (for zoom out) and 4 child tiles (for zoom in).
//!
//! ## Directional
//! Prefetches tiles in a specific direction (for panning).
//!
//! # Performance Characteristics
//!
//! - Cache lookup: O(1) average, O(n) worst case
//! - Cache insertion: O(1) average, O(n) worst case
//! - Cache eviction: O(1)
//! - Pyramid traversal: O(log n) for parent/child lookup
//!
//! Memory overhead:
//! - TileCoord: 12 bytes (3 u32 values)
//! - CachedTile: ~48 bytes + data size
//! - Cache entry: ~80 bytes + data size
//!
//! # Example: Complete Tile Loading
//!
//! ```rust
//! use oxigdal_wasm::{TileCache, TileCoord, TilePrefetcher, PrefetchStrategy, TilePyramid};
//!
//! // Setup
//! let mut cache = TileCache::new(50 * 1024 * 1024); // 50 MB cache
//! let pyramid = TilePyramid::new(4096, 4096, 256, 256);
//! let prefetcher = TilePrefetcher::new(
//!     PrefetchStrategy::Neighbors,
//!     pyramid.clone()
//! );
//!
//! // Load center tile
//! let center = TileCoord::new(0, 8, 8); // Center of level 0
//! let tile_data = vec![0u8; 256 * 256 * 4]; // Simulated tile data
//!
//! cache.put(center, tile_data, 0.0).expect("Cache put failed");
//!
//! // Get tiles to prefetch
//! let to_prefetch = prefetcher.prefetch_for(&center);
//! println!("Prefetching {} tiles", to_prefetch.len());
//!
//! // Load prefetch tiles (in real code, fetch from network)
//! for coord in to_prefetch {
//!     if !cache.contains(&coord) {
//!         // Fetch and cache tile
//!         // let tile = fetch_tile(coord).await;
//!         // cache.put(coord, tile, timestamp)?;
//!     }
//! }
//! ```

use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::hash::Hash;
use wasm_bindgen::prelude::*;

use crate::error::{TileCacheError, WasmError, WasmResult};

/// Maximum cache size in bytes (default: 100MB)
pub const DEFAULT_MAX_CACHE_SIZE: usize = 100 * 1024 * 1024;

/// Default tile size in pixels
#[allow(dead_code)]
pub const DEFAULT_TILE_SIZE: u32 = 256;

/// Tile coordinate in a pyramid
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TileCoord {
    /// Zoom level (0 = most zoomed out)
    pub level: u32,
    /// Column index (x)
    pub x: u32,
    /// Row index (y)
    pub y: u32,
}

impl TileCoord {
    /// Creates a new tile coordinate
    pub const fn new(level: u32, x: u32, y: u32) -> Self {
        Self { level, x, y }
    }

    /// Returns the parent tile coordinate (one level up)
    pub const fn parent(self) -> Option<Self> {
        if self.level == 0 {
            return None;
        }

        Some(Self {
            level: self.level - 1,
            x: self.x / 2,
            y: self.y / 2,
        })
    }

    /// Returns the four child tile coordinates (one level down)
    pub const fn children(self) -> [Self; 4] {
        let level = self.level + 1;
        let x2 = self.x * 2;
        let y2 = self.y * 2;

        [
            Self {
                level,
                x: x2,
                y: y2,
            },
            Self {
                level,
                x: x2 + 1,
                y: y2,
            },
            Self {
                level,
                x: x2,
                y: y2 + 1,
            },
            Self {
                level,
                x: x2 + 1,
                y: y2 + 1,
            },
        ]
    }

    /// Returns the tile key for caching
    pub fn key(&self) -> String {
        format!("{}/{}/{}", self.level, self.x, self.y)
    }

    /// Parses a tile key
    pub fn from_key(key: &str) -> Option<Self> {
        let parts: Vec<&str> = key.split('/').collect();
        if parts.len() != 3 {
            return None;
        }

        let level = parts[0].parse().ok()?;
        let x = parts[1].parse().ok()?;
        let y = parts[2].parse().ok()?;

        Some(Self::new(level, x, y))
    }

    /// Checks if this tile is valid for the given bounds
    pub const fn is_valid(&self, max_level: u32, max_x: u32, max_y: u32) -> bool {
        self.level <= max_level && self.x < max_x && self.y < max_y
    }

    /// Returns neighboring tile coordinates
    pub const fn neighbors(&self) -> [Option<Self>; 8] {
        let level = self.level;
        let x = self.x;
        let y = self.y;

        [
            // Top-left
            if x > 0 && y > 0 {
                Some(Self::new(level, x - 1, y - 1))
            } else {
                None
            },
            // Top
            if y > 0 {
                Some(Self::new(level, x, y - 1))
            } else {
                None
            },
            // Top-right
            if y > 0 {
                Some(Self::new(level, x + 1, y - 1))
            } else {
                None
            },
            // Left
            if x > 0 {
                Some(Self::new(level, x - 1, y))
            } else {
                None
            },
            // Right
            Some(Self::new(level, x + 1, y)),
            // Bottom-left
            if x > 0 {
                Some(Self::new(level, x - 1, y + 1))
            } else {
                None
            },
            // Bottom
            Some(Self::new(level, x, y + 1)),
            // Bottom-right
            Some(Self::new(level, x + 1, y + 1)),
        ]
    }
}

/// Tile bounds in the pyramid
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TileBounds {
    /// Minimum x coordinate
    pub min_x: u32,
    /// Minimum y coordinate
    pub min_y: u32,
    /// Maximum x coordinate (exclusive)
    pub max_x: u32,
    /// Maximum y coordinate (exclusive)
    pub max_y: u32,
}

impl TileBounds {
    /// Creates new tile bounds
    pub const fn new(min_x: u32, min_y: u32, max_x: u32, max_y: u32) -> Self {
        Self {
            min_x,
            min_y,
            max_x,
            max_y,
        }
    }

    /// Checks if a coordinate is within bounds
    pub const fn contains(&self, coord: &TileCoord) -> bool {
        coord.x >= self.min_x
            && coord.x < self.max_x
            && coord.y >= self.min_y
            && coord.y < self.max_y
    }

    /// Returns the number of tiles in these bounds
    pub const fn count(&self) -> u64 {
        (self.max_x - self.min_x) as u64 * (self.max_y - self.min_y) as u64
    }

    /// Returns an iterator over tile coordinates in these bounds
    pub fn iter(&self) -> TileBoundsIter {
        TileBoundsIter {
            bounds: *self,
            current_x: self.min_x,
            current_y: self.min_y,
        }
    }
}

/// Iterator over tile coordinates in bounds
pub struct TileBoundsIter {
    bounds: TileBounds,
    current_x: u32,
    current_y: u32,
}

impl Iterator for TileBoundsIter {
    type Item = (u32, u32);

    fn next(&mut self) -> Option<Self::Item> {
        if self.current_y >= self.bounds.max_y {
            return None;
        }

        let result = (self.current_x, self.current_y);

        self.current_x += 1;
        if self.current_x >= self.bounds.max_x {
            self.current_x = self.bounds.min_x;
            self.current_y += 1;
        }

        Some(result)
    }
}

/// Tile pyramid metadata
#[derive(Debug, Clone)]
pub struct TilePyramid {
    /// Image width in pixels
    pub width: u64,
    /// Image height in pixels
    pub height: u64,
    /// Tile width in pixels
    pub tile_width: u32,
    /// Tile height in pixels
    pub tile_height: u32,
    /// Number of pyramid levels
    pub num_levels: u32,
    /// Tiles per level (width, height)
    pub tiles_per_level: Vec<(u32, u32)>,
}

impl TilePyramid {
    /// Creates a new tile pyramid
    pub fn new(width: u64, height: u64, tile_width: u32, tile_height: u32) -> Self {
        let mut tiles_per_level = Vec::new();
        let mut level_width = width;
        let mut level_height = height;
        let mut num_levels = 0;

        while level_width > 0 && level_height > 0 {
            let tiles_x = level_width.div_ceil(u64::from(tile_width)) as u32;
            let tiles_y = level_height.div_ceil(u64::from(tile_height)) as u32;
            tiles_per_level.push((tiles_x, tiles_y));
            num_levels += 1;

            // Stop when we reach a single tile (pyramid top)
            if tiles_x == 1 && tiles_y == 1 {
                break;
            }

            level_width /= 2;
            level_height /= 2;
        }

        Self {
            width,
            height,
            tile_width,
            tile_height,
            num_levels,
            tiles_per_level,
        }
    }

    /// Returns the number of tiles at a given level
    pub fn tiles_at_level(&self, level: u32) -> Option<(u32, u32)> {
        self.tiles_per_level.get(level as usize).copied()
    }

    /// Returns the tile bounds for a given level
    pub fn bounds_at_level(&self, level: u32) -> Option<TileBounds> {
        self.tiles_at_level(level)
            .map(|(tiles_x, tiles_y)| TileBounds::new(0, 0, tiles_x, tiles_y))
    }

    /// Checks if a tile coordinate is valid
    pub fn is_valid_coord(&self, coord: &TileCoord) -> bool {
        if coord.level >= self.num_levels {
            return false;
        }

        if let Some((tiles_x, tiles_y)) = self.tiles_at_level(coord.level) {
            coord.x < tiles_x && coord.y < tiles_y
        } else {
            false
        }
    }

    /// Returns the total number of tiles in the pyramid
    pub fn total_tiles(&self) -> u64 {
        self.tiles_per_level
            .iter()
            .map(|(x, y)| u64::from(*x) * u64::from(*y))
            .sum()
    }
}

/// Cached tile data
#[derive(Debug, Clone)]
pub struct CachedTile {
    /// Tile coordinate
    pub coord: TileCoord,
    /// Tile data
    pub data: Vec<u8>,
    /// Size in bytes
    pub size: usize,
    /// Access timestamp (for LRU)
    pub last_access: f64,
    /// Load timestamp
    pub loaded_at: f64,
    /// Number of accesses
    pub access_count: u64,
}

impl CachedTile {
    /// Creates a new cached tile
    pub fn new(coord: TileCoord, data: Vec<u8>, timestamp: f64) -> Self {
        let size = data.len();
        Self {
            coord,
            data,
            size,
            last_access: timestamp,
            loaded_at: timestamp,
            access_count: 1,
        }
    }

    /// Updates access information
    pub fn access(&mut self, timestamp: f64) {
        self.last_access = timestamp;
        self.access_count += 1;
    }
}

/// LRU tile cache
pub struct TileCache {
    /// Cache entries
    cache: HashMap<String, CachedTile>,
    /// LRU queue (tile keys in access order)
    lru_queue: VecDeque<String>,
    /// Current cache size in bytes
    current_size: usize,
    /// Maximum cache size in bytes
    max_size: usize,
    /// Cache hit count
    hit_count: u64,
    /// Cache miss count
    miss_count: u64,
}

impl TileCache {
    /// Creates a new tile cache
    pub fn new(max_size: usize) -> Self {
        Self {
            cache: HashMap::new(),
            lru_queue: VecDeque::new(),
            current_size: 0,
            max_size,
            hit_count: 0,
            miss_count: 0,
        }
    }

    /// Creates a new tile cache with default size
    pub fn with_default_size() -> Self {
        Self::new(DEFAULT_MAX_CACHE_SIZE)
    }

    /// Gets a tile from the cache
    pub fn get(&mut self, coord: &TileCoord, timestamp: f64) -> Option<Vec<u8>> {
        let key = coord.key();

        if let Some(tile) = self.cache.get_mut(&key) {
            tile.access(timestamp);
            self.hit_count += 1;

            // Move to end of LRU queue
            if let Some(pos) = self.lru_queue.iter().position(|k| k == &key) {
                self.lru_queue.remove(pos);
            }
            self.lru_queue.push_back(key);

            Some(tile.data.clone())
        } else {
            self.miss_count += 1;
            None
        }
    }

    /// Puts a tile into the cache
    pub fn put(&mut self, coord: TileCoord, data: Vec<u8>, timestamp: f64) -> WasmResult<()> {
        let key = coord.key();
        let tile_size = data.len();

        // Evict tiles if necessary when adding would exceed or reach capacity
        // Using >= because cache should maintain strict size limit
        while !self.lru_queue.is_empty() && self.current_size + tile_size >= self.max_size {
            self.evict_oldest()?;
        }

        // Check if we can fit the tile
        if tile_size > self.max_size {
            return Err(WasmError::TileCache(TileCacheError::Full {
                current_size: self.current_size,
                max_size: self.max_size,
            }));
        }

        // Add or update tile
        if let Some(old_tile) = self.cache.remove(&key) {
            self.current_size -= old_tile.size;
            if let Some(pos) = self.lru_queue.iter().position(|k| k == &key) {
                self.lru_queue.remove(pos);
            }
        }

        let tile = CachedTile::new(coord, data, timestamp);
        self.current_size += tile_size;
        self.cache.insert(key.clone(), tile);
        self.lru_queue.push_back(key);

        Ok(())
    }

    /// Evicts the oldest tile from the cache
    fn evict_oldest(&mut self) -> WasmResult<()> {
        if let Some(key) = self.lru_queue.pop_front() {
            if let Some(tile) = self.cache.remove(&key) {
                self.current_size -= tile.size;
                Ok(())
            } else {
                Err(WasmError::TileCache(TileCacheError::EvictionFailed {
                    message: format!("Tile {key} not found in cache"),
                }))
            }
        } else {
            Err(WasmError::TileCache(TileCacheError::EvictionFailed {
                message: "No tiles to evict".to_string(),
            }))
        }
    }

    /// Checks if a tile is in the cache
    pub fn contains(&self, coord: &TileCoord) -> bool {
        self.cache.contains_key(&coord.key())
    }

    /// Removes a tile from the cache
    pub fn remove(&mut self, coord: &TileCoord) -> Option<Vec<u8>> {
        let key = coord.key();
        if let Some(tile) = self.cache.remove(&key) {
            self.current_size -= tile.size;
            if let Some(pos) = self.lru_queue.iter().position(|k| k == &key) {
                self.lru_queue.remove(pos);
            }
            Some(tile.data)
        } else {
            None
        }
    }

    /// Clears the entire cache
    pub fn clear(&mut self) {
        self.cache.clear();
        self.lru_queue.clear();
        self.current_size = 0;
    }

    /// Returns cache statistics
    pub fn stats(&self) -> CacheStats {
        CacheStats {
            current_size: self.current_size,
            max_size: self.max_size,
            entry_count: self.cache.len(),
            hit_count: self.hit_count,
            miss_count: self.miss_count,
        }
    }

    /// Returns the cache hit rate
    pub fn hit_rate(&self) -> f64 {
        let total = self.hit_count + self.miss_count;
        if total == 0 {
            0.0
        } else {
            self.hit_count as f64 / total as f64
        }
    }

    /// Prefetches tiles in the given coordinates
    pub fn prefetch_list(&self, coords: &[TileCoord]) -> Vec<TileCoord> {
        coords
            .iter()
            .filter(|coord| !self.contains(coord))
            .copied()
            .collect()
    }
}

/// Cache statistics
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct CacheStats {
    /// Current cache size in bytes
    pub current_size: usize,
    /// Maximum cache size in bytes
    pub max_size: usize,
    /// Number of cached entries
    pub entry_count: usize,
    /// Number of cache hits
    pub hit_count: u64,
    /// Number of cache misses
    pub miss_count: u64,
}

impl CacheStats {
    /// Returns the cache hit rate
    pub fn hit_rate(&self) -> f64 {
        let total = self.hit_count + self.miss_count;
        if total == 0 {
            0.0
        } else {
            self.hit_count as f64 / total as f64
        }
    }

    /// Returns the cache utilization as a fraction
    pub fn utilization(&self) -> f64 {
        if self.max_size == 0 {
            0.0
        } else {
            self.current_size as f64 / self.max_size as f64
        }
    }
}

/// Prefetching strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PrefetchStrategy {
    /// No prefetching
    None,
    /// Prefetch immediate neighbors
    Neighbors,
    /// Prefetch in a radius
    Radius(u32),
    /// Prefetch pyramid (parent and children)
    Pyramid,
    /// Prefetch along a direction
    Directional {
        /// X direction delta
        dx: i32,
        /// Y direction delta
        dy: i32,
        /// Number of tiles to prefetch
        count: u32,
    },
}

/// Tile prefetcher
pub struct TilePrefetcher {
    /// Prefetch strategy
    strategy: PrefetchStrategy,
    /// Pyramid metadata
    pyramid: TilePyramid,
}

impl TilePrefetcher {
    /// Creates a new tile prefetcher
    pub const fn new(strategy: PrefetchStrategy, pyramid: TilePyramid) -> Self {
        Self { strategy, pyramid }
    }

    /// Returns tiles to prefetch for a given center tile
    pub fn prefetch_for(&self, center: &TileCoord) -> Vec<TileCoord> {
        match self.strategy {
            PrefetchStrategy::None => vec![],
            PrefetchStrategy::Neighbors => self.prefetch_neighbors(center),
            PrefetchStrategy::Radius(radius) => self.prefetch_radius(center, radius),
            PrefetchStrategy::Pyramid => self.prefetch_pyramid(center),
            PrefetchStrategy::Directional { dx, dy, count } => {
                self.prefetch_directional(center, dx, dy, count)
            }
        }
    }

    /// Prefetches immediate neighbors
    fn prefetch_neighbors(&self, center: &TileCoord) -> Vec<TileCoord> {
        center
            .neighbors()
            .iter()
            .filter_map(|&n| n)
            .filter(|coord| self.pyramid.is_valid_coord(coord))
            .collect()
    }

    /// Prefetches tiles in a radius
    fn prefetch_radius(&self, center: &TileCoord, radius: u32) -> Vec<TileCoord> {
        let mut tiles = Vec::new();
        let radius_i32 = radius as i32;

        for dy in -radius_i32..=radius_i32 {
            for dx in -radius_i32..=radius_i32 {
                // Skip the center tile itself
                if dx == 0 && dy == 0 {
                    continue;
                }

                // Calculate Euclidean distance
                let dist_sq = (dx * dx + dy * dy) as f64;
                if dist_sq > (radius as f64 * radius as f64) {
                    continue;
                }

                let x = center.x as i32 + dx;
                let y = center.y as i32 + dy;

                if x >= 0 && y >= 0 {
                    let coord = TileCoord::new(center.level, x as u32, y as u32);
                    if self.pyramid.is_valid_coord(&coord) {
                        tiles.push(coord);
                    }
                }
            }
        }

        tiles
    }

    /// Prefetches parent and children tiles
    fn prefetch_pyramid(&self, center: &TileCoord) -> Vec<TileCoord> {
        let mut tiles = Vec::new();

        // Add parent
        if let Some(parent) = center.parent() {
            if self.pyramid.is_valid_coord(&parent) {
                tiles.push(parent);
            }
        }

        // Add children
        for child in center.children() {
            if self.pyramid.is_valid_coord(&child) {
                tiles.push(child);
            }
        }

        tiles
    }

    /// Prefetches tiles in a direction
    fn prefetch_directional(
        &self,
        center: &TileCoord,
        dx: i32,
        dy: i32,
        count: u32,
    ) -> Vec<TileCoord> {
        let mut tiles = Vec::new();

        for i in 1..=count {
            let x = center.x as i32 + dx * i as i32;
            let y = center.y as i32 + dy * i as i32;

            if x >= 0 && y >= 0 {
                let coord = TileCoord::new(center.level, x as u32, y as u32);
                if self.pyramid.is_valid_coord(&coord) {
                    tiles.push(coord);
                } else {
                    break;
                }
            }
        }

        tiles
    }
}

/// WASM bindings for tile management
#[wasm_bindgen]
pub struct WasmTileCache {
    cache: TileCache,
}

#[wasm_bindgen]
impl WasmTileCache {
    /// Creates a new tile cache
    #[wasm_bindgen(constructor)]
    pub fn new(max_size_mb: usize) -> Self {
        let max_size = max_size_mb * 1024 * 1024;
        Self {
            cache: TileCache::new(max_size),
        }
    }

    /// Gets cache statistics as JSON
    #[wasm_bindgen(js_name = getStats)]
    pub fn get_stats(&self) -> String {
        serde_json::to_string(&self.cache.stats()).unwrap_or_default()
    }

    /// Clears the cache
    #[wasm_bindgen]
    pub fn clear(&mut self) {
        self.cache.clear();
    }

    /// Returns the cache hit rate
    #[wasm_bindgen(js_name = hitRate)]
    pub fn hit_rate(&self) -> f64 {
        self.cache.hit_rate()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tile_coord() {
        let coord = TileCoord::new(5, 10, 20);
        assert_eq!(coord.level, 5);
        assert_eq!(coord.x, 10);
        assert_eq!(coord.y, 20);
        assert_eq!(coord.key(), "5/10/20");
    }

    #[test]
    fn test_tile_coord_parent() {
        let coord = TileCoord::new(5, 10, 20);
        let parent = coord.parent().expect("Should have parent");
        assert_eq!(parent.level, 4);
        assert_eq!(parent.x, 5);
        assert_eq!(parent.y, 10);

        let root = TileCoord::new(0, 0, 0);
        assert!(root.parent().is_none());
    }

    #[test]
    fn test_tile_coord_children() {
        let coord = TileCoord::new(5, 10, 20);
        let children = coord.children();
        assert_eq!(children.len(), 4);
        assert_eq!(children[0], TileCoord::new(6, 20, 40));
        assert_eq!(children[1], TileCoord::new(6, 21, 40));
        assert_eq!(children[2], TileCoord::new(6, 20, 41));
        assert_eq!(children[3], TileCoord::new(6, 21, 41));
    }

    #[test]
    fn test_tile_pyramid() {
        let pyramid = TilePyramid::new(4096, 2048, 256, 256);
        assert_eq!(pyramid.tile_width, 256);
        assert_eq!(pyramid.tile_height, 256);

        let (tiles_x, tiles_y) = pyramid.tiles_at_level(0).expect("Level 0 exists");
        assert_eq!(tiles_x, 16); // 4096 / 256
        assert_eq!(tiles_y, 8); // 2048 / 256
    }

    #[test]
    fn test_tile_cache() {
        let mut cache = TileCache::new(1024);
        let coord = TileCoord::new(0, 0, 0);
        let data = vec![1, 2, 3, 4];

        // Put and get
        cache
            .put(coord, data.clone(), 0.0)
            .expect("Put should succeed");
        let retrieved = cache.get(&coord, 0.0).expect("Should find tile");
        assert_eq!(retrieved, data);

        // Check stats
        let stats = cache.stats();
        assert_eq!(stats.entry_count, 1);
        assert_eq!(stats.hit_count, 1);
    }

    #[test]
    fn test_cache_eviction() {
        let mut cache = TileCache::new(10); // Very small cache
        let coord1 = TileCoord::new(0, 0, 0);
        let coord2 = TileCoord::new(0, 0, 1);

        cache
            .put(coord1, vec![1, 2, 3, 4, 5], 0.0)
            .expect("Put should succeed");
        cache
            .put(coord2, vec![6, 7, 8, 9, 10], 1.0)
            .expect("Put should succeed");

        // First tile should be evicted
        assert!(!cache.contains(&coord1));
        assert!(cache.contains(&coord2));
    }

    #[test]
    fn test_tile_bounds() {
        let bounds = TileBounds::new(0, 0, 10, 10);
        assert_eq!(bounds.count(), 100);

        let coord_in = TileCoord::new(0, 5, 5);
        let coord_out = TileCoord::new(0, 15, 15);

        assert!(bounds.contains(&coord_in));
        assert!(!bounds.contains(&coord_out));
    }

    #[test]
    fn test_prefetch_neighbors() {
        let pyramid = TilePyramid::new(1024, 1024, 256, 256);
        let prefetcher = TilePrefetcher::new(PrefetchStrategy::Neighbors, pyramid);

        let center = TileCoord::new(0, 1, 1);
        let to_prefetch = prefetcher.prefetch_for(&center);

        // Should prefetch up to 8 neighbors
        assert!(!to_prefetch.is_empty());
        assert!(to_prefetch.len() <= 8);
    }

    #[test]
    fn test_from_key() {
        let coord = TileCoord::new(5, 10, 20);
        let key = coord.key();
        let parsed = TileCoord::from_key(&key).expect("Should parse");
        assert_eq!(parsed, coord);

        assert!(TileCoord::from_key("invalid").is_none());
        assert!(TileCoord::from_key("1/2").is_none());
    }
}