"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var fs_1 = __importDefault(require("fs"));
var mkdirp = require("mkdirp");
var homeDir = process.env.HOME;
exports.BASE_REPOS_URI = "file://" + homeDir + "/rubberduck-local-repos";
exports.toPath = function (uriPath) {
    return uriPath.replace("file://", "");
};
exports.readFile = function (fileUri) {
    return new Promise(function (resolve, reject) {
        fs_1.default.readFile(exports.toPath(fileUri), "utf8", function (err, contents) {
            resolve(contents);
        });
    });
};
exports.clean = function (message, repoInfo) {
    // Removes uri full path with relative path
    var str = JSON.stringify(message);
    var rootUri = exports.constructRootUri(repoInfo);
    var toReplace = "\"uri\":\"" + rootUri + "/";
    return JSON.parse(str.replace(new RegExp(toReplace, "g"), '"path":"'));
};
exports.constructCloneUri = function (repoInfo) {
    return exports.BASE_REPOS_URI + "/" + repoInfo.name + ".git";
};
exports.constructRootUri = function (repoInfo) {
    var name = repoInfo.name, sha = repoInfo.sha;
    return exports.BASE_REPOS_URI + "/" + name + "_" + sha.substring(0, 7);
};
exports.constructFileUri = function (repoInfo, path) {
    var rootUri = exports.constructRootUri(repoInfo);
    return rootUri + "/" + path;
};
exports.mkdir = function (path) {
    return new Promise(function (resolve, reject) {
        mkdirp(path, function (err) {
            if (err)
                reject(err);
            else
                resolve();
        });
    });
};
