"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.homedir = void 0;
exports.makeStatusDirs = makeStatusDirs;
exports.renameCurrentFile = renameCurrentFile;
var path = __importStar(require("path"));
var fs = __importStar(require("fs"));
var os = __importStar(require("os"));
exports.homedir = os.homedir ? os.homedir() : (process.env[(process.platform == "win32") ? "USERPROFILE" : "HOME"]);
/**
 * Zero dependencies: recursive mkdir
 */
function mkDirByPathSync(HOME_DIR, targetDir, _a) {
    var _b = _a === void 0 ? {} : _a, _c = _b.isRelativeToScript, isRelativeToScript = _c === void 0 ? false : _c;
    var sep = path.sep;
    var initDir = path.isAbsolute(targetDir) ? sep : "";
    var baseDir = isRelativeToScript ? __dirname : ".";
    return targetDir.split(sep).reduce(function (parentDir, childDir) {
        var curDir = path.resolve(baseDir, parentDir, childDir);
        try {
            // Don't try to recreate homedir
            if (HOME_DIR.indexOf(curDir) === -1) {
                fs.mkdirSync(curDir);
            }
        }
        catch (err) {
            if (err.code === "EEXIST") { // curDir already exists!
                return curDir;
            }
            // To avoid `EISDIR` error on Mac and `EACCES`-->`ENOENT` and `EPERM` on Windows.
            if (err.code === "ENOENT") { // Throw the original parentDir error on curDir `ENOENT` failure.
                throw new Error("EACCES: permission denied, mkdir \"".concat(parentDir, "\""));
            }
            var caughtErr = ["EACCES", "EPERM", "EISDIR"].indexOf(err.code) > -1;
            if (!caughtErr || caughtErr && curDir === path.resolve(targetDir)) {
                throw err; // Throw if it's just the last created dir.
            }
        }
        return curDir;
    }, initDir);
}
function makeStatusDirs(filepath) {
    try {
        mkDirByPathSync(exports.homedir, filepath.replace(/\\/g, path.sep).replace(/\//g, path.sep));
        return true;
    }
    catch (e) {
        console.error("Error creating Application Insights status folder", e);
        return false;
    }
}
function renameCurrentFile(filepath, filename, callback) {
    var fullpath = path.join(filepath, filename);
    var basename = path.basename(filename, path.extname(filename));
    var stats = fs.stat(fullpath, function (statsErr, stats) {
        if (statsErr) {
            return callback(statsErr);
        }
        var createDate = new Date(stats.birthtime);
        var destfilename = basename + "-" +
            createDate.toISOString().replace(/[T:\.]/g, "_").replace("Z", "") +
            path.extname(filename) + ".old";
        var destfullpath = path.join(filepath, destfilename);
        fs.rename(fullpath, destfullpath, function (renameErr) {
            if (typeof callback === "function") {
                callback(renameErr, destfullpath);
            }
        });
    });
}
//# sourceMappingURL=FileHelpers.js.map