"use strict";
// Copyright 2018-2024 the Deno authors. MIT license.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseModule = exports.init = exports.createGraph = exports.MediaType = exports.load = void 0;
/**
 * A JavaScript/TypeScript interface to the Deno CLI's module graph logic.
 *
 * ### Example
 *
 * To build and output a graph as a JSON structure to the console:
 *
 * ```ts
 * import { createGraph } from "jsr:@deno/graph/@{VERSION}";
 *
 * const graph = await createGraph("https://deno.land/x/std/testing/asserts.ts");
 *
 * console.log(JSON.stringify(graph, undefined, "  "));
 * ```
 *
 * @module
 */
const wasm = __importStar(require("./deno_graph_wasm.generated.js"));
const loader_js_1 = require("./loader.js");
var loader_js_2 = require("./loader.js");
Object.defineProperty(exports, "load", { enumerable: true, get: function () { return loader_js_2.load; } });
var media_type_js_1 = require("./media_type.js");
Object.defineProperty(exports, "MediaType", { enumerable: true, get: function () { return media_type_js_1.MediaType; } });
const encoder = new TextEncoder();
/** Create a module graph using the same algorithms that are used in the Deno
 * CLI, resolving with the module graph for further processing.
 *
 * A default `load()` function is provided which will attempt to load local
 * modules via `Deno.readFile()` and will use `fetch()` to load remote
 * modules. An alternative `load()` function can be provided via the options.
 *
 * ### Example
 *
 * ```ts
 * import { createGraph } from "https://deno.land/x/deno_graph/mod.ts";
 *
 * const graph = await createGraph("https://example.com/a.ts");
 *
 * console.log(graph.toString());
 * ```
 *
 * @param rootSpecifiers A URL string of the root module specifier to build the
 * graph from or array of URL strings.
 * @param options A set of options for building the graph
 */
async function createGraph(rootSpecifiers, options = {}) {
    rootSpecifiers = Array.isArray(rootSpecifiers)
        ? rootSpecifiers
        : [rootSpecifiers];
    const { load = loader_js_1.load, defaultJsxImportSource, jsxImportSourceModule, cacheInfo, resolve, resolveTypes, kind, imports, } = options;
    const { createGraph } = await wasm.instantiate();
    return await createGraph(rootSpecifiers, async (specifier, options) => {
        const result = await load(specifier, options.isDynamic, options.cacheSetting, options.checksum);
        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;
    }, defaultJsxImportSource, jsxImportSourceModule, cacheInfo, resolve, resolveTypes, kind, imports);
}
exports.createGraph = createGraph;
/** Instantiates the Wasm module used within deno_graph. */
async function init(opts) {
    await wasm.instantiate(opts);
}
exports.init = init;
/** Parse a module based on the supplied information and return its analyzed
 * representation. If an error is encountered when parsing, the function will
 * throw.
 *
 * @param specifier The URL text specifier to use when parsing the module.
 * @param content The content of the module to be parsed.
 * @param options Options to use when parsing the module.
 */
function parseModule(specifier, content, options = {}) {
    const { headers, defaultJsxImportSource, jsxImportSourceModule, resolve, resolveTypes, } = options;
    if (!wasm.isInstantiated()) {
        throw new Error("Please call `init()` at least once before calling `parseModule`.");
    }
    return wasm.parseModule(specifier, headers, defaultJsxImportSource, jsxImportSourceModule, content, resolve, resolveTypes);
}
exports.parseModule = parseModule;
