(() => {
  'use strict';

  function render(root = document) {
    if (typeof QRCode === 'undefined') return;

    root.querySelectorAll('[data-vstrb-payment-qr]').forEach((element) => {
      if (element.dataset.vstrbPaymentQrRendered === '1') return;

      const target = element.querySelector('.vstrb-payment-qr-code');
      const payload = element.dataset.vstrbPaymentQr || '';
      if (!target || !payload) return;

      target.textContent = '';
      new QRCode(target, {
        text: payload,
        width: 180,
        height: 180,
        correctLevel: QRCode.CorrectLevel.M
      });
      element.dataset.vstrbPaymentQrRendered = '1';
    });
  }

  window.VSTRBPaymentQR = { render };
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', () => render());
  } else {
    render();
  }
})();
