"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const canLink = require("can-link");
const makeDir = require("make-dir");
const fs = require("mz/fs");
const os = require("os");
const path = require("path");
const pathAbsolute = require("path-absolute");
const pathTemp = require("path-temp");
const rootLinkTarget = require("root-link-target");
const touch = require("touch");
const STORE_VERSION = '2';
async function default_1(pkgRoot, storePath) {
    if (!storePath || isHomepath(storePath)) {
        const relStorePath = storePath ? storePath.substr(2) : '.pnpm-store';
        return await storePathRelativeToHome(pkgRoot, relStorePath);
    }
    const storeBasePath = pathAbsolute(storePath, pkgRoot);
    if (storeBasePath.endsWith(`${path.sep}${STORE_VERSION}`)) {
        return storeBasePath;
    }
    return path.join(storeBasePath, STORE_VERSION);
}
exports.default = default_1;
async function storePathRelativeToHome(pkgRoot, relStore) {
    const tempFile = pathTemp(pkgRoot);
    await makeDir(path.dirname(tempFile));
    await touch(tempFile);
    const homedir = getHomedir();
    if (await canLink(tempFile, pathTemp(homedir))) {
        await fs.unlink(tempFile);
        // 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);
    }
    try {
        const mountpoint = await rootLinkTarget(tempFile);
        // If linking works only in the project folder
        // then prefer to place the store inside the homedir
        if (path.relative(pkgRoot, mountpoint) === '.') {
            return path.join(homedir, relStore, STORE_VERSION);
        }
        return path.join(mountpoint, relStore, STORE_VERSION);
    }
    catch (err) {
        // this is an unlikely situation but if there is no way to find
        // a linkable place on the disk, create the store in homedir
        return path.join(homedir, relStore, STORE_VERSION);
    }
    finally {
        await fs.unlink(tempFile);
    }
}
function getHomedir() {
    const home = os.homedir();
    if (!home)
        throw new Error('Could not find the homedir');
    return home;
}
function isHomepath(filepath) {
    return filepath.indexOf('~/') === 0 || filepath.indexOf('~\\') === 0;
}
//# sourceMappingURL=index.js.map