"use client"; /** * v1.15.18 — per-dose configurable on-time WINDOW editor (Marc's headline * lever). Shared verbatim by the Zeitplan tab (everyday inline edit) and * the wizard's Step 7 (structural create / edit) so the window semantics * are identical wherever a dose time is set. * * Per dose time the user expresses EITHER a point time (e.g. 19:00 → * implies the ±1h default band) OR an explicit RANGE (e.g. 07:00–09:00). * The editor shows the resulting on-time band plus a cadence-aware * "verspätet bis …" hint derived from the engine's late tail. It is a * controlled component over the persisted `DoseWindowEntry[]` contract * (`{ timeOfDay, start, end }`), validated client-side to match the W7 * zod: HH:mm, `start <= end`, `timeOfDay ∈ timesOfDay`. * * Calm, inset-grouped, AA. No card tint — the surface follows the muted * section chrome the other Zeitplan / Erinnerung rows use. */ import { useCallback, useId, useMemo } from "react"; import { Input } from "@/components/ui/input"; import { Switch } from "@/components/ui/switch"; import { Label } from "@/components/ui/label"; import { useTranslations } from "@/lib/i18n/context"; import { buildRows, defaultBandForTime, isOrderedRange, isValidHhmm, lateTailDays, lateTailEndHhmm, rowsToEntries, type DoseWindowEntry, type DoseWindowRow, type DoseWindowScale, } from "./dose-window"; export interface DoseWindowEditorProps { /** The schedule's dose times (HH:mm) — one editor row per time. */ timesOfDay: string[]; /** The persisted explicit windows for this schedule. */ value: DoseWindowEntry[]; /** Emits the next explicit-window set (point-equivalent rows dropped). */ onChange: (next: DoseWindowEntry[]) => void; /** * Cadence scale — drives the late-tail hint. `intraday` (daily / * multi-daily) shows a wall-clock late end; `dayScale` (weekly / * rolling) shows the day-count tail. Defaults to `intraday`. */ scale?: DoseWindowScale; /** Disables every control (e.g. while a save is in flight). */ disabled?: boolean; /** Stable id-prefix for the rendered inputs (defaults to a generated id). */ idPrefix?: string; } export function DoseWindowEditor({ timesOfDay, value, onChange, scale = "intraday", disabled = false, idPrefix, }: DoseWindowEditorProps) { const { t } = useTranslations(); const generatedId = useId(); const prefix = idPrefix ?? generatedId; const rows = useMemo(() => buildRows(timesOfDay, value), [timesOfDay, value]); // Rebuild the persisted set from the next row state and bubble it up. const emit = useCallback( (nextRows: DoseWindowRow[]) => { onChange(rowsToEntries(nextRows)); }, [onChange], ); const setRow = useCallback( (timeOfDay: string, patch: Partial) => { emit( rows.map((r) => (r.timeOfDay === timeOfDay ? { ...r, ...patch } : r)), ); }, [emit, rows], ); const toggleCustom = useCallback( (row: DoseWindowRow, custom: boolean) => { if (custom) { // Switching ON seeds the range from the current default band so // the user starts from the same band the engine would derive. const def = defaultBandForTime(row.timeOfDay); setRow(row.timeOfDay, { custom: true, start: def.start, end: def.end }); } else { setRow(row.timeOfDay, { custom: false }); } }, [setRow], ); if (rows.length === 0) { return (

{t("medications.detail.zeitplan.window.noTimes")}

); } return (

{t("medications.detail.zeitplan.window.intro")}

); }