"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.time_now = exports.is_file = exports.modified_time = exports.atomic_write_file = exports.read_file_bytes = void 0;
const dntShim = __importStar(require("../../../../../../../../_dnt.shims.js"));
function read_file_bytes(path) {
    try {
        return dntShim.Deno.readFileSync(path);
    }
    catch (err) {
        if (err instanceof dntShim.Deno.errors.NotFound) {
            return undefined;
        }
        else {
            throw err;
        }
    }
}
exports.read_file_bytes = read_file_bytes;
function atomic_write_file(path, bytes) {
    function parentPath(path) {
        const lastSlashIndex = path.lastIndexOf("/") ?? path.lastIndexOf("\\");
        return path.slice(0, lastSlashIndex);
    }
    // write to a temporary file write beside the other file, then rename it
    // in a single sys call in order to prevent issues where the process
    // is killed while writing to a file and the file ends up in a corrupted state
    const cachePerm = 0o644;
    const tempName = path + "." + randomHex();
    try {
        dntShim.Deno.writeFileSync(tempName, bytes, { mode: cachePerm });
    }
    catch (err) {
        if (err instanceof dntShim.Deno.errors.NotFound) {
            try {
                dntShim.Deno.mkdirSync(parentPath(path), { recursive: true });
            }
            catch {
                // ignore
            }
            dntShim.Deno.writeFileSync(tempName, bytes, { mode: cachePerm });
        }
        else {
            throw err;
        }
    }
    try {
        dntShim.Deno.renameSync(tempName, path);
    }
    catch (err) {
        try {
            dntShim.Deno.removeSync(tempName);
        }
        catch {
            // ignore
        }
        throw err;
    }
    function randomHex() {
        //https://stackoverflow.com/a/27747377/188246
        const arr = new Uint8Array(2);
        crypto.getRandomValues(arr);
        return Array.from(arr, (dec) => dec.toString(16).padStart(2, "0")).join("");
    }
}
exports.atomic_write_file = atomic_write_file;
function modified_time(path) {
    try {
        const stat = dntShim.Deno.lstatSync(path);
        return msToS(stat.mtime.getTime());
    }
    catch (err) {
        if (err instanceof dntShim.Deno.errors.NotFound) {
            return undefined;
        }
        else {
            throw err;
        }
    }
}
exports.modified_time = modified_time;
function is_file(path) {
    try {
        const stat = dntShim.Deno.lstatSync(path);
        return stat.isFile;
    }
    catch {
        return false;
    }
}
exports.is_file = is_file;
function time_now() {
    return msToS(Date.now());
}
exports.time_now = time_now;
function msToS(ms) {
    return Math.round(ms / 1000);
}
