// Goal Power Dialer — dashboard bridge
//
// Loaded into the dashboard page (https://app.goalpowerdialer.com/dashboard)
// at document_start in the MAIN world. Its only job is to tell the
// dashboard JS that the extension is installed, so the install card
// can flip from "Install" to "You're all set — sign in".
//
// Why MAIN world? Content scripts in the isolated world have a
// separate `window` object — they can't set window flags the page's
// own JS can read. MAIN-world content scripts share the page's
// `window`. We don't need any chrome.* APIs here other than
// chrome.runtime.id, which IS exposed to MAIN-world scripts.

(function () {
  "use strict";
  if (window.__pdExtensionDetected) return;
  try {
    const id = (typeof chrome !== "undefined" && chrome.runtime && chrome.runtime.id) || null;
    if (!id) return;
    window.__pdExtensionDetected = {
      id: id,
      detectedAt: Date.now(),
    };
    // Fire a CustomEvent on document so dashboard JS that's already
    // listening can react immediately. (Document is shared across
    // worlds, so this works even if a future version of the bridge
    // moves to the isolated world.)
    document.dispatchEvent(new CustomEvent("pd-extension-detected", {
      detail: { id: id },
    }));
  } catch (_e) {
    // Swallow — failing detection just means the dashboard shows the
    // install button, which is the safe default.
  }
})();
