"use strict";
/**
 * penguins-eggs
 * class: locales.ts
 * author: Piero Proietti
 * email: piero.proietti@gmail.com
 * license: MIT
 */
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const utils_1 = require("../lib/utils");
const fs_1 = tslib_1.__importDefault(require("fs"));
const distro_1 = tslib_1.__importDefault(require("./distro"));
class Locales {
    /**
       *
       */
    async getEnabled() {
        // Restituisce i locales abilitati in Debian, per manjaro quelli presenti
        // in /etc/locale.gen anche se #disabilitati
        const cmd = 'localectl list-locales';
        const enabledLocales = [];
        const result = await (0, utils_1.exec)(cmd, { capture: true, echo: false, ignore: false });
        if (result.code === 0) {
            const lines = result.data.split('\n');
            for (const line of lines) {
                enabledLocales.push(line.replace(/"/g, '').replace(/'/g, '').trim());
            }
        }
        return enabledLocales;
    }
    /**
       *
       */
    async getSupported() {
        const distro = new distro_1.default();
        let supporteds = [];
        if (distro.familyId === 'debian') {
            supporteds = fs_1.default.readFileSync('/usr/share/i18n/SUPPORTED', 'utf-8').split('\n');
        }
        else if (distro.familyId === 'archlinux') {
            supporteds = (await (0, utils_1.exec)('localectl list-locales', { capture: true, echo: false, ignore: false })).data.split('\n');
        }
        const elements = [];
        for (const elem of supporteds) {
            elements.push(elem.replace(' UTF-8', ''));
        }
        return elements;
    }
    /**
       *
       */
    async getDefault() {
        const file = '/etc/default/locale';
        const cmd = `grep LANG < ${file}|cut -f2 -d=`;
        let defaultLanguage = 'en_US.UTF-8';
        if (fs_1.default.existsSync(file)) {
            const result = await (0, utils_1.exec)(cmd, { capture: true, echo: false, ignore: false });
            if (result.code === 0) {
                defaultLanguage = result.data.replace(/"/g, '').replace(/'/g, '').trim();
            }
        }
        return defaultLanguage;
    }
}
exports.default = Locales;
