// BENCH-HIDDEN — эталонное решение задачи 4. import { collection } from "./lib/store.mjs"; const patients = collection("patients"); export function normalizePhone(raw) { const d = String(raw ?? "").replace(/[\s()-]/g, ""); return /^\+7\d{10}$/.test(d) ? d : null; } export function parseBirthDate(raw, today = new Date()) { const s = String(raw ?? "").trim(); if (!s) return { value: null }; const m = s.match(/^(\d{4})-(\d{2})-(\d{2})$/); if (!m) return { error: "Укажите дату в виде ДД.ММ.ГГГГ." }; const d = new Date(Date.UTC(+m[1], +m[2] - 1, +m[3])); if (d.getUTCFullYear() !== +m[1] || d.getUTCMonth() !== +m[2] - 1 || d.getUTCDate() !== +m[3]) return { error: "Такой даты нет в календаре." }; if (d > today) return { error: "Дата рождения не может быть в будущем." }; return { value: s }; } export function formatBirthDate(v) { if (!v) return "—"; const [y, mo, d] = v.split("-"); return `${d}.${mo}.${y}`; } export function validatePatient(b) { const errors = {}; const name = String(b.name ?? "").trim(); if (name.length < 2) errors.name = "Укажите имя и фамилию."; const phone = normalizePhone(b.phone); if (!phone) errors.phone = "Укажите телефон в виде +7 и 10 цифр."; const birth = parseBirthDate(b.birthDate); if (birth.error) errors.birthDate = birth.error; return { errors, value: { name, phone, birthDate: birth.value ?? null } }; } export function addPatient(fields) { return patients.add(fields); } export function listPatients() { return patients.all(); }