"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 path = require("path");
const pnpm_logger_1 = require("pnpm-logger");
const pnpmPkgJson_1 = require("../pnpmPkgJson");
const modulesController_1 = require("../fs/modulesController");
const normalizeRegistryUrl = require("normalize-registry-url");
const package_store_1 = require("package-store");
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();
    const store = yield package_store_1.resolveStore(opts.store, prefix);
    return {
        fetchRetries: 2,
        fetchRetryFactor: 10,
        fetchRetryMintimeout: 1e4,
        fetchRetryMaxtimeout: 6e4,
        store,
        locks: path.join(store, '_locks'),
        ignoreScripts: false,
        strictSsl: true,
        tag: 'latest',
        production: true,
        development: true,
        bin: path.join(prefix, 'node_modules', '.bin'),
        prefix,
        nodeVersion: process.version,
        force: false,
        depth: 0,
        engineStrict: false,
        metaCache: new Map(),
        networkConcurrency: 16,
        fetchingConcurrency: 16,
        lockStaleDuration: 60 * 1000,
        lock: true,
        childConcurrency: 5,
        offline: false,
        registry: 'https://registry.npmjs.org/',
        userAgent: `${packageManager.name}/${packageManager.version} npm/? node/${process.version} ${process.platform} ${process.arch}`,
        rawNpmConfig: {},
        alwaysAuth: false,
        update: false,
        repeatInstallDepth: -1,
        optional: typeof opts.production === 'boolean' ? opts.production : true,
        independentLeaves: false,
        packageManager,
        verifyStoreIntegrity: true,
        hooks: {},
        savePrefix: '^',
    };
});
exports.default = (opts) => __awaiter(this, void 0, void 0, function* () {
    opts = opts || {};
    if (opts) {
        for (const key in opts) {
            if (opts[key] === undefined) {
                delete opts[key];
            }
        }
    }
    if (opts.storePath && !opts.store) {
        pnpm_logger_1.default.warn('the `store-path` config is deprecated. Use `store` instead.');
        opts.store = opts.storePath;
    }
    const defaultOpts = yield defaults(opts);
    const extendedOpts = Object.assign({}, defaultOpts, opts, { store: defaultOpts.store });
    if (extendedOpts.force) {
        pnpm_logger_1.default.warn('using --force I sure hope you know what you are doing');
    }
    if (extendedOpts.lock === false) {
        pnpm_logger_1.default.warn('using --no-lock I sure hope you know what you are doing');
    }
    if (extendedOpts.userAgent.startsWith('npm/')) {
        extendedOpts.userAgent = `${extendedOpts.packageManager.name}/${extendedOpts.packageManager.version} ${extendedOpts.userAgent}`;
    }
    extendedOpts.registry = normalizeRegistryUrl(extendedOpts.registry);
    if (extendedOpts.global) {
        const subfolder = modulesController_1.LAYOUT_VERSION.toString() + (extendedOpts.independentLeaves ? '_independent_leaves' : '');
        extendedOpts.prefix = path.join(extendedOpts.prefix, subfolder);
    }
    return extendedOpts;
});
//# sourceMappingURL=extendOptions.js.map