"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const error_1 = __importDefault(require("@pnpm/error"));
const read_project_manifest_1 = require("@pnpm/read-project-manifest");
const path = require("path");
const R = require("ramda");
// property keys that are copied from publishConfig into the manifest
const PUBLISH_CONFIG_WHITELIST = new Set([
    // manifest fields that may make sense to overwrite
    'bin',
    // https://github.com/stereobooster/package.json#package-bundlers
    'main',
    'module',
    'typings',
    'types',
    'exports',
    'browser',
    'esnext',
    'es2015',
    'unpkg',
    'umd:main',
]);
async function makePublishManifest(dir, originalManifest) {
    const publishManifest = {
        ...originalManifest,
    };
    for (const depsField of ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies']) {
        const deps = await makePublishDependencies(dir, originalManifest[depsField]);
        if (deps) {
            publishManifest[depsField] = deps;
        }
    }
    const { publishConfig } = originalManifest;
    if (publishConfig) {
        Object.keys(publishConfig)
            .filter(key => PUBLISH_CONFIG_WHITELIST.has(key))
            .forEach(key => {
            publishManifest[key] = publishConfig[key];
        });
    }
    return publishManifest;
}
exports.default = makePublishManifest;
async function makePublishDependencies(dir, dependencies) {
    if (!dependencies)
        return dependencies;
    const publishDependencies = R.fromPairs(await Promise.all(Object.entries(dependencies)
        .map(async ([depName, depSpec]) => [
        depName,
        await makePublishDependency(depName, depSpec, dir),
    ])));
    return publishDependencies;
}
async function makePublishDependency(depName, depSpec, dir) {
    if (!depSpec.startsWith('workspace:')) {
        return depSpec;
    }
    if (depSpec === 'workspace:*' || depSpec.endsWith('@*')) {
        const { manifest } = await read_project_manifest_1.tryReadProjectManifest(path.join(dir, 'node_modules', depName));
        if (!manifest || !manifest.version) {
            throw new error_1.default('CANNOT_RESOLVE_WORKSPACE_PROTOCOL', `Cannot resolve workspace protocol of dependency "${depName}" ` +
                'because this dependency is not installed. Try running "pnpm install".');
        }
        if (depName !== manifest.name) {
            return `npm:${manifest.name}@${manifest.version}`;
        }
        return manifest.version;
    }
    if (depSpec.startsWith('workspace:./') || depSpec.startsWith('workspace:../')) {
        const { manifest } = await read_project_manifest_1.tryReadProjectManifest(path.join(dir, depSpec.substr(10)));
        if (!manifest || !manifest.name || !manifest.version) {
            throw new error_1.default('CANNOT_RESOLVE_WORKSPACE_PROTOCOL', `Cannot resolve workspace protocol of dependency "${depName}" ` +
                'because this dependency is not installed. Try running "pnpm install".');
        }
        if (manifest.name === depName)
            return `${manifest.version}`;
        return `npm:${manifest.name}@${manifest.version}`;
    }
    depSpec = depSpec.substr(10);
    if (depSpec.includes('@')) {
        return `npm:${depSpec}`;
    }
    return depSpec;
}
//# sourceMappingURL=index.js.map