import { useEffect, useState } from 'react'
import { useStore } from './store/useStore.js'
import { useUI } from './store/useUI.js'
import { EXDB, EXIDX, BODYPARTS, isCardio, allExercises } from './lib/exercises.js'
import { fmtDate, fmtNum, fmtVol, fmtDur, todayISO, uid, DAYN, MONTHS_LONG, ACCENTS } from './lib/format.js'
import { lastEntryFor, bestWeightFor, buildSets, effectiveRoutineId, workoutVolume, setsDone, setsDoneActive, lastBW, supersetUnits, unitOf, setLabel, defaultConfig, cleanupSg } from './lib/history.js'
import { beep, vibrate } from './lib/sound.js'
import { t, instrFor, getLang, INSTR_LANGS } from './lib/i18n.js'
import { nav } from './lib/nav.js'
import Media, { Thumb } from './components/Media.jsx'
import Stepper from './components/Stepper.jsx'
const S = () => useStore.getState().S
const update = (...a) => useStore.getState().update(...a)
const ui = () => useUI.getState()
const toast = m => ui().toast(m)
const snd = () => S().sound
/* ============================ custom confirm dialog ============================ */
function ConfirmDialog({ title, message, confirmText, cancelText, danger, onConfirm, close }) {
return
{title &&
{title}
}
{message}
}
// Themed replacement for window.confirm β callback-based (no blocking).
export function confirmSheet(opts) {
ui().openSheet(close => , { kind: 'center' })
}
/* ============================ starter plan ============================ */
export function loadStarterPlan() {
const mk = (name, emoji, list) => ({ id: uid(), name, emoji, ex: list.map(([id, sets, reps]) => ({ id, sets, reps, weight: 0 })) })
update(st => {
const push = mk('Push Day', 'π«Έ', [['0025', 4, 8], ['0047', 3, 10], ['0426', 3, 10], ['0334', 3, 12], ['0241', 3, 12], ['0251', 3, 10]])
const pull = mk('Pull Day', 'π«·', [['2330', 4, 10], ['0027', 4, 8], ['1323', 3, 10], ['0031', 3, 10], ['0313', 3, 12]])
const legs = mk('Leg Day', 'π¦΅', [['0043', 4, 8], ['0085', 3, 10], ['0739', 3, 12], ['0585', 3, 12], ['0586', 3, 12], ['0605', 4, 15]])
st.routines.push(push, pull, legs)
st.week[1] = push.id; st.week[3] = pull.id; st.week[5] = legs.id
})
toast(t('Starter plan loaded β Mon Push Β· Wed Pull Β· Fri Legs'))
}
/* ============================ weight picker (shared: body weight + goal) ============================ */
// Fixed range, not a moving window β a window that resizes itself mid-drag (the previous
// attempt) makes the thumb's position unpredictable: every time it grows, everything already
// placed on it shifts toward one side. A static range never has that problem, at the cost of
// coarser precision per pixel β the +/- buttons cover exact values.
const W_LO = 1, W_HI = 300
function WeightInput({ value, setValue, unit }) {
const clamp = x => Math.max(W_LO, Math.min(W_HI, Math.round((x || 0) * 10) / 10))
const sv = Math.max(W_LO, Math.min(W_HI, value))
const pct = ((sv - W_LO) / (W_HI - W_LO)) * 100
const onSlide = v => setValue(clamp(v))
return <>
{fmtNum(value)} {unit}
onSlide(parseFloat(e.target.value))}
style={{ '--track': `linear-gradient(90deg, var(--acc) ${pct}%, var(--bg2) ${pct}%)` }} />
>
}
/* ============================ body weight ============================ */
function BwSheet({ required, onDone, close }) {
const st = useStore(s => s.S)
const unit = st.unit
const bw = lastBW(st)
const [v, setV] = useState(bw ? bw.w : 70)
const save = () => {
const n = Math.round((v || 0) * 10) / 10
if (!n || n <= 0) { toast(t('Enter a valid weight')); return }
update(s => {
const iso = todayISO()
const ex = s.bodyweight.find(b => b.d === iso)
if (ex) { ex.w = n; ex.t = Date.now() } else s.bodyweight.push({ d: iso, w: n, t: Date.now() })
s.bodyweight.sort((a, b) => (a.d < b.d ? -1 : 1))
})
close()
if (onDone) onDone(n); else toast(t('Weight saved β'))
}
const recent = [...st.bodyweight].reverse().slice(0, 3)
const delEntry = d => update(s => { s.bodyweight = s.bodyweight.filter(b => b.d !== d) })
return <>
{required ? t('Quick check-in βοΈ') : t('Log body weight')}
{required ? t('Slide or tap to set your weight β tracked before every workout so your curve stays honest.') : t('Today') + ', ' + fmtDate(todayISO(), true)}