import type { BaseTool } from '@/tools'; import type { ToolType } from '@/types'; /** * Tool loader cache */ const toolCache = new Map(); const toolLoadingPromises = new Map>(); /** * Dynamically import and instantiate a tool */ async function loadTool(toolType: ToolType): Promise { // Check cache first if (toolCache.has(toolType)) { return toolCache.get(toolType)!; } // Check if already loading if (toolLoadingPromises.has(toolType)) { return toolLoadingPromises.get(toolType)!; } // Start loading const loadPromise = (async () => { let tool: BaseTool; switch (toolType) { case 'pencil': { const { PencilTool } = await import('@/tools/pencil-tool'); tool = new PencilTool(); break; } case 'brush': { const { BrushTool } = await import('@/tools/brush-tool'); tool = new BrushTool(); break; } case 'eraser': { const { EraserTool } = await import('@/tools/eraser-tool'); tool = new EraserTool(); break; } case 'fill': { const { FillTool } = await import('@/tools/fill-tool'); tool = new FillTool(); break; } case 'eyedropper': { const { EyedropperTool } = await import('@/tools/eyedropper-tool'); tool = new EyedropperTool(); break; } case 'select': case 'rectangular-select': { const { RectangularSelectionTool } = await import('@/tools/rectangular-selection-tool'); tool = new RectangularSelectionTool(); break; } case 'elliptical-select': { const { EllipticalSelectionTool } = await import('@/tools/elliptical-selection-tool'); tool = new EllipticalSelectionTool(); break; } case 'lasso-select': { const { LassoSelectionTool } = await import('@/tools/lasso-selection-tool'); tool = new LassoSelectionTool(); break; } case 'magic-wand': { const { MagicWandTool } = await import('@/tools/magic-wand-tool'); tool = new MagicWandTool(); break; } case 'move': { const { MoveTool } = await import('@/tools/move-tool'); tool = new MoveTool(); break; } case 'transform': { const { FreeTransformTool } = await import('@/tools/free-transform-tool'); tool = new FreeTransformTool(); break; } case 'shape': { const { ShapeTool } = await import('@/tools/shape-tool'); tool = new ShapeTool(); break; } case 'text': { const { TextTool } = await import('@/tools/text-tool'); tool = new TextTool(); break; } default: { // Fallback to pencil tool const { PencilTool } = await import('@/tools/pencil-tool'); tool = new PencilTool(); } } // Cache the tool toolCache.set(toolType, tool); toolLoadingPromises.delete(toolType); return tool; })(); toolLoadingPromises.set(toolType, loadPromise); return loadPromise; } /** * Get a tool instance (loads it if not cached) */ export async function getTool(toolType: ToolType): Promise { return loadTool(toolType); } /** * Preload a tool (for performance optimization) */ export function preloadTool(toolType: ToolType): void { loadTool(toolType).catch((error) => { console.error(`Failed to preload tool ${toolType}:`, error); }); } /** * Preload commonly used tools */ export function preloadCommonTools(): void { // Preload the most commonly used tools preloadTool('pencil'); preloadTool('brush'); preloadTool('eraser'); } /** * Check if a tool is loaded */ export function isToolLoaded(toolType: ToolType): boolean { return toolCache.has(toolType); } /** * Clear tool cache (for testing) */ export function clearToolCache(): void { toolCache.clear(); toolLoadingPromises.clear(); }