// AdBlockMaxxer — generic cosmetic ad hider (ISOLATED world, all sites).
//
// declarativeNetRequest blocks ad *requests*; this hides the leftover ad *containers*
// (empty boxes, sticky banners, "sponsored" slots) that load from a page's own domain and
// can't be network-blocked. Curated + conservative on purpose — a short, well-known list so
// it doesn't nuke real content. Skips any site on your allowlist and obeys the popup toggle.

(function () {
  "use strict";

  const STYLE_ID = "adblockmaxxer-cosmetic";
  const SELECTORS = [
    "ins.adsbygoogle",
    ".adsbygoogle",
    '[id^="div-gpt-ad"]',
    '[id^="google_ads_"]',
    '[id^="aswift_"]',
    'iframe[src*="googlesyndication"]',
    'iframe[src*="doubleclick.net"]',
    'iframe[src*="/ads/"]',
    'iframe[id^="google_ads_iframe"]',
    'ins[data-ad-client]',
    ".ad-container",
    ".ad-wrapper",
    ".ad-banner",
    ".ad-slot",
    ".adslot",
    ".ad-unit",
    ".advertisement",
    ".advert",
    ".sponsored-content",
    '[class*="-advertisement"]',
    '[data-ad-slot]',
    '[data-adunit]',
    '[aria-label="advertisement" i]',
    ".taboola",
    "#taboola-below-article",
    '[id^="taboola-"]',
    ".trc_related_container",
    '[id^="outbrain_widget"]',
    ".OUTBRAIN",
    ".sticky-ad",
    ".leaderboard-ad",
    ".dfp-ad",
  ];

  let enabled = true;
  let allowed = true; // not allowlisted
  let snoozeUntil = 0;
  let custom = []; // extra selectors imported from the user's custom filter lists

  function shouldRun() {
    return enabled && allowed && Date.now() >= snoozeUntil;
  }

  function apply() {
    const existing = document.getElementById(STYLE_ID);
    if (!shouldRun()) {
      if (existing) existing.remove();
      return;
    }
    // rebuild every time so toggling custom lists / snooze takes effect immediately
    const style = existing || document.createElement("style");
    style.id = STYLE_ID;
    style.textContent = SELECTORS.concat(custom).join(",") + "{display:none !important;}";
    if (!existing) (document.head || document.documentElement).appendChild(style);
  }

  function refreshAllow(list) {
    const h = location.hostname;
    allowed = !((list || []).some((d) => h === d || h.endsWith("." + d)));
    apply();
  }

  chrome.storage.local.get(["cosmetic", "allowlist", "snoozeUntil", "customCosmetic"], (r) => {
    if (r && r.cosmetic === false) enabled = false;
    snoozeUntil = (r && r.snoozeUntil) || 0;
    if (r && Array.isArray(r.customCosmetic)) custom = r.customCosmetic;
    refreshAllow(r && r.allowlist);
  });
  chrome.storage.onChanged.addListener((c) => {
    if (c.cosmetic) enabled = c.cosmetic.newValue !== false;
    if (c.snoozeUntil) snoozeUntil = c.snoozeUntil.newValue || 0;
    if (c.customCosmetic && Array.isArray(c.customCosmetic.newValue)) custom = c.customCosmetic.newValue;
    if (c.allowlist) {
      refreshAllow(c.allowlist.newValue);
      return;
    }
    apply();
  });

  // Insert as early as possible, then once more when <head> exists.
  apply();
  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", apply, { once: true });
  }
})();
