"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.getUniqueEmails = exports.getAllEmails = void 0;
const globby = require("globby");
const multimatch = require("multimatch");
const ramda_1 = require("ramda");
const upath_1 = require("upath");
const I18n_1 = require("../../../modules/I18n");
const Logger_1 = require("../../../modules/Logger");
function getAllEmails(emailsFolder) {
    return __awaiter(this, void 0, void 0, function* () {
        const existingEmails = yield getEmailsPaths('**/App.vue', emailsFolder);
        const duplicatedEmails = getDuplicatedEmails(existingEmails);
        duplicatedEmails.length && Logger_1.default.error(I18n_1.default.error('EMAIL_DUPLICATED', duplicatedEmails.join('\n')));
        return ramda_1.without(duplicatedEmails, existingEmails);
    });
}
exports.getAllEmails = getAllEmails;
function getUniqueEmails(emailsFolder, emails) {
    return __awaiter(this, void 0, void 0, function* () {
        const uniqueEmails = [...new Set(emails)];
        const patterns = uniqueEmails.map((email) => `**/${email}/App.vue`);
        const existingEmails = yield getEmailsPaths(patterns, emailsFolder);
        const nonExistentEmails = getNonExistentEmails(uniqueEmails, existingEmails);
        const duplicatedEmails = getDuplicatedEmails(existingEmails);
        nonExistentEmails.length && Logger_1.default.error(I18n_1.default.error('EMAIL_NOT_EXISTS', nonExistentEmails.join(', ')));
        duplicatedEmails.length && Logger_1.default.error(I18n_1.default.error('EMAIL_DUPLICATED', duplicatedEmails.join('\n')));
        return ramda_1.without(duplicatedEmails, existingEmails);
    });
}
exports.getUniqueEmails = getUniqueEmails;
function getNonExistentEmails(passedEmails, existingPaths) {
    return passedEmails.filter((email) => !multimatch(existingPaths, upath_1.resolve('**', email), { dot: true }).length);
}
function getDuplicatedEmails(existingPaths) {
    const duplicatedPaths = existingPaths.filter((email) => multimatch(existingPaths, upath_1.basename(email), { dot: true, matchBase: true }).length > 1);
    return [...new Set(duplicatedPaths)];
}
function getEmailsPaths(patterns, emailsFolder) {
    return __awaiter(this, void 0, void 0, function* () {
        const emailPaths = yield globby(patterns, {
            cwd: emailsFolder,
            ignore: ['**/node_modules', '**/build'],
            absolute: true,
        });
        return emailPaths.map((path) => upath_1.dirname(path));
    });
}
//# sourceMappingURL=getEmailsList.js.map