"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = require("@pnpm/logger");
const driveByPath = require("drive-by-path");
const osHomedir = require("os-homedir");
const path = require("path");
const pathAbsolute = require("path-absolute");
const R = require("ramda");
const STORE_VERSION = '2';
const STORE_DEFAULT_PATH = '~/.pnpm-store';
function default_1(storePath, pkgRoot) {
    return __awaiter(this, void 0, void 0, function* () {
        const pkgDrive = yield safeDriveByPath(pkgRoot);
        if (!storePath || isHomepath(storePath)) {
            const relStorePath = storePath ? storePath.substr(2) : '.pnpm-store';
            return yield storePathRelativeToHome(pkgDrive, relStorePath);
        }
        const storeBasePath = pathAbsolute(storePath, pkgRoot);
        const storeDrive = yield safeDriveByPath(storeBasePath);
        if (pkgDrive && storeDrive && pkgDrive.device !== storeDrive.device) {
            logger_1.default.warn('The store is not on the same device on which the installation is done. ' +
                `Store is on ${storeDrive.displayName}, device is on ${pkgDrive.displayName}`);
        }
        if (storeBasePath.endsWith(`${path.sep}${STORE_VERSION}`)) {
            return storeBasePath;
        }
        return path.join(storeBasePath, STORE_VERSION);
    });
}
exports.default = default_1;
function shortestDriveMountpoint(drive) {
    // `as string` might be a bad thing to do but as of now,
    // this will never return undefined
    return R.head(R.map(R.prop('path'), drive.mountpoints).sort());
}
function storePathRelativeToHome(pkgDrive, relStore) {
    return __awaiter(this, void 0, void 0, function* () {
        const homedir = getHomedir();
        const homeDrive = yield safeDriveByPath(homedir);
        if (!pkgDrive || !homeDrive || pkgDrive.device === homeDrive.device) {
            // If the project is on the drive on which the OS home directory
            // then the store is placed in the home directory
            return path.join(homedir, relStore, STORE_VERSION);
        }
        const mountpoint = shortestDriveMountpoint(pkgDrive);
        return path.join(mountpoint, relStore, STORE_VERSION);
    });
}
function getHomedir() {
    const home = osHomedir();
    if (!home)
        throw new Error('Could not find the homedir');
    return home;
}
function safeDriveByPath(p) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            return yield driveByPath(p);
        }
        catch (err) {
            // When devices could not be detected
            // it is assumed that the project is on the same device as the store
            return null;
        }
    });
}
function isHomepath(filepath) {
    return filepath.indexOf('~/') === 0 || filepath.indexOf('~\\') === 0;
}
//# sourceMappingURL=resolveStore.js.map