import { translate, loadLocale } from "../preview/i18n.js"; import { createLessonTutorial, completedTurns, } from "../src/tutorial-controller.js"; import { LESSONS } from "../src/tutorials.js"; import { snapshot } from "../src/view.js"; import { mountConsole } from "../preview/app.js"; import { pictogram } from "../preview/icons.js"; import { tutorialTextHtml } from "../preview/tutorial-text.js"; import { tutorialMissionHeader, updateTutorialMissionProgress } from "../preview/tutorial-header.js"; import { energyDiscovery, ENERGY_DEMOS } from "../preview/tutorial-energy.js"; import { tutorialPhaseButton } from "../preview/tutorial-program.js"; import { duration } from "../preview/clock.js"; import { createSoundDesign } from "../preview/sound.js"; import html from "../preview/index.html"; import css from "../preview/style.css"; export async function mountLesson( target, { chapter = "basics", locale = "en", onProgress, nextChapter, standalone = false, } = {}, ) { locale = await loadLocale(locale); const t = (key, params) => translate(locale, key, params); const index = LESSONS.findIndex((l) => l.id === chapter); if (index < 0) throw Error("Unknown tutorial chapter"); const sound = createSoundDesign(); let soundOn = true, disposed = false, lastStep = -1, lastTurn = 0, lastTick = performance.now(); let energyDemo = 0; // Local presentation only; never a controller checkpoint. let playing = false, previousStage, previousPhase, previousThreats, playbackBusy = false; const shotExerciseEnded = (s) => chapter === "first-shot" && s.state.tutorial.stopAtTarget && s.state.game.resolution?.turn >= 3 && ["threats", "computer", "appear"].includes(s.state.game.resolution.step); const canPlay = (s) => !shotExerciseEnded(s) && !(chapter === 'energy' && completedTurns(s.state) >= 4) && s.state.stage === "resolution" && !s.completed && !s.state.game.resolution?.outcome && !(chapter === "basics" && completedTurns(s.state) >= 3); let storage; try { storage = window.localStorage; soundOn = storage.getItem("space-alert:tutorial-sound") !== "off"; } catch {} // Unlock audio on the first real interaction, respecting browser autoplay rules. const unlockSound = () => sound.enable(soundOn); target.ownerDocument.addEventListener("pointerdown", unlockSound, true); target.ownerDocument.addEventListener("keydown", unlockSound, true); const tutorial = await createLessonTutorial(chapter, { locale, storage, onProgress, // Keep the closed action tokens until the native guide renders them safely. formatText: (text) => text, }); target.innerHTML = html .split("")[1] .split('