/* global usScheduler */ (function () { 'use strict'; const app = document.getElementById('us-booking-app'); if (!app) return; const slotList = document.getElementById('us-slot-list'); const confirm = document.getElementById('us-booking-confirmation'); const errorBox = document.getElementById('us-booking-error'); const { restUrl, nonce } = usScheduler; function apiFetch(path, options = {}) { return fetch(restUrl + path, { ...options, headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': nonce, ...(options.headers || {}), }, }).then(async (res) => { const data = await res.json(); if (!res.ok) throw new Error(data.message || 'Request failed'); return data; }); } function showError(message) { errorBox.textContent = message; errorBox.style.display = 'block'; } function clearError() { errorBox.style.display = 'none'; } function escHtml(str) { return String(str) .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"'); } const dayKey = (dt) => String(dt).slice(0, 10); // "2026-07-06 14:30:00" → "2:30 PM" function timeOf(dt) { const hours = Number(String(dt).slice(11, 13)); const minutes = String(dt).slice(14, 16); return `${hours % 12 || 12}:${minutes} ${hours < 12 ? 'AM' : 'PM'}`; } function dayLabel(key) { const date = new Date(key + 'T00:00:00'); if (Number.isNaN(date.getTime())) return key; return date.toLocaleDateString(undefined, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', }); } function shortDayLabel(key) { const date = new Date(key + 'T00:00:00'); if (Number.isNaN(date.getTime())) return key; return date.toLocaleDateString(undefined, { weekday: 'short', month: 'short', day: 'numeric' }); } function groupByDay(slots) { const groups = new Map(); slots.forEach((slot) => { const key = dayKey(slot.start_dt); if (!groups.has(key)) groups.set(key, []); groups.get(key).push(slot); }); return [...groups.entries()].sort((a, b) => a[0].localeCompare(b[0])); } // --- calendar view state (list is the default; week keeps its position) --- let allSlots = []; let view = 'list'; let weekStart = null; const pad = (n) => String(n).padStart(2, '0'); const toKey = (d) => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`; function addDays(key, days) { const date = new Date(key + 'T00:00:00'); date.setDate(date.getDate() + days); return toKey(date); } // First day of the week containing `key`, honouring the site's // start-of-week setting (0 = Sunday … 6 = Saturday). function weekStartOf(key) { const startOfWeek = Number(usScheduler.startOfWeek) || 0; const date = new Date(key + 'T00:00:00'); return addDays(key, -((date.getDay() - startOfWeek + 7) % 7)); } function toggleHtml() { return `
No available lesson slots at this time.
'; return; } slotList.innerHTML = toggleHtml() + (view === 'week' ? weekHtml() : listHtml()); wireCalendarEvents(); } function wireCalendarEvents() { document.getElementById('us-view-list').addEventListener('click', () => { view = 'list'; render(); }); document.getElementById('us-view-week').addEventListener('click', () => { view = 'week'; // Default to the week of the earliest open slot (the API returns // slots ordered by start), so the first look is never empty. if (!weekStart) weekStart = weekStartOf(dayKey(allSlots[0].start_dt)); render(); }); const prev = document.getElementById('us-week-prev'); const next = document.getElementById('us-week-next'); if (prev) prev.addEventListener('click', () => { weekStart = addDays(weekStart, -7); render(); }); if (next) next.addEventListener('click', () => { weekStart = addDays(weekStart, 7); render(); }); slotList.querySelectorAll('.us-book-btn[data-slot-id]').forEach((btn) => { const slot = allSlots.find((s) => String(s.id) === btn.dataset.slotId); if (slot) btn.addEventListener('click', () => openRegistration(slot)); }); } function questionField(q) { const name = `q_${q.id}`; const required = q.is_required ? 'required' : ''; let input; if (q.field_type === 'textarea') { input = ``; } else if (q.field_type === 'select') { const opts = (q.options || []).map((o) => ``).join(''); input = ``; } else if (q.field_type === 'checkbox') { input = ``; } else { input = ``; } return ``; } function policyField(p) { return `