"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;
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _HttpCache_cache, _HttpCache_readOnly;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpCache = void 0;
// Copyright 2018-2024 the Deno authors. MIT license.
const dntShim = __importStar(require("../../../../../_dnt.shims.js"));
const deps_js_1 = require("./deps.js");
const util_js_1 = require("./util.js");
const deno_cache_dir_generated_js_1 = require("./lib/deno_cache_dir.generated.js");
class HttpCache {
    constructor(cache, readOnly) {
        _HttpCache_cache.set(this, void 0);
        _HttpCache_readOnly.set(this, void 0);
        __classPrivateFieldSet(this, _HttpCache_cache, cache, "f");
        __classPrivateFieldSet(this, _HttpCache_readOnly, readOnly, "f");
    }
    static async create(options) {
        (0, util_js_1.assert)((0, deps_js_1.isAbsolute)(options.root), "Root must be an absolute path.");
        if (options.vendorRoot != null) {
            (0, util_js_1.assert)((0, deps_js_1.isAbsolute)(options.vendorRoot), "Vendor root must be an absolute path.");
        }
        const { GlobalHttpCache, LocalHttpCache } = await (0, deno_cache_dir_generated_js_1.instantiate)();
        let cache;
        if (options.vendorRoot != null) {
            cache = LocalHttpCache.new(options.vendorRoot, options.root);
        }
        else {
            cache = GlobalHttpCache.new(options.root);
        }
        return new HttpCache(cache, options.readOnly);
    }
    [(_HttpCache_cache = new WeakMap(), _HttpCache_readOnly = new WeakMap(), Symbol.dispose)]() {
        this.free();
    }
    free() {
        __classPrivateFieldGet(this, _HttpCache_cache, "f")?.free();
    }
    getHeaders(url) {
        const map = __classPrivateFieldGet(this, _HttpCache_cache, "f").getHeaders(url.toString());
        return map == null ? undefined : Object.fromEntries(map);
    }
    get(url, options) {
        const data = __classPrivateFieldGet(this, _HttpCache_cache, "f").getFileBytes(url.toString(), options?.checksum, options?.allowCopyGlobalToLocal ?? true);
        return data == null ? undefined : data;
    }
    set(url, headers, content) {
        if (__classPrivateFieldGet(this, _HttpCache_readOnly, "f") === undefined) {
            __classPrivateFieldSet(this, _HttpCache_readOnly, (dntShim.Deno.permissions.querySync({ name: "write" })).state === "denied"
                ? true
                : false, "f");
        }
        if (__classPrivateFieldGet(this, _HttpCache_readOnly, "f")) {
            return;
        }
        __classPrivateFieldGet(this, _HttpCache_cache, "f").set(url.toString(), headers, content);
    }
}
exports.HttpCache = HttpCache;
