(function (global) {
    function logoUrl(options) {
        return options.logoUrl || global.MEETUP_APP_ICON || '/images/mexco-app-icon.png';
    }

    function render(container, text, size, options) {
        options = options || {};
        if (!container || typeof QRCode === 'undefined') {
            return null;
        }

        container.innerHTML = '';
        const wrap = document.createElement('div');
        wrap.className = 'mexco-qr-wrap';

        const qrHost = document.createElement('div');
        qrHost.className = 'mexco-qr-canvas';
        wrap.appendChild(qrHost);

        const logoScale = options.logoScale != null ? options.logoScale : 0.2;
        const logoSize = Math.max(28, Math.round(size * logoScale));
        const logo = document.createElement('img');
        logo.src = logoUrl(options);
        logo.alt = 'MeXCo';
        logo.className = 'mexco-qr-logo';
        logo.width = logoSize;
        logo.height = logoSize;
        logo.style.width = logoSize + 'px';
        logo.style.height = logoSize + 'px';
        wrap.appendChild(logo);

        container.appendChild(wrap);

        new QRCode(qrHost, {
            text: text,
            width: size,
            height: size,
            colorDark: options.colorDark || '#5B21B6',
            colorLight: options.colorLight || '#ffffff',
            correctLevel: QRCode.CorrectLevel.H,
        });

        return wrap;
    }

    global.MexcoQR = {
        render: render,
        openPrint: function (url) {
            window.open(url, '_blank', 'noopener');
        },
    };
})(window);
