"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 normalizeRegistryUrl = require("normalize-registry-url");
const path = require("path");
const modulesController_1 = require("../fs/modulesController");
const pnpmPkgJson_1 = require("../pnpmPkgJson");
const defaults = (opts) => __awaiter(this, void 0, void 0, function* () {
    const packageManager = opts.packageManager || {
        name: pnpmPkgJson_1.default.name,
        version: pnpmPkgJson_1.default.version,
    };
    const prefix = opts.prefix || process.cwd();
    return {
        bin: path.join(prefix, 'node_modules', '.bin'),
        childConcurrency: 5,
        depth: 0,
        development: true,
        engineStrict: false,
        force: false,
        global: false,
        hooks: {},
        ignoreScripts: false,
        independentLeaves: false,
        lock: true,
        lockStaleDuration: 60 * 1000,
        locks: path.join(opts.store, '_locks'),
        nodeVersion: process.version,
        optional: typeof opts.production === 'boolean' ? opts.production : true,
        packageManager,
        prefix,
        production: true,
        rawNpmConfig: {},
        registry: 'https://registry.npmjs.org/',
        repeatInstallDepth: -1,
        saveDev: false,
        saveExact: false,
        saveOptional: false,
        savePrefix: '^',
        shamefullyFlatten: false,
        shrinkwrap: true,
        shrinkwrapOnly: false,
        sideEffectsCache: false,
        sideEffectsCacheReadonly: false,
        store: opts.store,
        storeController: opts.storeController,
        tag: 'latest',
        unsafePerm: process.platform === 'win32' ||
            process.platform === 'cygwin' ||
            !(process.getuid && process.setuid &&
                process.getgid && process.setgid) ||
            process.getuid() !== 0,
        update: false,
        userAgent: `${packageManager.name}/${packageManager.version} npm/? node/${process.version} ${process.platform} ${process.arch}`,
        verifyStoreIntegrity: true,
    };
});
exports.default = (opts) => __awaiter(this, void 0, void 0, function* () {
    if (opts) {
        for (const key in opts) {
            if (opts[key] === undefined) {
                delete opts[key];
            }
        }
    }
    const defaultOpts = yield defaults(opts);
    const extendedOpts = Object.assign({}, defaultOpts, opts, { store: defaultOpts.store });
    if (!extendedOpts.reinstallForFlatten) {
        if (extendedOpts.force) {
            logger_1.default.warn('using --force I sure hope you know what you are doing');
        }
        if (extendedOpts.lock === false) {
            logger_1.default.warn('using --no-lock I sure hope you know what you are doing');
        }
        if (extendedOpts.shamefullyFlatten) {
            logger_1.default.warn('using --shamefully-flatten is discouraged, you should declare all of your dependencies in package.json');
        }
        if (!extendedOpts.shrinkwrap && extendedOpts.shrinkwrapOnly) {
            throw new Error('Cannot generate a shrinkwrap.yaml because shrinkwrap is set to false');
        }
        if (extendedOpts.userAgent.startsWith('npm/')) {
            extendedOpts.userAgent = `${extendedOpts.packageManager.name}/${extendedOpts.packageManager.version} ${extendedOpts.userAgent}`;
        }
        extendedOpts.registry = normalizeRegistryUrl(extendedOpts.registry);
        if (extendedOpts.global) {
            const independentLeavesSuffix = extendedOpts.independentLeaves ? '_independent_leaves' : '';
            const shamefullyFlattenSuffix = extendedOpts.shamefullyFlatten ? '_shamefully_flatten' : '';
            const subfolder = modulesController_1.LAYOUT_VERSION.toString() + independentLeavesSuffix + shamefullyFlattenSuffix;
            extendedOpts.prefix = path.join(extendedOpts.prefix, subfolder);
        }
        extendedOpts.rawNpmConfig['registry'] = extendedOpts.registry; // tslint:disable-line:no-string-literal
        // if sideEffectsCacheReadonly is true, sideEffectsCache is necessarily true too
        if (extendedOpts.sideEffectsCache && extendedOpts.sideEffectsCacheReadonly) {
            logger_1.default.warn("--side-effects-cache-readonly turns on side effects cache too, you don't need to specify both");
        }
        extendedOpts.sideEffectsCache = extendedOpts.sideEffectsCache || extendedOpts.sideEffectsCacheReadonly;
    }
    return extendedOpts;
});
//# sourceMappingURL=extendInstallOptions.js.map