/* This file is excluded from coverage because the intrumented code
 * translates "self" which gives a reference error.
 */

/* istanbul ignore next */

export function workerCode() {
    self.document = {}; // Workaround for "ReferenceError: document is not defined" in hpccWasm
    var hpccWasm;

    self.onmessage = function(event) {
        if (event.data.vizURL) {
            importScripts(event.data.vizURL);
            hpccWasm = self["@hpcc-js/wasm"];
            hpccWasm.wasmFolder(event.data.vizURL.match(/.*\//));
    // This is an alternative workaround where wasmFolder() is not needed
    //                    document = {currentScript: {src: event.data.vizURL}};
        }
        hpccWasm.graphviz.layout(event.data.dot, "svg", event.data.engine, event.data.options).then((svg) => {
            if (svg) {
                self.postMessage({
                    type: "done",
                    svg: svg,
                });
            } else if (event.data.vizURL) {
                self.postMessage({
                    type: "init",
                });
            } else {
                self.postMessage({
                    type: "skip",
                });
            }
        }).catch(error => {
            self.postMessage({
                type: "error",
                error: error.message,
            });
        });
    }
}

/* This file is excluded from coverage because the intrumented code
 * translates "self" which gives a reference error.
 */

/* istanbul ignore next */

export function workerCode() {
    self.document = {}; // Workaround for "ReferenceError: document is not defined" in hpccWasm
    var hpccWasm;

    self.onmessage = function(event) {
        if (event.data.vizURL) {
            importScripts(event.data.vizURL);
            hpccWasm = self["@hpcc-js/wasm"];
            hpccWasm.wasmFolder(event.data.vizURL.match(/.*\//)[0]);
    // This is an alternative workaround where wasmFolder() is not needed
    //                    document = {currentScript: {src: event.data.vizURL}};
        }
        hpccWasm.graphviz.layout(event.data.dot, "svg", event.data.engine, event.data.options).then((svg) => {
            if (svg) {
                self.postMessage({
                    type: "done",
                    svg: svg,
                });
            } else if (event.data.vizURL) {
                self.postMessage({
                    type: "init",
                });
            } else {
                self.postMessage({
                    type: "skip",
                });
            }
        }).catch(error => {
            self.postMessage({
                type: "error",
                error: error.message,
            });
        });
    }
}

Fix URL argument to wasmFolder to be string, not array
Fix URL argument to wasmFolder to be string, not array

Fixes https://github.com/magjac/d3-graphviz/issues/154

For some reason this incorrectness was working in @hpcc-js/wasm
version 0.3.11 and before, but with the changes in
https://github.com/hpcc-systems/hpcc-js-wasm/commit/1e4f67e26a94f9de6f6b1a7b9ceed85bceed3007
it no longer does.
