import * as Config from 'config' export type Step = { name: 'warmup', remaining: number } | { name: 'prepare', tabata: string, remaining: number } | { name: 'work', tabata: string, cycle: number, remaining: number } | { name: 'rest', tabata: string, cycle: number, remaining: number } | { name: 'end' } export function prettyPrint(step: Step): string { switch (step.name) { case 'warmup': return 'Warm Up' case 'prepare': return `${step.tabata}\nPreparation` case 'work': return `${step.tabata}\nWork ${step.cycle}` case 'rest': return `${step.tabata}\nRest ${step.cycle}` case 'end': return 'End!' } } export function getAt(config: Config.Config, elapsed: number): Step { if (elapsed < config.warmup) { return { name: 'warmup', remaining: config.warmup - elapsed } } const tabataElapsed = elapsed - config.warmup const cycleDuration = config.work + config.rest const tabataDuration = config.prepare + (config.cycles * cycleDuration) if (tabataElapsed >= tabataDuration * config.tabatas.length) { return { name: 'end' } } const tabata = config.tabatas[Math.floor(tabataElapsed / tabataDuration)] const currentTabataElapsed = tabataElapsed % tabataDuration if (currentTabataElapsed < config.prepare) { return { name: 'prepare', tabata, remaining: config.prepare - currentTabataElapsed } } else { const currentCycleElapsed = (currentTabataElapsed - config.prepare) % cycleDuration const cycle = currentTabataElapsed < config.prepare ? 1 : Math.floor((currentTabataElapsed - config.prepare) / cycleDuration) + 1 if (currentCycleElapsed < config.work) { return { name: 'work', tabata, cycle, remaining: config.work - currentCycleElapsed } } else { return { name: 'rest', tabata, cycle, remaining: config.work + config.rest - currentCycleElapsed } } } }