/*
  ads.js — Save Pet
  ---------------------------------------------------------------
  Ad slots are OFF by default (SP.Config.ADSENSE_ENABLED = false)
  so the game ships with zero third-party network calls until you
  add your own AdSense IDs in config.js. Banner containers are
  positioned in normal document flow above/below the play area —
  never as an overlay — so an ad can never cover gameplay.
  Rewarded/interstitial calls always eventually invoke their
  callback even if no ad network is wired up, so a broken/missing
  ad can never soft-lock the player.
*/
window.SP = window.SP || {};

SP.Ads = (function () {
  "use strict";

  function fillSlot(container, slotId) {
    if (!container) return;
    if (!SP.Config.ADSENSE_ENABLED || typeof window.adsbygoogle === "undefined" || !slotId) {
      container.classList.add("ad-slot--placeholder");
      return;
    }
    try {
      const ins = document.createElement("ins");
      ins.className = "adsbygoogle";
      ins.style.display = "block";
      ins.setAttribute("data-ad-client", SP.Config.ADSENSE_CLIENT_ID);
      ins.setAttribute("data-ad-slot", slotId);
      ins.setAttribute("data-ad-format", "auto");
      ins.setAttribute("data-full-width-responsive", "true");
      container.innerHTML = "";
      container.appendChild(ins);
      (window.adsbygoogle = window.adsbygoogle || []).push({});
    } catch (e) {
      container.classList.add("ad-slot--placeholder");
    }
  }

  function initAll() {
    const slots = SP.Config.ADSENSE_SLOTS;
    fillSlot(document.getElementById("ad-top-banner"), slots.topBanner);
    fillSlot(document.getElementById("ad-bottom-banner"), slots.bottomBanner);
    fillSlot(document.getElementById("ad-interstitial"), slots.interstitial);
    fillSlot(document.getElementById("ad-gameover"), slots.gameOver);
  }

  /* "Between levels" interstitial placeholder. Swap the body of
     this function for your ad network's interstitial call — just
     always call onClose() (even on failure) so play can continue. */
  function showInterstitial(onClose) {
    if (SP.UI && SP.UI.showInterstitialPlaceholder) SP.UI.showInterstitialPlaceholder(onClose);
    else onClose();
  }

  /* Rewarded-ad placeholder used by Hint/Skip flows. Swap this for
     your real rewarded-ad SDK call; call onReward() only if the ad
     was actually watched, and always call onClose() when done. */
  function showRewarded(onReward, onClose) {
    if (SP.UI && SP.UI.showRewardedPlaceholder) SP.UI.showRewardedPlaceholder(onReward, onClose);
    else { onReward(); onClose(); }
  }

  return { initAll: initAll, showInterstitial: showInterstitial, showRewarded: showRewarded };
})();
