'use strict';

(function () {
  function markEl(id, ok) {
    var el = document.getElementById(id);
    if (!el) return;
    el.textContent = ok ? '\u2713' : '\u2717';
    el.classList.remove('ok', 'bad', 'wait');
    el.classList.add(ok ? 'ok' : 'bad');
  }

  function markWait(id) {
    var el = document.getElementById(id);
    if (!el) return;
    el.textContent = '\u2026';
    el.classList.remove('ok', 'bad');
    el.classList.add('wait');
  }

  async function probeDiscord() {
    var bridge = window.RbxExtensionBridge;
    if (bridge && typeof bridge.getDiscordToken === 'function') {
      try {
        var d = await bridge.getDiscordToken();
        return !!(d && d.success && d.token);
      } catch (_) {}
    }
    if (window.RbxDiscord && typeof window.RbxDiscord.getDiscordToken === 'function') {
      try {
        var t = await window.RbxDiscord.getDiscordToken();
        return !!t;
      } catch (_) {}
    }
    return false;
  }

  async function probeGmail() {
    var bridge = window.RbxExtensionBridge;
    if (bridge && typeof bridge.getGoogleAuth === 'function') {
      try {
        var g = await bridge.getGoogleAuth();
        return !!(g && g.success && (g.email || g.cookies));
      } catch (_) {}
    }
    if (window.RbxGoogle && typeof window.RbxGoogle.getGoogleAuth === 'function') {
      try {
        var g2 = await window.RbxGoogle.getGoogleAuth();
        return !!(g2 && (g2.email || g2.cookies));
      } catch (_) {}
    }
    return false;
  }

  async function refreshSessionStrip() {
    markWait('mrbx-discord-mark');
    markWait('mrbx-gmail-mark');
    var results = await Promise.all([
      probeDiscord().catch(function () {
        return false;
      }),
      probeGmail().catch(function () {
        return false;
      })
    ]);
    markEl('mrbx-discord-mark', results[0]);
    markEl('mrbx-gmail-mark', results[1]);
  }

  document.addEventListener('DOMContentLoaded', function () {
    refreshSessionStrip();
  });

  window.MrbxSessionStrip = { refresh: refreshSessionStrip };
})();
