(function () {
  const OWNER_MAX_RECORDS = 99999999999999;

  function setOwnerMode() {
    window.OWNER_MAX_RECORDS = OWNER_MAX_RECORDS;
    window.isTrial = false;

    try {
      isTrial = false;
    } catch (error) {
      // The obfuscated build may keep this as a local global binding.
    }

    const modal = document.getElementById("licenseModal");
    if (modal) {
      modal.style.display = "none";
    }

    const message = document.getElementById("license_msg1");
    if (message) {
      message.textContent = "Owner build: fetch limit set to 99999999999999 records.";
    }

    const downloadButton = document.getElementById("downloadBtn");
    if (downloadButton) {
      downloadButton.disabled = false;
      downloadButton.classList.remove("disabled");
      downloadButton.removeAttribute("disabled");
    }
  }

  function enforceOwnerLimit() {
    try {
      if (Array.isArray(allData) && allData.length >= OWNER_MAX_RECORDS) {
        allData.length = OWNER_MAX_RECORDS;

        try {
          isStopped = true;
        } catch (error) {
          window.isStopped = true;
        }

        if (window.chrome && chrome.runtime && chrome.runtime.sendMessage) {
          chrome.runtime.sendMessage({ type: "STOP_SCRAPE" });
        }
      }
    } catch (error) {
      // allData is created by the obfuscated app after startup.
    }
  }

  setOwnerMode();
  document.addEventListener("DOMContentLoaded", setOwnerMode);
  window.addEventListener("load", setOwnerMode);
  setInterval(setOwnerMode, 1000);
  setInterval(enforceOwnerLimit, 500);
})();
