"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.copy = exports.getTmpPath = void 0;
const os_1 = __importDefault(require("os"));
const path_1 = require("path");
const recursive_copy_1 = __importDefault(require("recursive-copy"));
const crypto_1 = require("@f2/crypto");
const dir_1 = require("./dir");
/**
 * @deprecated
 */
function getTmpPath(ext) {
    const maxAttempts = 10;
    let attemptCount = 0;
    return new Promise((resolve, reject) => {
        const tryGenerate = async () => {
            attemptCount++;
            try {
                const filename = await (0, crypto_1.generate)(32);
                const dir = os_1.default.tmpdir();
                const path = (0, path_1.join)(dir, filename + (ext ? '.' + ext : ''));
                await (0, dir_1.prepareDir)(dir);
                resolve(path);
            }
            catch (err) {
                if (maxAttempts > attemptCount) {
                    setImmediate(tryGenerate);
                    return;
                }
                reject(new Error('Impossible de générer un chemin temporaire valide'));
            }
        };
        tryGenerate();
    });
}
exports.getTmpPath = getTmpPath;
async function copy(src, dest, options) {
    await (0, recursive_copy_1.default)(src, dest, options);
}
exports.copy = copy;
