// App List content-script anti-inspect — runs on groupass + groupy.id.
//
// Why this exists on top of the SW-level (chrome.debugger) navigation hooks:
//   - The SW hooks (webNavigation.onBeforeNavigate / tabs.onUpdated) only fire
//     when a protected page NAVIGATES. They catch "open DevTools FIRST, then
//     navigate", but are BLIND to "page already loaded, THEN open DevTools" —
//     which is why App List only closed the tab on refresh.
//   - groupy.id is NOT served by our backend, so it has no page-side
//     anti-inspect script of its own. This is how we protect it.
//
// Mechanism: a HEARTBEAT. Only the service worker can call
// chrome.debugger.getTargets() — the authoritative, dock-agnostic "is a
// debugger attached to this tab" check (the same one that already makes App
// List close the tab on refresh). A content script can't call it, but it CAN
// poll the SW. So every ~1.5s we ping the SW; the SW re-runs getTargets() for
// THIS tab and closes it if DevTools is attached. That closes the
// "open page, then inspect" gap reliably, on groupy.id too.
//
// A passive window-dimension check also drops a local cover instantly for
// DOCKED DevTools so there's immediate feedback before the SW kill lands.
//
// Note: content scripts run in the isolated world, so the page can't read or
// disable this. To inspect while developing, disable the App List extension.
(function () {
  "use strict";

  // Guard against double-execution: the manifest content_scripts entry AND
  // the SW's programmatic chrome.scripting injection (for already-open tabs)
  // both target this same isolated world, so they share `window`.
  if (window.__GP_AI_CS_LOADED__) return;
  window.__GP_AI_CS_LOADED__ = true;

  // ---- Block the obvious "open DevTools" affordances --------------------
  // Best-effort friction (same as the groupass page-side script, now on
  // groupy.id too): swallow F12, the inspect/console/view-source shortcuts,
  // and the right-click context menu (kills its "Inspect" entry). This is NOT
  // absolute — DevTools can still be opened from the browser ⋮ menu — but the
  // heartbeat below is the backstop that closes the tab if it's opened anyway.
  try {
    window.addEventListener(
      "keydown",
      function (e) {
        var k = (e.key || "").toLowerCase();
        var code = e.keyCode || e.which || 0;
        var ctrl = e.ctrlKey || e.metaKey;
        var shift = e.shiftKey;
        if (
          code === 123 || // F12
          k === "f12" ||
          (ctrl && shift && (k === "i" || k === "j" || k === "c" || k === "k")) ||
          (ctrl && k === "u") || // view-source
          (ctrl && k === "s") // save-as
        ) {
          e.preventDefault();
          e.stopPropagation();
          return false;
        }
      },
      true
    );
    window.addEventListener(
      "contextmenu",
      function (e) {
        e.preventDefault();
        e.stopPropagation();
        return false;
      },
      true
    );
  } catch (_) {}

  var OVERLAY_ID = "__gp_ai_cs_overlay__";
  var hidden = false;

  function applyHide() {
    if (hidden) return;
    hidden = true;
    try {
      document.documentElement.style.setProperty("visibility", "hidden", "important");
    } catch (_) {}
    try {
      if (!document.getElementById(OVERLAY_ID)) {
        var el = document.createElement("div");
        el.id = OVERLAY_ID;
        el.setAttribute(
          "style",
          "position:fixed!important;inset:0!important;z-index:2147483647!important;" +
            "background:#0f172a!important;"
        );
        (document.documentElement || document.body).appendChild(el);
      }
    } catch (_) {}
  }

  function clearHide() {
    if (!hidden) return;
    hidden = false;
    try {
      document.documentElement.style.removeProperty("visibility");
    } catch (_) {}
    try {
      var el = document.getElementById(OVERLAY_ID);
      if (el && el.parentNode) el.parentNode.removeChild(el);
    } catch (_) {}
  }

  // Passive docked-DevTools hint → instant local cover (reversible).
  function dimsOpen() {
    try {
      return (
        window.outerWidth - window.innerWidth > 160 ||
        window.outerHeight - window.innerHeight > 160
      );
    } catch (_) {
      return false;
    }
  }

  // Heartbeat → SW runs the authoritative getTargets() check + tab-kill.
  function ping() {
    try {
      chrome.runtime.sendMessage({ type: "GP_AI_PING" });
    } catch (_) {}
  }

  function tick() {
    if (dimsOpen()) applyHide();
    else clearHide();
    ping();
  }

  setInterval(tick, 1500);
  tick();
})();
