var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import React from 'react';
import ReactDOM from 'react-dom';
import QRCode from 'qrcode.react';
import state from '../state';
const renderQRCode = ({ element, sku, showInstructions = 'always', }) => {
    const { language, _id: customerId } = state.customer;
    const baseURL = 'https://viewer.r2u.io';
    const url = new URL(baseURL);
    url.searchParams.set('customerId', customerId);
    url.searchParams.set('sku', sku);
    url.searchParams.set('showInstructions', showInstructions);
    if (language) {
        url.searchParams.set('lng', language);
    }
    ReactDOM.render(React.createElement(QRCode, Object.assign({}, { 'data-url': url }, { value: url.toString(), style: { width: '100%', height: '100%' }, renderAs: "svg" })), element);
};
function create({ element, sku, showInstructions, }) {
    return __awaiter(this, void 0, void 0, function* () {
        if (!state.init)
            return Promise.reject(new Error('R2U - Not initialized'));
        renderQRCode({
            element,
            sku,
            showInstructions,
        });
        const setSku = (newSku) => __awaiter(this, void 0, void 0, function* () {
            renderQRCode({
                element,
                sku: newSku,
                showInstructions,
            });
        });
        return { setSku };
    });
}
export default {
    create,
};
//# sourceMappingURL=qrCode.js.map