"use strict";
/**
 * penguins-eggs
 * class: sources_list.ts
 * author: Piero Proietti
 * email: piero.proietti@gmail.com
 * license: MIT
 */
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const utils_1 = tslib_1.__importDefault(require("./utils"));
const utils_2 = require("../lib/utils");
const distro_1 = tslib_1.__importDefault(require("./distro"));
/**
 *
 */
class SourcesList {
    /**
      *
      * @param repos
      * @returns
      */
    async distribution(distributions) {
        /**
          * Linuxmint non ha nessuna configurazione in /etc/apt/sources.list
          */
        let checked = true;
        checked = false;
        const distro = new distro_1.default();
        for (const distribution of distributions) {
            if (distribution.includes(distro.codenameLikeId)) {
                checked = true;
            }
        }
        return checked;
    }
    /**
      *
      * @param repos
      * @returns
      */
    async components(components) {
        /**
          * Linuxmint non ha nessuna configurazione in /etc/apt/sources.list
          */
        let checked = true;
        const repos = await this.get();
        if (repos.length > 0) {
            for (const repo of repos) {
                for (const component of components) {
                    // On security we need just main
                    if (!repo.includes('security') && !repo.includes(component)) {
                        console.log('component: ' + chalk_1.default.green(component) + ' is not included in repo: ' + chalk_1.default.green(repo));
                        checked = false;
                    }
                }
            }
            if (checked) {
                utils_1.default.warning('repositories checked');
            }
            else {
                utils_1.default.pressKeyToExit('component warning, check your /etc/apt/sources.list', true);
            }
        }
        return checked;
    }
    /**
      *
      */
    async get() {
        let universalSourcesList = '/etc/apt/sources.list';
        const distro = new distro_1.default();
        if (distro.distroId === 'Linuxmint') {
            universalSourcesList = '/etc/apt/sources.list.d/official-package-repositories.list';
        }
        // deb uri distribution [component1] [component2] [...]
        const checkRepos = await (0, utils_2.exec)(`grep "deb http"<${universalSourcesList}`, { echo: false, capture: true });
        let tmp = [];
        if (checkRepos.code === 0) {
            tmp = checkRepos.data.split('\n');
        }
        // remove empty line
        const repos = [];
        for (const repo of tmp) {
            if (repo !== '') {
                repos.push(repo);
            }
        }
        return repos;
    }
}
exports.default = SourcesList;
