/*
  APP — shared chrome for every page: footer API label, toast notifications.
  (Settings moved to its own page: settings.html)
*/
(function () {
  'use strict';

  function initChrome() {
    const label = document.getElementById('apiBaseLabel');
    if (label) {
      const base = SR.config.getBase();
      label.textContent = base;
      label.href = base;
    }
  }

  let toastTimer = null;

  function toast(message, isError) {
    let el = document.getElementById('toast');
    if (!el) {
      el = document.createElement('div');
      el.id = 'toast';
      el.className = 'toast';
      el.setAttribute('role', 'status');
      document.body.appendChild(el);
    }
    el.textContent = message;
    el.classList.toggle('toast--error', !!isError);
    el.classList.add('toast--show');
    clearTimeout(toastTimer);
    toastTimer = setTimeout(() => el.classList.remove('toast--show'), 4500);
  }

  function showError(err, fallback) {
    toast(err && err.message ? err.message : fallback || 'Something went wrong', true);
  }

  window.SR = window.SR || {};
  SR.app = { initChrome, toast, showError };
})();
