"use strict";
/**
 * penguins-eggs
 * krill modules: locale-cfg.ts
 * author: Piero Proietti
 * email: piero.proietti@gmail.com
 * license: MIT
 * https://stackoverflow.com/questions/23876782/how-do-i-split-a-typescript-class-into-multiple-files
 */
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const fs_1 = tslib_1.__importDefault(require("fs"));
/* localeCfg
* Enable the configured locales (those set by the user on the
* user page) in /etc/locale.gen, if they are available in the
* target system.
*/
async function localeCfg() {
    /**
      * influence: - locale-gen
      */
    let supporteds = [];
    if (this.distro.familyId === 'debian') {
        // Format: en_US.UTF-8 UTF-8
        supporteds = fs_1.default.readFileSync('/usr/share/i18n/SUPPORTED', 'utf-8').split('\n');
    }
    else if (this.distro.familyId === 'archlinux') {
        // shx.exec('localectl list-locales > /tmp/SUPPORTED') // with await exec don't work!
        const supportedsSource = fs_1.default.readFileSync('/etc/locale.gen', 'utf-8').split('\n');
        // Original Format: #en_US.UTF-8 UTF-8
        for (let line of supportedsSource) {
            if (line.slice(0, 2) !== '# ') { // se non è un commento
                line = line.slice(1); // Rimuove #
            }
            supporteds.push(line);
        }
        // Format: en_US.UTF-8 UTF-8
    }
    const localeGenSource = fs_1.default.readFileSync(`${this.installTarget}/etc/locale.gen`, 'utf-8').split('\n');
    let localeGenDest = '';
    const krillBookmark = '#   Locales enabled by krill\n';
    for (const line of localeGenSource) {
        if (line.includes(krillBookmark)) {
            break;
        }
        localeGenDest += line + '\n';
    }
    localeGenDest += '\n';
    localeGenDest += krillBookmark;
    const locales = [];
    if (this.language !== 'en_US.UTF-8') {
        locales.push('en_US.UTF-8');
    }
    locales.push(this.language);
    for (const supported of supporteds) {
        for (const locale of locales) {
            if (supported.includes(locale)) {
                localeGenDest += `${supported}\n`;
            }
        }
    }
    fs_1.default.writeFileSync(`${this.installTarget}/etc/locale.gen`, localeGenDest);
}
exports.default = localeCfg;
