| 
									
										
										
										
											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-14 14:22:32 +02:00
										 |  |  | import { Utils } from "../../Utils" | 
					
						
							| 
									
										
										
										
											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-24 17:23:44 +02:00
										 |  |  |     private readonly _layerWhitelist: Set<string> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 16:14:57 +02:00
										 |  |  |     constructor(layout: LayoutConfig) { | 
					
						
							|  |  |  |         this._layout = layout | 
					
						
							| 
									
										
										
										
											2024-09-24 17:23:44 +02:00
										 |  |  |         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-24 17:23:44 +02:00
										 |  |  |     static scoreLayers(query: string, options: { | 
					
						
							|  |  |  |         whitelist?: Set<string>, blacklist?: Set<string> | 
					
						
							|  |  |  |     }): Record<string, number> { | 
					
						
							| 
									
										
										
										
											2024-09-11 17:31:38 +02:00
										 |  |  |         const result: Record<string, number> = {} | 
					
						
							| 
									
										
										
										
											2024-09-14 14:22:32 +02:00
										 |  |  |         const queryParts = query.trim().split(" ").map(q => Utils.simplifyStringForSearch(q)) | 
					
						
							| 
									
										
										
										
											2024-09-11 17:31:38 +02:00
										 |  |  |         for (const id in ThemeSearch.officialThemes.layers) { | 
					
						
							| 
									
										
										
										
											2024-09-24 17:23:44 +02:00
										 |  |  |             if (options?.whitelist && !options?.whitelist.has(id)) { | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (options?.blacklist?.has(id)) { | 
					
						
							| 
									
										
										
										
											2024-09-11 17:31:38 +02:00
										 |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             const keywords = ThemeSearch.officialThemes.layers[id] | 
					
						
							| 
									
										
										
										
											2024-09-24 17:23:44 +02:00
										 |  |  |             const distance = Math.min(...queryParts.map(q => SearchUtils.scoreKeywords(q, keywords))) | 
					
						
							| 
									
										
										
										
											2024-09-11 17:31:38 +02:00
										 |  |  |             result[id] = distance | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return result | 
					
						
							| 
									
										
										
										
											2024-09-11 01:46:55 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-24 18:08:01 +02:00
										 |  |  |     public search(query: string, limit: number, scoreThreshold: number = 2): LayerConfig[] { | 
					
						
							| 
									
										
										
										
											2024-09-11 01:46:55 +02:00
										 |  |  |         if (query.length < 1) { | 
					
						
							|  |  |  |             return [] | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-09-24 17:53:31 +02:00
										 |  |  |         const scores = LayerSearch.scoreLayers(query, { whitelist: this._layerWhitelist }) | 
					
						
							| 
									
										
										
										
											2024-09-24 17:23:44 +02:00
										 |  |  |         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-24 17:23:44 +02:00
										 |  |  |                 score: scores[layer], | 
					
						
							| 
									
										
										
										
											2024-09-11 01:46:55 +02:00
										 |  |  |             }) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         asList.sort((a, b) => a.score - b.score) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return asList | 
					
						
							| 
									
										
										
										
											2024-09-24 18:08:01 +02:00
										 |  |  |             .filter(sorted => sorted.score < scoreThreshold) | 
					
						
							| 
									
										
										
										
											2024-09-11 01:46:55 +02:00
										 |  |  |             .slice(0, limit) | 
					
						
							| 
									
										
										
										
											2024-09-11 17:31:38 +02:00
										 |  |  |             .map(l => l.layer) | 
					
						
							| 
									
										
										
										
											2024-09-11 01:46:55 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |