import { ViewerTool } from '@/types/viewer-tool'; import { computed } from 'vue'; type Tool = T extends ViewerTool[] ? T[number] : never; /** * Create a tool group where only one tool can be active at a time. * @param tools The tools. */ export function useToolGroup(...tools: T) { const activeTool = computed({ get(): Tool | null { const idx = tools.findIndex((tool) => tool.isActive.value); return idx !== -1 ? (tools[idx] as Tool) : null; }, set(tool: Tool | null): void { const prev = tools.find((t) => t.isActive.value); if (prev) prev.isActive.value = false; const next = tools.find((t) => t === tool); if (next) next.isActive.value = true; }, }); const activeIndex = computed(() => { const idx = tools.findIndex((tool) => tool.isActive.value); return idx !== -1 ? idx : null; }); function toggleTool(tool: Tool): boolean { const state = activeTool.value !== tool; activeTool.value = state ? tool : null; return state; } return { activeTool, activeIndex, toggleTool }; }