"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.NpmShrinkwrapFile = void 0;
const node_core_library_1 = require("@rushstack/node-core-library");
const BaseShrinkwrapFile_1 = require("../base/BaseShrinkwrapFile");
const DependencySpecifier_1 = require("../DependencySpecifier");
class NpmShrinkwrapFile extends BaseShrinkwrapFile_1.BaseShrinkwrapFile {
    constructor(shrinkwrapJson) {
        super();
        this._shrinkwrapJson = shrinkwrapJson;
        // Normalize the data
        if (!this._shrinkwrapJson.version) {
            this._shrinkwrapJson.version = '';
        }
        if (!this._shrinkwrapJson.name) {
            this._shrinkwrapJson.name = '';
        }
        if (!this._shrinkwrapJson.dependencies) {
            this._shrinkwrapJson.dependencies = {};
        }
        // Workspaces not supported in NPM
        this.isWorkspaceCompatible = false;
    }
    static loadFromFile(shrinkwrapJsonFilename) {
        try {
            const shrinkwrapContent = node_core_library_1.FileSystem.readFile(shrinkwrapJsonFilename);
            return NpmShrinkwrapFile.loadFromString(shrinkwrapContent);
        }
        catch (error) {
            if (node_core_library_1.FileSystem.isNotExistError(error)) {
                return undefined; // file does not exist
            }
            throw new Error(`Error reading "${shrinkwrapJsonFilename}":\n  ${error.message}`);
        }
    }
    static loadFromString(shrinkwrapContent) {
        // strip BOM
        const data = shrinkwrapContent.charCodeAt(0) === 0xfeff ? shrinkwrapContent.slice(1) : shrinkwrapContent;
        // We don't use JsonFile/jju here because shrinkwrap.json is a special NPM file format
        // and typically very large, so we want to load it the same way that NPM does.
        return new NpmShrinkwrapFile(JSON.parse(data));
    }
    /** @override */
    getTempProjectNames() {
        return this._getTempProjectNames(this._shrinkwrapJson.dependencies);
    }
    /** @override */
    serialize() {
        return node_core_library_1.JsonFile.stringify(this._shrinkwrapJson);
    }
    /** @override */
    getTopLevelDependencyVersion(dependencyName) {
        // First, check under tempProjectName, as this is the first place we look during linking.
        const dependencyJson = NpmShrinkwrapFile.tryGetValue(this._shrinkwrapJson.dependencies, dependencyName);
        if (!dependencyJson) {
            return undefined;
        }
        return DependencySpecifier_1.DependencySpecifier.parseWithCache(dependencyName, dependencyJson.version);
    }
    /**
     * @param dependencyName the name of the dependency to get a version for
     * @param tempProjectName the name of the temp project to check for this dependency
     * @param versionRange Not used, just exists to satisfy abstract API contract
     * @override
     */
    tryEnsureDependencyVersion(dependencySpecifier, tempProjectName) {
        // First, check under tempProjectName, as this is the first place we look during linking.
        let dependencyJson = undefined;
        const tempDependency = NpmShrinkwrapFile.tryGetValue(this._shrinkwrapJson.dependencies, tempProjectName);
        if (tempDependency && tempDependency.dependencies) {
            dependencyJson = NpmShrinkwrapFile.tryGetValue(tempDependency.dependencies, dependencySpecifier.packageName);
        }
        // Otherwise look at the root of the shrinkwrap file
        if (!dependencyJson) {
            return this.getTopLevelDependencyVersion(dependencySpecifier.packageName);
        }
        return DependencySpecifier_1.DependencySpecifier.parseWithCache(dependencySpecifier.packageName, dependencyJson.version);
    }
    /** @override */
    getProjectShrinkwrap(project) {
        return undefined;
    }
    /** @override */
    async isWorkspaceProjectModifiedAsync(project, subspace, variant) {
        throw new node_core_library_1.InternalError('Not implemented');
    }
}
exports.NpmShrinkwrapFile = NpmShrinkwrapFile;
//# sourceMappingURL=NpmShrinkwrapFile.js.map