"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertLockfileV9ToLockfileObject = convertLockfileV9ToLockfileObject;
const pnpmKitV9 = __importStar(require("@rushstack/rush-pnpm-kit-v9"));
const objectUtilities_1 = require("../../utilities/objectUtilities");
// eslint-disable-next-line @typescript-eslint/typedef
const DEPENDENCIES_FIELDS = ['optionalDependencies', 'dependencies', 'devDependencies'];
function revertProjectSnapshot(from) {
    const specifiers = {};
    function moveSpecifiers(fromDep) {
        const resolvedDependencies = {};
        for (const [depName, { specifier, version }] of Object.entries(fromDep)) {
            const existingValue = specifiers[depName];
            if (existingValue != null && existingValue !== specifier) {
                throw new Error(`Project snapshot lists the same dependency more than once with conflicting versions: ${depName}`);
            }
            specifiers[depName] = specifier;
            resolvedDependencies[depName] = version;
        }
        return resolvedDependencies;
    }
    const dependencies = from.dependencies == null ? from.dependencies : moveSpecifiers(from.dependencies);
    const devDependencies = from.devDependencies == null ? from.devDependencies : moveSpecifiers(from.devDependencies);
    const optionalDependencies = from.optionalDependencies == null ? from.optionalDependencies : moveSpecifiers(from.optionalDependencies);
    return {
        ...(0, objectUtilities_1.removeNullishProps)({
            ...from,
            dependencies,
            devDependencies,
            optionalDependencies
        }),
        specifiers
    };
}
function convertFromLockfileFileMutable(lockfileFile) {
    if (typeof (lockfileFile === null || lockfileFile === void 0 ? void 0 : lockfileFile.importers) === 'undefined') {
        lockfileFile.importers = {
            '.': {
                dependenciesMeta: lockfileFile.dependenciesMeta,
                publishDirectory: lockfileFile.publishDirectory
            }
        };
        for (const depType of DEPENDENCIES_FIELDS) {
            if (lockfileFile[depType] != null) {
                lockfileFile.importers['.'][depType] = lockfileFile[depType];
                delete lockfileFile[depType];
            }
        }
    }
    return lockfileFile;
}
function mapValues(obj, mapper) {
    const result = {};
    for (const [key, value] of Object.entries(obj)) {
        result[key] = mapper(value, key);
    }
    return result;
}
/**
 * Convert lockfile v9 object to standard lockfile object.
 *
 * This function will mutate the lockfile object. It will:
 * 1. Ensure importers['.'] exists.
 * 2. Merge snapshots and packages into packages.
 * 3. Extract specifier from importers['xxx'] into the specifiers field.
 */
function convertLockfileV9ToLockfileObject(lockfile) {
    var _a, _b;
    const { importers, ...rest } = convertFromLockfileFileMutable(lockfile);
    const packages = {};
    for (const [depPath, pkg] of Object.entries((_a = lockfile.snapshots) !== null && _a !== void 0 ? _a : {})) {
        const pkgId = pnpmKitV9.dependencyPath.removeSuffix(depPath);
        packages[depPath] = Object.assign(pkg, (_b = lockfile.packages) === null || _b === void 0 ? void 0 : _b[pkgId]);
    }
    return {
        ...rest,
        packages,
        importers: mapValues(importers !== null && importers !== void 0 ? importers : {}, revertProjectSnapshot)
    };
}
//# sourceMappingURL=PnpmShrinkWrapFileConverters.js.map