/* Payment-finalizing poll for the public quote and invoice pages.
   A notice element carrying data-po-poll="<sessionStorage key>" means the
   customer just returned from Stripe and the webhook may not have landed
   yet: reload up to 12 times, 5s apart, so the page flips to its real
   paid state on its own. With no such element on the page, clear the
   counters so a later payment starts fresh. */
(function () {
  var el = document.querySelector('[data-po-poll]');
  var KEYS = ['po_pay_poll', 'po_inv_poll'];
  try {
    if (el) {
      var key = el.getAttribute('data-po-poll');
      if (KEYS.indexOf(key) === -1) { return; }
      var n = parseInt(sessionStorage.getItem(key) || '0', 10);
      if (n < 12) {
        sessionStorage.setItem(key, String(n + 1));
        setTimeout(function () { location.reload(); }, 5000);
      }
    } else {
      KEYS.forEach(function (k) { sessionStorage.removeItem(k); });
    }
  } catch (e) { /* sessionStorage unavailable; nothing to do */ }
})();
