/**
 * Zeq extension update check.
 *
 * WHY THIS FILE EXISTS: this extension cannot update itself. A zip loaded via
 * "Load unpacked" is frozen at whatever you loaded, forever — Chrome will not
 * touch it, with or without update_url. So every fix shipped since your install
 * has been invisible to you. That is how this extension came to be three weeks
 * behind the website's crypto while still claiming "identical derivation".
 *
 * It cannot pull. So it asks, and tells you plainly.
 */
(function (root) {
  "use strict";
  var LATEST = "https://zeq.me/extensions/latest.json";

  function cmp(a, b) {
    var x = String(a).split("."), y = String(b).split(".");
    for (var i = 0; i < Math.max(x.length, y.length); i++) {
      var d = (parseInt(x[i], 10) || 0) - (parseInt(y[i], 10) || 0);
      if (d) return d;
    }
    return 0;
  }

  async function check() {
    try {
      var mine = (chrome.runtime.getManifest() || {}).version || "0";
      var r = await fetch(LATEST, { cache: "no-store" });
      if (!r.ok) return null;
      var d = await r.json();
      var them = (d.chrome || {}).version;
      if (!them || cmp(them, mine) <= 0) return null;
      return { current: mine, latest: them, download: (d.chrome || {}).download,
               notes: (d.chrome || {}).notes };
    } catch (_) { return null; }   // offline is not an error worth shouting about
  }

  root.ZeqUpdateCheck = { check: check, compare: cmp };
})(typeof globalThis !== "undefined" ? globalThis : window);
