importScripts('../vendor/xmldom.js');
importScripts('../vendor/wijmo.min.js');
importScripts('../vendor/wijmo.pdf.min.js');
importScripts('../vendor/wijmo.grid.pdf.min.js');

this.DOMParser = xmldom.DOMParser;

wijmo.grid.pdf.PdfWebWorker.initExport(function (doc, clientData) {
    wijmo.grid.pdf.PdfWebWorker.sendExportProgress(0);

    doc.drawText(clientData['title'].content);
    doc.drawImage(clientData['flag'].content);
    doc.drawSvg(clientData['chart'].content);

    // Suppose that text and images rendering takes 5% of the time.
    wijmo.grid.pdf.PdfWebWorker.sendExportProgress(0.05);
    
    var gridData = clientData['grid'];
    gridData.settings.progress = function (value) {
        // Change progress from 6% to 90%. Left the final 10% for the document finalization, which will start after the doc.end() method is called.
        wijmo.grid.pdf.PdfWebWorker.sendExportProgress(map(value, 0, 1, 0.06, 0.9));
    };

    doc.ended.addHandler(function () {
        // Document has been created, set progress to 100%.
        wijmo.grid.pdf.PdfWebWorker.sendExportProgress(1);
    }); 

    wijmo.grid.pdf.FlexGridPdfConverter.draw(gridData.content, doc, null, null, gridData.settings);

    doc.end();
});


function map(value, inMin, inMax, outMin, outMax) {
    var k = (outMax - outMin) / (inMax - inMin);
    return (value - inMin) * k + outMin;
}