"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.build = exports.Parser = exports.options = void 0;
const eszip_wasm_generated_js_1 = require("./eszip_wasm.generated.js");
const encoder = new TextEncoder();
exports.options = { wasmURL: undefined };
class Parser extends eszip_wasm_generated_js_1.Parser {
    static async createInstance() {
        await (0, eszip_wasm_generated_js_1.instantiate)({ url: exports.options.wasmURL });
        return new Parser();
    }
}
exports.Parser = Parser;
async function build(roots, loader, importMapUrl) {
    const { build } = await (0, eszip_wasm_generated_js_1.instantiate)({ url: exports.options.wasmURL });
    return build(roots, (specifier, options) => {
        return loader(specifier, options.isDynamic, options.cacheSetting, options.checksum)
            .then((result) => {
            if (result?.kind === 'module') {
                if (typeof result.content === 'string') {
                    result.content = encoder.encode(result.content);
                }
                // need to convert to an array for serde_wasm_bindgen to work
                // deno-lint-ignore no-explicit-any
                result.content = Array.from(result.content);
            }
            return result;
        })
            .catch((err) => Promise.reject(String(err)));
    }, importMapUrl);
}
exports.build = build;
