"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.DiskCache = 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 DiskCache {
    constructor(location) {
        Object.defineProperty(this, "location", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        (0, util_js_1.assert)((0, deps_js_1.isAbsolute)(location));
        this.location = location;
    }
    async get(filename) {
        const path = (0, deps_js_1.join)(this.location, filename);
        const file = await dntShim.Deno.open(path, { read: true });
        const value = await (0, deps_js_1.readAll)(file);
        file.close();
        return value;
    }
    async set(filename, data) {
        const path = (0, deps_js_1.join)(this.location, filename);
        const parentFilename = (0, deps_js_1.dirname)(path);
        await (0, deps_js_1.ensureDir)(parentFilename);
        const file = await dntShim.Deno.open(path, {
            write: true,
            create: true,
            mode: util_js_1.CACHE_PERM,
        });
        await (0, deps_js_1.writeAll)(file, data);
        file.close();
    }
    static async getCacheFilename(url) {
        const { url_to_filename } = await (0, deno_cache_dir_generated_js_1.instantiate)();
        return url_to_filename(url.toString());
    }
}
exports.DiskCache = DiskCache;
