"use client"; import { useState, useMemo } from "react"; import Image from "next/image"; import { Calendar, Clock, User, MapPin, Instagram } from "lucide-react"; import { SectionHeading } from "@/components/ui/SectionHeading"; import { Reveal } from "@/components/ui/Reveal"; import { SignupModal } from "@/components/ui/SignupModal"; import type { SiteContent, MasterClassItem, MasterClassSlot } from "@/types"; interface MasterClassesProps { data: SiteContent["masterClasses"]; } const MONTHS_RU = [ "января", "февраля", "марта", "апреля", "мая", "июня", "июля", "августа", "сентября", "октября", "ноября", "декабря", ]; const WEEKDAYS_RU = [ "воскресенье", "понедельник", "вторник", "среда", "четверг", "пятница", "суббота", ]; function parseDate(iso: string) { return new Date(iso + "T00:00:00"); } function formatSlots(slots: MasterClassSlot[]): string { if (slots.length === 0) return ""; const sorted = [...slots].sort( (a, b) => parseDate(a.date).getTime() - parseDate(b.date).getTime() ); const dates = sorted.map((s) => parseDate(s.date)).filter((d) => !isNaN(d.getTime())); if (dates.length === 0) return ""; const timePart = sorted[0].startTime ? `, ${sorted[0].startTime}–${sorted[0].endTime}` : ""; if (dates.length === 1) { const d = dates[0]; return `${d.getDate()} ${MONTHS_RU[d.getMonth()]} (${WEEKDAYS_RU[d.getDay()]})${timePart}`; } const sameMonth = dates.every((d) => d.getMonth() === dates[0].getMonth()); const sameWeekday = dates.every((d) => d.getDay() === dates[0].getDay()); if (sameMonth) { const days = dates.map((d) => d.getDate()).join(" и "); const weekdayHint = sameWeekday ? ` (${WEEKDAYS_RU[dates[0].getDay()]})` : ""; return `${days} ${MONTHS_RU[dates[0].getMonth()]}${weekdayHint}${timePart}`; } const parts = dates.map((d) => `${d.getDate()} ${MONTHS_RU[d.getMonth()]}`); return parts.join(", ") + timePart; } function calcDuration(slot: MasterClassSlot): string { if (!slot.startTime || !slot.endTime) return ""; const [sh, sm] = slot.startTime.split(":").map(Number); const [eh, em] = slot.endTime.split(":").map(Number); const mins = (eh * 60 + em) - (sh * 60 + sm); if (mins <= 0) return ""; const h = Math.floor(mins / 60); const m = mins % 60; if (h > 0 && m > 0) return `${h} ч ${m} мин`; if (h > 0) return h === 1 ? "1 час" : h < 5 ? `${h} часа` : `${h} часов`; return `${m} мин`; } function isUpcoming(item: MasterClassItem): boolean { const now = new Date(); const slots = item.slots ?? []; if (slots.length === 0) return false; // Series MC: check earliest slot — if first session passed, group already started const earliestSlot = slots.reduce((min, s) => s.date < min.date ? s : min, slots[0]); const d = parseDate(earliestSlot.date); if (earliestSlot.startTime) { const [h, m] = earliestSlot.startTime.split(":").map(Number); d.setHours(h, m, 0, 0); } else { d.setHours(23, 59, 59, 999); } return d > now; } function MasterClassCard({ item, onSignup, }: { item: MasterClassItem; onSignup: () => void; }) { const duration = item.slots[0] ? calcDuration(item.slots[0]) : ""; const slotsDisplay = formatSlots(item.slots); return (
{/* Full-bleed image or placeholder */}
{item.image ? ( <> {item.title}
) : (
)}
{/* Content overlay at bottom */}
{/* Tags row */}
{item.style} {duration && ( {duration} )}
{/* Title */}

{item.title}

{/* Trainer */}
{item.trainer}
{/* Divider */}
{/* Date + Location */}
{slotsDisplay}
{item.location && (
{item.location}
)}
{/* Price + Actions */}
{item.instagramUrl && ( )}
{/* Price floating tag */}
{item.cost}
); } export function MasterClasses({ data }: MasterClassesProps) { const [signupTitle, setSignupTitle] = useState(null); const upcoming = useMemo(() => { return data.items .filter(isUpcoming) .sort((a, b) => { const aFirst = parseDate(a.slots[0]?.date ?? ""); const bFirst = parseDate(b.slots[0]?.date ?? ""); return aFirst.getTime() - bFirst.getTime(); }); }, [data.items]); return (
{data.title} {upcoming.length === 0 ? (

Следите за анонсами мастер-классов в нашем{" "} Instagram

) : (
{upcoming.map((item) => ( setSignupTitle(item.title)} /> ))}
)}
setSignupTitle(null)} subtitle={signupTitle ?? ""} endpoint="/api/master-class-register" extraBody={{ masterClassTitle: signupTitle }} successMessage={data.successMessage} />
); }