(function () { const onboardingBridgeState = { chapters: null, pending: null, }; function getPuzzleModalManagerClass() { if (typeof PuzzleModalManager !== 'undefined') { return PuzzleModalManager; } if (typeof window.PuzzleModalManager !== 'undefined') { return window.PuzzleModalManager; } return null; } function ensureDependencies() { if (!getPuzzleModalManagerClass()) { throw new Error('PuzzleModalManager is not loaded'); } } async function loadOnboardingChapters() { if (Array.isArray(onboardingBridgeState.chapters)) { return onboardingBridgeState.chapters; } if (onboardingBridgeState.pending) { return onboardingBridgeState.pending; } onboardingBridgeState.pending = fetch('/bbs/api/get_onboarding_chapters.php', { credentials: 'same-origin', }) .then(async function (response) { const payload = await response.json(); if (!response.ok || !payload || payload.success !== true || !Array.isArray(payload.chapters)) { throw new Error(payload?.message || 'failed_to_load_onboarding_chapters'); } onboardingBridgeState.chapters = payload.chapters; window.puzzleChapterData = payload.chapters; return payload.chapters; }) .finally(function () { onboardingBridgeState.pending = null; }); return onboardingBridgeState.pending; } function cloneChapter(chapter) { return JSON.parse(JSON.stringify(chapter || {})); } function findChapterIndexByContentId(chapters, contentId) { const id = String(contentId || ''); if (id === '') { return -1; } return chapters.findIndex(function (chapter) { const lessons = Array.isArray(chapter?.lessons) ? chapter.lessons : []; return lessons.some(function (lesson) { return String(lesson?.content_id || '') === id; }); }); } function applyBridgeOverrides(chapter, info) { const contentId = String(info?.contentId || ''); if (!contentId || !Array.isArray(chapter?.lessons)) { return chapter; } chapter.lessons = chapter.lessons.map(function (lesson) { if (String(lesson?.content_id || '') !== contentId) { return lesson; } return { ...lesson, watch_tm: Math.max(Number(lesson?.watch_tm || 0), Number(info?.watchTm || 0)), content_tm: Math.max(Number(lesson?.content_tm || 0), Number(info?.contentTm || 0)), is_bookmarked: info?.bookmark === true ? true : lesson?.is_bookmarked === true, }; }); return chapter; } window._onboardingOpenModal = async function (info) { ensureDependencies(); const PuzzleModalManagerClass = getPuzzleModalManagerClass(); const chapters = await loadOnboardingChapters(); const chapterIndex = findChapterIndexByContentId(chapters, info?.contentId); if (chapterIndex < 0) { throw new Error('onboarding chapter not found'); } const chapter = applyBridgeOverrides(cloneChapter(chapters[chapterIndex]), info); await PuzzleModalManagerClass.openChapterModal(chapterIndex, chapter); }; })();