"use strict";
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.load = void 0;
// Copyright 2018-2024 the Deno authors. MIT license.
const dntShim = __importStar(require("../../../../../_dnt.shims.js"));
const hasPermissions = "permissions" in dntShim.Deno;
let readRequested = false;
const netRequested = new Set();
async function requestRead(path) {
    if (readRequested || !hasPermissions) {
        return;
    }
    readRequested = true;
    await dntShim.Deno.permissions.request({ name: "read", path });
}
async function requestNet(host) {
    if (!hasPermissions || netRequested.has(host)) {
        return;
    }
    netRequested.add(host);
    await dntShim.Deno.permissions.request({ name: "net", host });
}
/** A Deno specific loader function that can be passed to the
 * `createModuleGraph` which will use `Deno.readTextFile` for local files, or
 * use `fetch()` for remote modules.
 *
 * @param specifier The string module specifier from the module graph.
 */
async function load(specifier) {
    const url = new URL(specifier);
    try {
        switch (url.protocol) {
            case "file:": {
                await requestRead(url);
                const content = await dntShim.Deno.readTextFile(url);
                return {
                    kind: "module",
                    specifier,
                    content,
                };
            }
            case "http:":
            case "https:": {
                await requestNet(url.host);
                const response = await dntShim.fetch(String(url), { redirect: "follow" });
                if (response.status !== 200) {
                    // ensure the body is read as to not leak resources
                    await response.arrayBuffer();
                    return undefined;
                }
                const content = await response.text();
                const headers = {};
                for (const [key, value] of response.headers) {
                    headers[key.toLowerCase()] = value;
                }
                return {
                    kind: "module",
                    specifier: response.url,
                    headers,
                    content,
                };
            }
            default:
                return undefined;
        }
    }
    catch {
        return undefined;
    }
}
exports.load = load;
