/**
 * 247ChatMate — Intro Controller (LOCKED pacing, reset-safe)
 * --------------------------------------------------
 * • Plays intro bubbles on widget open
 * • Removes intro after completion
 * • Emits matewa:introComplete for flow start
 * • Rebuilds intro after hard reset without changing timing
 */
(function () {
  'use strict';

  var introTemplate = '';

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
  } else {
    init();
  }

  function init() {
    var widget = document.getElementById('matewa-widget');
    if (!widget) return;

    var firstIntro = widget.querySelector('.matewa-intro');
    if (firstIntro && !introTemplate) introTemplate = firstIntro.outerHTML;

    window.__matewaRestoreIntro = function () {
      restoreIntro(widget);
    };

    widget.addEventListener('matewa:languageReady', function () {
      applyIntroLanguage(widget);
      if (widget.classList.contains('is-open')) playIntro(widget);
    });

    widget.addEventListener('matewa:opened', function () {
      playIntro(widget);
    });

    // Safety: if widget is already open
    if (widget.classList.contains('is-open')) {
      playIntro(widget);
    }
  }

  function restoreIntro(widget) {
    var stream = widget.querySelector('.matewa-stream');
    if (!stream) return;

    stream.innerHTML = '';

    var wrap = document.createElement('div');
    wrap.innerHTML = introTemplate || '<div class="matewa-intro"><div class="matewa-bubble"></div><div class="matewa-bubble matewa-muted"></div></div>';
    var intro = wrap.firstElementChild;
    if (!intro) return;

    intro.removeAttribute('data-matewa-intro-played');
    Array.prototype.forEach.call(intro.querySelectorAll('.matewa-bubble'), function (bubble) {
      bubble.classList.remove('is-visible');
    });

    stream.appendChild(intro);
    applyIntroLanguage(widget);
  }

  function applyIntroLanguage(widget) {
    var intro = widget.querySelector('.matewa-intro');
    if (!intro) return;

    function t(key, fallback) {
      if (typeof window.matewa_get_i18n_label === 'function') {
        return window.matewa_get_i18n_label(key) || fallback;
      }
      if (window.MATEWA_I18N && window.MATEWA_I18N[key]) return window.MATEWA_I18N[key];
      return fallback;
    }

    var bubbles = intro.querySelectorAll('.matewa-bubble');
    if (bubbles[0]) bubbles[0].textContent = t('intro.line_1', bubbles[0].textContent);
    if (bubbles[1]) bubbles[1].textContent = t('intro.line_2', bubbles[1].textContent);
  }

  function playIntro(widget) {
    var intro = widget.querySelector('.matewa-intro');
    if (!intro) return;
    if (intro.getAttribute('data-matewa-intro-played') === '1') return;

    if (window.MATEWA_LANGUAGE_READY === false) {
      widget.addEventListener('matewa:languageReady', function () { playIntro(widget); }, { once: true });
      return;
    }

    intro.setAttribute('data-matewa-intro-played', '1');
    applyIntroLanguage(widget);

    var T = window.MATEWA_TEMPO || {};
    var INTRO_STEP = T.INTRO_STEP || 1750;
    var INTRO_POST_DELAY = T.INTRO_POST_DELAY || 300;
    var introBubbles = Array.prototype.slice.call(intro.querySelectorAll('.matewa-bubble'));

    introBubbles.forEach(function (bubble, i) {
      setTimeout(function () {
        bubble.classList.add('is-visible');
      }, i * INTRO_STEP);
    });

    setTimeout(function () {
      if (intro && intro.parentNode) intro.parentNode.removeChild(intro);
      widget.dispatchEvent(new CustomEvent('matewa:introComplete'));
    }, introBubbles.length * INTRO_STEP + INTRO_POST_DELAY);
  }
})();
