"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const stacktrace_js_1 = require("stacktrace-js");
/**
 * {@link call} shortcut that only returns the output files.
 */
async function Call(inputFiles, command) {
    const result = await call(inputFiles, command);
    for (let outputFile of result.outputFiles) {
        outputFile.blob = new Blob([outputFile.buffer]);
    }
    return result.outputFiles;
}
exports.Call = Call;
/**
 * Low level execution function. All the other functions like [execute](https://github.com/KnicKnic/WASM-ImageMagick/tree/master/apidocs#execute)
 * ends up calling this one. It accept only one command and only in the form of array of strings.
 */
function call(inputFiles, command) {
    const request = {
        files: inputFiles,
        args: command,
        requestNumber: magickWorkerPromisesKey,
        transferable: true,
    };
    let transfer = [];
    for (let file of request.files) {
        if (file.content instanceof ArrayBuffer) {
            transfer.push(file.content);
        }
        else {
            transfer.push(file.content.buffer);
        }
    }
    const promise = CreatePromiseEvent();
    magickWorkerPromises[magickWorkerPromisesKey] = promise;
    magickWorker.postMessage(request, transfer);
    magickWorkerPromisesKey++;
    return promise;
}
exports.call = call;
function CreatePromiseEvent() {
    let resolver;
    let rejecter;
    const emptyPromise = new Promise((resolve, reject) => {
        resolver = resolve;
        rejecter = reject;
    });
    emptyPromise.resolve = resolver;
    emptyPromise.reject = rejecter;
    return emptyPromise;
}
exports.CreatePromiseEvent = CreatePromiseEvent;
function ChangeUrl(url, fileName) {
    let splitUrl = url.split('/');
    splitUrl[splitUrl.length - 1] = fileName;
    return splitUrl.join('/');
}
function GetCurrentUrlDifferentFilename(fileName) {
    return ChangeUrl(currentJavascriptURL, fileName);
}
let currentJavascriptURL = './magickApi.js';
// // instead of doing the sane code of being able to just use import.meta.url 
// // (Edge doesn't work) (safari mobile, chrome, opera, firefox all do)
// // 
// // I will use stacktrace-js library to get the current file name
// //
// try {
//   // @ts-ignore
//   let packageUrl = import.meta.url;
//   currentJavascriptURL = packageUrl;
// } catch (error) {
//   // eat
// }
//
//
{
    let stacktrace = stacktrace_js_1.default.getSync();
    currentJavascriptURL = stacktrace[0].fileName;
}
const magickWorkerUrl = GetCurrentUrlDifferentFilename('magick.js');
function GenerateMagickWorkerText(magickUrl) {
    // generates code for the following
    // var magickJsCurrentPath = 'magickUrl';
    // importScripts(magickJsCurrentPath);
    return "var magickJsCurrentPath = '" + magickUrl + "';\n" +
        'importScripts(magickJsCurrentPath);';
}
let magickWorker;
if (currentJavascriptURL.startsWith('http')) {
    magickWorker = new Worker(window.URL.createObjectURL(new Blob([GenerateMagickWorkerText(magickWorkerUrl)])));
}
else {
    magickWorker = new Worker(magickWorkerUrl);
}
const magickWorkerPromises = {};
let magickWorkerPromisesKey = 1;
// handle responses as they stream in after being outputFiles by image magick
magickWorker.onmessage = e => {
    const response = e.data;
    const promise = magickWorkerPromises[response.requestNumber];
    delete magickWorkerPromises[response.requestNumber];
    const result = {
        outputFiles: response.outputFiles,
        stdout: response.stdout,
        stderr: response.stderr,
        exitCode: response.exitCode || 0,
    };
    promise.resolve(result);
};
//# sourceMappingURL=magickApi.js.map