"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.makeExecutable = exports.isExecutable = exports.getTmpFilePath = exports.unlinkIfExists = exports.unlinks = exports.unlink = exports.writeFile = exports.readFile = exports.readFileToBuffer = void 0;
const fs_1 = __importStar(require("fs"));
const path_1 = require("path");
const crypto_1 = require("@f2/crypto");
const info_1 = require("./info");
const dir_1 = require("./dir");
function readFileToBuffer(path) {
    return new Promise((resolve, reject) => {
        fs_1.default.readFile(path, (err, data) => {
            if (err) {
                reject(err);
                return;
            }
            resolve(data);
        });
    });
}
exports.readFileToBuffer = readFileToBuffer;
;
async function readFile(path) {
    const buffer = await readFileToBuffer(path);
    return buffer.toString('utf8');
}
exports.readFile = readFile;
;
function writeFile(path, content) {
    return new Promise((resolve, reject) => {
        fs_1.default.writeFile(path, content, (err) => {
            if (err) {
                return reject(err);
            }
            resolve();
        });
    });
}
exports.writeFile = writeFile;
function unlink(path) {
    return new Promise((resolve, reject) => {
        fs_1.default.unlink(path, (err) => {
            if (err) {
                reject(err);
                return;
            }
            resolve();
        });
    });
}
exports.unlink = unlink;
async function unlinks(paths) {
    let unlinkPromises = [];
    paths.forEach((path) => {
        unlinkPromises.push(unlink(path));
    });
    await Promise.all(unlinkPromises);
}
exports.unlinks = unlinks;
async function unlinkIfExists(path) {
    const access = await (0, info_1.accessPath)(path);
    if (!access) {
        return false;
    }
    await unlink(path);
    return true;
}
exports.unlinkIfExists = unlinkIfExists;
async function getTmpFilePath(optionsOrExtension) {
    let ext;
    let dirSuffix;
    let filenameLength;
    if (!optionsOrExtension) {
        optionsOrExtension = {};
    }
    else if (typeof optionsOrExtension === 'string') {
        ext = optionsOrExtension;
    }
    else {
        ext = optionsOrExtension.ext;
        dirSuffix = optionsOrExtension.dirSuffix;
        filenameLength = optionsOrExtension.filnameLength;
    }
    const [dirPath, filename] = await Promise.all([
        (0, dir_1.getTmpDirPath)(dirSuffix),
        (0, crypto_1.generate)(filenameLength || 16)
    ]);
    return (0, path_1.join)(dirPath, filename + (ext ? `.${ext}` : ''));
}
exports.getTmpFilePath = getTmpFilePath;
async function isExecutable(path) {
    return (0, info_1.accessPath)(path, fs_1.default.constants.X_OK);
}
exports.isExecutable = isExecutable;
async function makeExecutable(path, _chmod = '0755') {
    const executable = await isExecutable(path);
    if (executable) {
        return;
    }
    await new Promise((resolve, reject) => {
        (0, fs_1.chmod)(path, _chmod, (err) => {
            if (err) {
                return reject(err);
            }
            resolve();
        });
    });
}
exports.makeExecutable = makeExecutable;
