/* WAM · main.js — dueño D (núcleo).
Bootstrap: carga el estado, aplica tema, renderiza chat y editor,
y enlaza la delegación de clics del chat, el composer, la reproducción
de la demo (velocidad, reiniciar chat) y la toolbar. Único script con
ejecución al cargar. */
window.WAM = window.WAM || {};
(function () {
"use strict";
function porId(id) {
return document.getElementById(id);
}
function al(id, evento, manejador) {
var el = porId(id);
if (el) el.addEventListener(evento, manejador);
}
/* ————————————————— aviso visible (toast) ————————————————— */
/* Aviso flotante accesible. Estilos por CSSOM (compatibles con una CSP
style-src 'self'); no depende de ningún CSS ajeno. */
var avisoTimer = null;
WAM.avisar = function (texto, opciones) {
var op = opciones || {};
var caja = porId("wam-aviso");
if (!caja) {
caja = document.createElement("div");
caja.id = "wam-aviso";
caja.setAttribute("role", "alert");
caja.setAttribute("aria-live", "assertive");
var s = caja.style;
s.position = "fixed";
s.left = "50%";
s.bottom = "20px";
s.transform = "translateX(-50%)";
s.zIndex = "9999";
s.maxWidth = "min(560px, calc(100vw - 32px))";
s.padding = "12px 40px 12px 16px";
s.borderRadius = "12px";
s.font = "500 14px/1.4 -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif";
s.boxShadow = "0 8px 28px rgba(0,0,0,.28)";
var cerrar = document.createElement("button");
cerrar.type = "button";
cerrar.setAttribute("aria-label", "Cerrar aviso");
cerrar.textContent = "×";
var c = cerrar.style;
c.position = "absolute";
c.top = "6px";
c.right = "8px";
c.border = "0";
c.background = "transparent";
c.color = "inherit";
c.font = "600 20px/1 Arial, sans-serif";
c.cursor = "pointer";
cerrar.addEventListener("click", function () { caja.hidden = true; });
var txt = document.createElement("span");
txt.className = "wam-aviso-texto";
caja.appendChild(txt);
caja.appendChild(cerrar);
document.body.appendChild(caja);
}
var error = op.tipo !== "info";
caja.style.background = error ? "#b3261e" : "#1f2c33";
caja.style.color = "#fff";
caja.querySelector(".wam-aviso-texto").textContent = String(texto == null ? "" : texto);
caja.hidden = false;
if (avisoTimer) clearTimeout(avisoTimer);
avisoTimer = null;
if (op.duracion) {
avisoTimer = setTimeout(function () { caja.hidden = true; }, op.duracion);
}
};
/* ————————————————— botón "Reproducir demo" ————————————————— */
var SVG_STOP = '';
/* Contador discreto «Paso 3/12» junto al botón (se crea una vez). */
function contadorDemo(btn) {
var c = porId("demo-paso");
if (c) return c;
c = document.createElement("span");
c.id = "demo-paso";
c.className = "demo-paso";
c.setAttribute("aria-live", "polite");
c.setAttribute("role", "status");
c.hidden = true;
/* estilo mínimo en línea (tabular) mientras no haya regla en iphone.css */
c.style.fontSize = "12px";
c.style.fontVariantNumeric = "tabular-nums";
c.style.opacity = "0.75";
c.style.whiteSpace = "nowrap";
btn.parentNode.insertBefore(c, btn);
return c;
}
function pintarBotonDemo() {
var btn = porId("btn-reproducir");
if (!btn) return;
var activa = WAM.demoActiva();
btn.setAttribute("aria-pressed", activa ? "true" : "false");
var svg = btn.querySelector("svg");
if (svg) {
if (!svg.dataset.play) svg.dataset.play = svg.innerHTML;
var deseado = activa ? SVG_STOP : svg.dataset.play;
if (svg.innerHTML !== deseado) svg.innerHTML = deseado;
}
var span = btn.querySelector("span");
if (span) {
if (!span.dataset.etiqueta) span.dataset.etiqueta = span.textContent;
span.textContent = activa ? "Detener" : span.dataset.etiqueta;
}
if (btn.parentNode) {
var c = contadorDemo(btn);
var p = typeof WAM.progresoDemo === "function" ? WAM.progresoDemo() : null;
if (activa && p && p.total > 0 && p.paso > 0) {
var txt = "Paso " + p.paso + "/" + p.total;
if (c.textContent !== txt) c.textContent = txt;
c.hidden = false;
} else {
c.hidden = true;
c.textContent = "";
}
}
}
/* Evita que un doble clic arranque y detenga la demo en el mismo gesto. */
var ultimoReproducir = 0;
function alReproducir() {
var ahora = Date.now();
if (ahora - ultimoReproducir < 350) return;
ultimoReproducir = ahora;
if (WAM.demoActiva()) WAM.detenerDemo();
else WAM.reproducirDemo();
pintarBotonDemo();
}
function alReiniciarChat() {
if (typeof WAM.reiniciarChat === "function") WAM.reiniciarChat();
else WAM.render();
pintarBotonDemo();
}
function alVelocidad(e) {
var v = WAM.setVelocidad(e.currentTarget.value);
e.currentTarget.value = String(v);
}
/* ————————————————— toolbar ————————————————— */
function alTema() {
WAM.estado.tema = WAM.estado.tema === "oscuro" ? "claro" : "oscuro";
WAM.guardar();
WAM.setTema(WAM.estado.tema);
}
function alPresentacion() {
document.body.classList.toggle("presentacion");
var btn = porId("btn-presentacion");
if (btn) {
btn.setAttribute("aria-pressed",
document.body.classList.contains("presentacion") ? "true" : "false");
}
}
function reRenderTotal() {
paso(function () { WAM.setTema(WAM.estado.tema); });
paso(function () { WAM.render(); });
paso(function () { WAM.renderEditor(); });
pintarBotonDemo();
}
function alExportar() {
if (typeof WAM.guardarYa === "function") WAM.guardarYa();
if (typeof WAM.exportarJSON === "function") WAM.exportarJSON();
}
function alImportar() {
var abrir = function () {
var archivo = porId("importar-archivo");
if (archivo) archivo.click();
};
/* Hook opcional de js/ux.js: