"use strict";
exports.__esModule = true;
function CreatePromiseEvent() {
    var resolver;
    var rejecter;
    var emptyPromise = new Promise(function (resolve, reject) { resolver = resolve; rejecter = reject; });
    emptyPromise['resolve'] = resolver;
    emptyPromise['reject'] = rejecter;
    return emptyPromise;
}
exports.CreatePromiseEvent = CreatePromiseEvent;
function Call(inputFiles, command) {
    var request = {
        'files': inputFiles,
        'args': command,
        'requestNumber': magickWorkerPromisesKey
    };
    var emptyPromise = CreatePromiseEvent();
    magickWorkerPromises[magickWorkerPromisesKey] = emptyPromise;
    magickWorker.postMessage(request);
    magickWorkerPromisesKey = magickWorkerPromisesKey + 1;
    return emptyPromise;
}
exports.Call = Call;
var magickWorker = new Worker('magick.js');
var magickWorkerPromises = {};
var magickWorkerPromisesKey = 1;
// handle responses as they stream in after being processed by image magick
magickWorker.onmessage = function (e) {
    // display split images
    var response = e.data;
    var getPromise = magickWorkerPromises[response['requestNumber']];
    delete magickWorkerPromises[response['requestNumber']];
    var files = response['processed'];
    if (files.length == 0) {
        getPromise['reject']("No files generated");
    }
    else {
        getPromise['resolve'](files);
    }
};
