"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getVueOverride = void 0;
const npm_controller_interface_1 = require("../interfaces/npm-controller.interface");
const parseNpmPackageSpec_1 = require("./parseNpmPackageSpec");
const VUE_OVERRIDE_LINKED = '$vue';
const VUE_OVERRIDE_CAPPED = '~2.6.14';
function getVueOverride({ content, deps, npmCommand }) {
    const { dependencies = {}, devDependencies = {}, overrides = {} } = content;
    const typedOverrides = overrides;
    const hasVueDep = isVueAmongDeps(deps);
    const hasVueInPackageJson = 'vue' in dependencies || 'vue' in devDependencies;
    const hasVueInDeps = npmCommand === npm_controller_interface_1.NpmCommand.Install
        ? hasVueInPackageJson || hasVueDep
        : hasVueInPackageJson && !hasVueDep;
    const newVueOverride = resolveVueOverride(typedOverrides.vue, hasVueInDeps);
    return newVueOverride ? Object.assign(Object.assign({}, typedOverrides), { vue: newVueOverride }) : null;
}
exports.getVueOverride = getVueOverride;
function isVueAmongDeps(deps) {
    return deps.some((dep) => {
        try {
            return (0, parseNpmPackageSpec_1.parseNpmPackageSpec)(dep).package === 'vue';
        }
        catch (_a) {
            return false;
        }
    });
}
function resolveVueOverride(currentOverride, hasVueInDeps) {
    if (currentOverride === VUE_OVERRIDE_CAPPED) {
        return hasVueInDeps ? VUE_OVERRIDE_LINKED : null;
    }
    if (currentOverride === VUE_OVERRIDE_LINKED) {
        return hasVueInDeps ? null : VUE_OVERRIDE_CAPPED;
    }
    if (!currentOverride) {
        return hasVueInDeps ? VUE_OVERRIDE_LINKED : VUE_OVERRIDE_CAPPED;
    }
    return null;
}
//# sourceMappingURL=getVueOverride.js.map