// ── Linked Timers ───────────────────────────────────────────── // "When timer A ends, start timer B" chain relationships import type { Timer } from './timer-engine'; // ── Types ───────────────────────────────────────────────────── export interface TimerLink { fromId: string; // timer that triggers toId: string; // timer that starts delay: number; // ms delay between from completing and to starting (0 = immediate) } export interface TimerChain { id: string; name: string; links: TimerLink[]; timerIds: string[]; // ordered list of timer IDs in chain } // ── Chain Building ──────────────────────────────────────────── /** * Build a chain from an ordered list of timer IDs. * Each timer links to the next with the specified delay. */ export function buildChain( name: string, timerIds: string[], delayMs: number = 0 ): TimerChain { const links: TimerLink[] = []; for (let i = 0; i < timerIds.length - 1; i++) { links.push({ fromId: timerIds[i], toId: timerIds[i + 1], delay: delayMs, }); } return { id: `chain-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`, name, links, timerIds, }; } /** * Add a timer to the end of an existing chain. */ export function appendToChain( chain: TimerChain, timerId: string, delayMs: number = 0 ): TimerChain { const lastId = chain.timerIds[chain.timerIds.length - 1]; if (!lastId) return chain; return { ...chain, timerIds: [...chain.timerIds, timerId], links: [ ...chain.links, { fromId: lastId, toId: timerId, delay: delayMs }, ], }; } /** * Remove a timer from a chain. Relinks neighbours if possible. */ export function removeFromChain( chain: TimerChain, timerId: string ): TimerChain { const idx = chain.timerIds.indexOf(timerId); if (idx === -1) return chain; const newTimerIds = chain.timerIds.filter((id) => id !== timerId); // Rebuild links from the new ordered list const newLinks: TimerLink[] = []; for (let i = 0; i < newTimerIds.length - 1; i++) { // Try to preserve original delay const existingLink = chain.links.find( (l) => l.fromId === newTimerIds[i] && l.toId === newTimerIds[i + 1] ); newLinks.push({ fromId: newTimerIds[i], toId: newTimerIds[i + 1], delay: existingLink?.delay ?? 0, }); } return { ...chain, timerIds: newTimerIds, links: newLinks, }; } // ── Chain Queries ───────────────────────────────────────────── /** * Find the next timer to start when a timer completes/fires. */ export function getNextInChain( chains: TimerChain[], completedTimerId: string ): TimerLink | null { for (const chain of chains) { const link = chain.links.find((l) => l.fromId === completedTimerId); if (link) return link; } return null; } /** * Find the chain a timer belongs to. */ export function findChainForTimer( chains: TimerChain[], timerId: string ): TimerChain | null { return chains.find((c) => c.timerIds.includes(timerId)) ?? null; } /** * Get the position of a timer within its chain. * Returns null if timer is not in any chain. */ export function getChainPosition( chains: TimerChain[], timerId: string ): { chain: TimerChain; index: number; total: number } | null { const chain = findChainForTimer(chains, timerId); if (!chain) return null; const index = chain.timerIds.indexOf(timerId); return { chain, index, total: chain.timerIds.length }; } /** * Get all timers in a chain with their labels, for visualization. */ export function getChainTimers( chain: TimerChain, allTimers: Timer[] ): (Timer | null)[] { return chain.timerIds.map((id) => allTimers.find((t) => t.id === id) ?? null); } /** * Check if cancelling a timer should prompt to cancel the whole chain. */ export function hasDownstreamTimers( chains: TimerChain[], timerId: string ): boolean { const chain = findChainForTimer(chains, timerId); if (!chain) return false; const idx = chain.timerIds.indexOf(timerId); return idx < chain.timerIds.length - 1; } /** * Get all downstream timer IDs (timers that follow this one in chain). */ export function getDownstreamTimerIds( chains: TimerChain[], timerId: string ): string[] { const chain = findChainForTimer(chains, timerId); if (!chain) return []; const idx = chain.timerIds.indexOf(timerId); if (idx === -1) return []; return chain.timerIds.slice(idx + 1); } // ── Built-in Chain Presets ──────────────────────────────────── export interface ChainPreset { name: string; steps: { label: string; durationMs: number }[]; } export const CHAIN_PRESETS: ChainPreset[] = [ { name: 'Pasta Timer', steps: [ { label: 'Boil Water', durationMs: 10 * 60_000 }, { label: 'Cook Pasta', durationMs: 12 * 60_000 }, { label: 'Prepare Sauce', durationMs: 8 * 60_000 }, ], }, { name: 'Laundry', steps: [ { label: 'Washer Cycle', durationMs: 45 * 60_000 }, { label: 'Transfer to Dryer', durationMs: 5 * 60_000 }, { label: 'Dryer Cycle', durationMs: 60 * 60_000 }, ], }, { name: 'Meeting Prep', steps: [ { label: 'Review Notes', durationMs: 10 * 60_000 }, { label: 'Get Ready', durationMs: 5 * 60_000 }, { label: 'Travel / Join Call', durationMs: 5 * 60_000 }, ], }, ];