"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasGitDependencies = void 0;
const node_path_1 = require("node:path");
const fs_extra_1 = require("fs-extra");
const npa = require("npm-package-arg");
const ramda_1 = require("ramda");
const I18n_1 = require("../modules/I18n");
const WizError_1 = require("../modules/WizError");
const GIT_PREFIX = 'git+';
function hasGitDependencies(cwd, deps = []) {
    return __awaiter(this, void 0, void 0, function* () {
        if (validateGitDependencies(deps)) {
            return true;
        }
        const packageJsonPath = (0, node_path_1.join)(cwd, 'package.json');
        const { dependencies = {}, devDependencies = {} } = yield (0, fs_extra_1.readJson)(packageJsonPath);
        const templateDeps = Object.assign(Object.assign({}, dependencies), devDependencies);
        return deps.length
            ? hasGitDepsExcludingArgs(deps, templateDeps)
            : validateGitDependencies(Object.values(templateDeps));
    });
}
exports.hasGitDependencies = hasGitDependencies;
function validateGitDependencies(array) {
    return array.some(isGitDependency);
}
function hasGitDepsExcludingArgs(args, dependenciesDictionary) {
    const argsDeps = args.map(getRegistryDependencyName);
    const gitDeps = Object.entries(dependenciesDictionary)
        .filter(([, value]) => isGitDependency(value))
        .map(([key]) => key);
    return !!gitDeps.length && !!(0, ramda_1.without)(argsDeps, gitDeps).length;
}
function getRegistryDependencyName(pkg) {
    try {
        const { name } = npa(pkg);
        if (!name) {
            throw new Error('Package spec is invalid!');
        }
        return name;
    }
    catch (_a) {
        throw new WizError_1.WizError(I18n_1.default.error('PACKAGE_NAME_NOT_EXISTS', pkg));
    }
}
function isGitDependency(dependency) {
    return dependency.startsWith(GIT_PREFIX);
}
//# sourceMappingURL=hasGitDependencies.js.map