| 
									
										
										
										
											2024-09-11 01:46:55 +02:00
										 |  |  | import Constants from "../../Models/Constants" | 
					
						
							| 
									
										
										
										
											2024-09-11 17:31:38 +02:00
										 |  |  | import SearchUtils from "./SearchUtils" | 
					
						
							|  |  |  | import ThemeSearch from "./ThemeSearch" | 
					
						
							|  |  |  | import LayerConfig from "../../Models/ThemeConfig/LayerConfig" | 
					
						
							| 
									
										
										
										
											2024-09-12 16:14:57 +02:00
										 |  |  | import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig" | 
					
						
							| 
									
										
										
										
											2024-09-11 01:46:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-11 17:31:38 +02:00
										 |  |  | export default class LayerSearch { | 
					
						
							| 
									
										
										
										
											2024-09-11 01:46:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 16:14:57 +02:00
										 |  |  |     private readonly _layout: LayoutConfig | 
					
						
							| 
									
										
										
										
											2024-09-11 01:46:55 +02:00
										 |  |  |     private readonly _layerWhitelist : Set<string> | 
					
						
							| 
									
										
										
										
											2024-09-12 16:14:57 +02:00
										 |  |  |     constructor(layout: LayoutConfig) { | 
					
						
							|  |  |  |         this._layout = layout | 
					
						
							|  |  |  |         this._layerWhitelist = new Set(layout.layers.map(l => l.id).filter(id => Constants.added_by_default.indexOf(<any> id) < 0)) | 
					
						
							| 
									
										
										
										
											2024-09-11 01:46:55 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-11 17:31:38 +02:00
										 |  |  |     static scoreLayers(query: string, layerWhitelist?: Set<string>): Record<string, number> { | 
					
						
							|  |  |  |         const result: Record<string, number> = {} | 
					
						
							|  |  |  |         for (const id in ThemeSearch.officialThemes.layers) { | 
					
						
							|  |  |  |             if(layerWhitelist !== undefined && !layerWhitelist.has(id)){ | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             const keywords = ThemeSearch.officialThemes.layers[id] | 
					
						
							|  |  |  |             const distance = SearchUtils.scoreKeywords(query, keywords) | 
					
						
							|  |  |  |             result[id] = distance | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return result | 
					
						
							| 
									
										
										
										
											2024-09-11 01:46:55 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-11 17:31:38 +02:00
										 |  |  |     public search(query: string, limit: number): LayerConfig[] { | 
					
						
							| 
									
										
										
										
											2024-09-11 01:46:55 +02:00
										 |  |  |         if (query.length < 1) { | 
					
						
							|  |  |  |             return [] | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-09-11 17:31:38 +02:00
										 |  |  |         const scores = LayerSearch.scoreLayers(query, this._layerWhitelist) | 
					
						
							|  |  |  |         const asList:({layer: LayerConfig, score:number})[] = [] | 
					
						
							| 
									
										
										
										
											2024-09-11 01:46:55 +02:00
										 |  |  |         for (const layer in scores) { | 
					
						
							|  |  |  |             asList.push({ | 
					
						
							| 
									
										
										
										
											2024-09-12 16:14:57 +02:00
										 |  |  |                 layer: this._layout.getLayer(layer), | 
					
						
							| 
									
										
										
										
											2024-09-11 01:46:55 +02:00
										 |  |  |                 score: scores[layer] | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         asList.sort((a, b) => a.score - b.score) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return asList | 
					
						
							|  |  |  |             .filter(sorted => sorted.score < 2) | 
					
						
							|  |  |  |             .slice(0, limit) | 
					
						
							| 
									
										
										
										
											2024-09-11 17:31:38 +02:00
										 |  |  |             .map(l => l.layer) | 
					
						
							| 
									
										
										
										
											2024-09-11 01:46:55 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |