/* Daily Word - audio chain diagnostic. Paste whole into the console on the
   Daily Word page (with the reader open or not - it does not matter).
   It runs the exact chain the player runs and prints each step.
   Result also lands on window.DW_AUDIO_DIAG. */
(function () {
  var K = { headers: { "api-key": "ihqFe6Ulyut2QBDL8qqXX" } };
  var API = "https://api.scripture.api.bible/v1";
  var out = { steps: [] };
  function log(s, x) { out.steps.push([s, x]); console.log("[" + s + "]", x); }

  fetch(API + "/audio-bibles?language=eng&include-full-details=true", K)
    .then(function (r) { log("1 list status", r.status); return r.json(); })
    .then(function (j) {
      var list = (j && j.data) || [];
      log("1 audio bibles found", list.map(function (b) {
        return b.id + " " + (b.abbreviationLocal || b.abbreviation || b.name);
      }));
      var pick = list.filter(function (b) {
        return /niv/i.test(b.abbreviation || "") || /niv/i.test(b.abbreviationLocal || "");
      })[0] || list[0];
      if (!pick) { log("STOP", "no audio bibles on this key"); throw "stop"; }
      out.bibleId = pick.id;
      log("2 picked", pick.id + " " + (pick.name || ""));
      return fetch(API + "/audio-bibles/" + pick.id + "/chapters/GEN.1", K);
    })
    .then(function (r) {
      log("3 chapter GEN.1 status", r.status);
      return r.text();
    })
    .then(function (t) {
      log("3 chapter raw body (first 600)", t.slice(0, 600));
      var j = null; try { j = JSON.parse(t); } catch (e) {}
      var url = j && j.data && j.data.resourceUrl;
      log("3 resourceUrl present", !!url);
      if (!url) { log("STOP", "no resourceUrl - this is the failure"); throw "stop"; }
      out.resourceUrl = url.slice(0, 120) + "...";
      log("4 fetching audio as blob (the player's first attempt)", url.slice(0, 90));
      return fetch(url).then(function (r) {
        log("4 blob fetch status", r.status);
        return r.blob();
      }).then(function (b) {
        log("4 blob size", b.size + " bytes, type " + b.type);
        var a = new Audio(URL.createObjectURL(b));
        return a.play().then(function () {
          log("5 play()", "STARTED - you should hear Genesis 1 now");
          setTimeout(function () { a.pause(); log("5", "paused after 4s"); }, 4000);
        }).catch(function (e) {
          log("5 play() refused", e.name + ": " + e.message);
          log("5b trying direct URL instead of blob", "");
          var d = new Audio(url);
          return d.play().then(function () {
            log("5b direct play", "STARTED");
            setTimeout(function () { d.pause(); }, 4000);
          }).catch(function (e2) { log("5b direct refused", e2.name + ": " + e2.message); });
        });
      }).catch(function (e) {
        log("4 blob fetch FAILED (likely CORS)", String(e));
        log("4b trying direct URL", "");
        var d = new Audio(url);
        return d.play().then(function () {
          log("4b direct play", "STARTED - you should hear audio");
          setTimeout(function () { d.pause(); }, 4000);
        }).catch(function (e2) { log("4b direct refused", e2.name + ": " + e2.message); });
      });
    })
    .catch(function (e) { if (e !== "stop") log("UNEXPECTED", String(e)); })
    .then(function () { window.DW_AUDIO_DIAG = out; console.log("done - window.DW_AUDIO_DIAG"); });
})();
