/******/ (() => { // webpackBootstrap
/******/ 	"use strict";
/******/ 	// The require scope
/******/ 	var __webpack_require__ = {};
/******/ 	
/************************************************************************/
/******/ 	/* webpack/runtime/define property getters */
/******/ 	(() => {
/******/ 		// define getter functions for harmony exports
/******/ 		__webpack_require__.d = (exports, definition) => {
/******/ 			for(var key in definition) {
/******/ 				if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ 					Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ 				}
/******/ 			}
/******/ 		};
/******/ 	})();
/******/ 	
/******/ 	/* webpack/runtime/hasOwnProperty shorthand */
/******/ 	(() => {
/******/ 		__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ 	})();
/******/ 	
/******/ 	/* webpack/runtime/make namespace object */
/******/ 	(() => {
/******/ 		// define __esModule on exports
/******/ 		__webpack_require__.r = (exports) => {
/******/ 			if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ 				Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ 			}
/******/ 			Object.defineProperty(exports, '__esModule', { value: true });
/******/ 		};
/******/ 	})();
/******/ 	
/************************************************************************/
var __webpack_exports__ = {};
/*!*********************************************************!*\
  !*** ./lib-intermediate-esm/logic/pnpm/PnpmfileShim.js ***!
  \*********************************************************/
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   hooks: () => (/* binding */ hooks),
/* harmony export */   reset: () => (/* binding */ reset)
/* harmony export */ });
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
var _a;
let settings;
let allPreferredVersions;
let rangeParseCache;
let allowedAlternativeVersions;
let workspaceVersions;
let userPnpmfile;
let semver;
// All calls to new semver.Range and the comparison functions need to have the same options
// It can be a different object with the same properties, but reusing this const avoids allocations
const SEMVER_COMPARE_OPTIONS = { includePrerelease: true };
// Resets the internal state of the pnpmfile
function reset() {
    settings = undefined;
    allPreferredVersions = undefined;
    allowedAlternativeVersions = undefined;
    workspaceVersions = undefined;
    rangeParseCache = undefined;
    userPnpmfile = undefined;
    semver = undefined;
}
// Initialize all external aspects of the pnpmfile shim. When using the shim, settings
// are always expected to be available. Init must be called before running any hook that
// depends on a resource obtained from or related to the settings, and will require modules
// once so they aren't repeatedly required in the hook functions.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function init(context) {
    // Sometimes PNPM may provide us a context arg that doesn't fit spec, ex.:
    // https://github.com/pnpm/pnpm/blob/97c64bae4d14a8c8f05803f1d94075ee29c2df2f/packages/get-context/src/index.ts#L134
    // So we need to normalize the context format before we move on
    if (typeof context !== 'object' || Array.isArray(context)) {
        context = {
            log: (message) => { },
            originalContext: context
        };
    }
    if (!settings) {
        // Initialize the settings from file
        if (!context.pnpmfileShimSettings) {
            context.pnpmfileShimSettings = require('./pnpmfileSettings.json');
        }
        settings = context.pnpmfileShimSettings;
    }
    else if (!context.pnpmfileShimSettings) {
        // Reuse the already initialized settings
        context.pnpmfileShimSettings = settings;
    }
    if (!allPreferredVersions && settings.allPreferredVersions) {
        allPreferredVersions = new Map(Object.entries(settings.allPreferredVersions));
        rangeParseCache = new Map();
    }
    if (!allowedAlternativeVersions && settings.allowedAlternativeVersions) {
        allowedAlternativeVersions = new Map(Object.entries(settings.allowedAlternativeVersions).map(([packageName, versions]) => {
            return [packageName, new Set(versions)];
        }));
    }
    if (!workspaceVersions && settings.workspaceVersions) {
        workspaceVersions = new Map(Object.entries(settings.workspaceVersions));
    }
    // If a userPnpmfilePath is provided, we expect it to exist
    if (!userPnpmfile && settings.userPnpmfilePath) {
        userPnpmfile = require(settings.userPnpmfilePath);
    }
    // If a semverPath is provided, we expect it to exist
    if (!semver && settings.semverPath) {
        semver = require(settings.semverPath);
    }
    // Return the normalized context
    return context;
}
function parseRange(range) {
    if (!rangeParseCache || !semver) {
        return false;
    }
    const entry = rangeParseCache.get(range);
    if (entry !== undefined) {
        return entry;
    }
    if (range.includes(':')) {
        // This version specifier has a protocol (e.g. npm:, workspace:, etc.), so it is not a normal semver range.
        rangeParseCache.set(range, false);
        return false;
    }
    try {
        const parsedRange = new semver.Range(range, SEMVER_COMPARE_OPTIONS);
        rangeParseCache.set(range, parsedRange);
        return parsedRange;
    }
    catch (_a) {
        rangeParseCache.set(range, false);
        return false;
    }
}
// Set the preferred versions on the dependency map. If the version on the map is an allowedAlternativeVersion
// then skip it. Otherwise, check to ensure that the common version is a subset of the specified version. If
// it is, then replace the specified version with the preferredVersion
function setPreferredVersions(dependencies) {
    if (!dependencies || !semver || !allPreferredVersions) {
        return;
    }
    // Needed for control flow analyzer.
    const definitelyDefinedAllPreferredVersions = allPreferredVersions;
    const definiteSemver = semver;
    Object.entries(dependencies).forEach(([name, version]) => {
        var _a;
        const preferredVersion = definitelyDefinedAllPreferredVersions.get(name);
        // If preferredVersionRange is valid and the current version is not an allowed alternative, proceed to check subsets
        if (preferredVersion && !((_a = allowedAlternativeVersions === null || allowedAlternativeVersions === void 0 ? void 0 : allowedAlternativeVersions.get(name)) === null || _a === void 0 ? void 0 : _a.has(version))) {
            const preferredVersionRange = parseRange(preferredVersion);
            if (!preferredVersionRange) {
                return;
            }
            const versionRange = parseRange(version);
            if (!versionRange) {
                return;
            }
            if (definiteSemver.subset(preferredVersionRange, versionRange, SEMVER_COMPARE_OPTIONS)) {
                dependencies[name] = preferredVersion;
            }
        }
    });
}
const hooks = {
    // Call the original pnpmfile (if it exists)
    afterAllResolved: (lockfile, context) => {
        var _a, _b, _c;
        context = init(context);
        return (_c = (_b = (_a = userPnpmfile === null || userPnpmfile === void 0 ? void 0 : userPnpmfile.hooks) === null || _a === void 0 ? void 0 : _a.afterAllResolved) === null || _b === void 0 ? void 0 : _b.call(_a, lockfile, context)) !== null && _c !== void 0 ? _c : lockfile;
    },
    // Set the preferred versions in the package, then call the original pnpmfile (if it exists)
    readPackage: (pkg, context) => {
        var _a, _b, _c;
        context = init(context);
        // Apply the user pnpmfile readPackage hook first, in case it moves dependencies around, and so that it sees the true package.json
        pkg = (_c = (_b = (_a = userPnpmfile === null || userPnpmfile === void 0 ? void 0 : userPnpmfile.hooks) === null || _a === void 0 ? void 0 : _a.readPackage) === null || _b === void 0 ? void 0 : _b.call(_a, pkg, context)) !== null && _c !== void 0 ? _c : pkg;
        // Then do version refinement of preferredVersions, since this is just supposed to act as if pnpm "prefers" these resolutions during
        // calculation.
        setPreferredVersions(pkg.dependencies);
        if (workspaceVersions && workspaceVersions.get(pkg.name) === pkg.version) {
            // devDependencies are only installed for workspace packages, so the rest of the time we can save the trouble of scanning.
            setPreferredVersions(pkg.devDependencies);
        }
        setPreferredVersions(pkg.optionalDependencies);
        return pkg;
    },
    // Call the original pnpmfile (if it exists)
    filterLog: (_a = userPnpmfile === null || userPnpmfile === void 0 ? void 0 : userPnpmfile.hooks) === null || _a === void 0 ? void 0 : _a.filterLog
};
//# sourceMappingURL=PnpmfileShim.js.map
module.exports = __webpack_exports__;
/******/ })()
;
//# sourceMappingURL=PnpmfileShim.js.map