"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
class TranslationsService {
    constructor() {
        this.translationFiles = [];
        this.defaultTranslations = {};
        this.getTranslationFiles();
        this.getDefaultTranslations();
    }
    getTranslationFiles() {
        return __awaiter(this, void 0, void 0, function* () {
            this.translationFiles = yield fs_1.promises.readdir(path_1.default.resolve(process.cwd(), 'translations'));
        });
    }
    getDefaultTranslations() {
        return __awaiter(this, void 0, void 0, function* () {
            this.defaultTranslations = JSON.parse(fs_1.readFileSync(path_1.default.join(path_1.default.resolve(process.cwd(), 'translations'), 'default.json'), 'utf-8'));
        });
    }
    sortTranslations(translations) {
        const sortedKeys = Object.keys(translations).sort();
        return sortedKeys.reduce((prev, curr) => {
            prev[curr] = translations[curr];
            return prev;
        }, {});
    }
    markDiffs(t) {
        const defaultKeys = Object.keys(this.defaultTranslations);
        const translations = Object.assign({}, t);
        for (const key of defaultKeys) {
            if (!(key in translations)) {
                translations[key] = {
                    missing: true,
                    message: this.defaultTranslations[key].message
                };
            }
            else {
                translations[key] = Object.assign(Object.assign({}, translations[key]), { missing: !translations[key].message });
            }
        }
        for (const key of Object.keys(translations)) {
            if (!(key in this.defaultTranslations)) {
                // key should not be here
                delete translations[key];
            }
        }
        return translations;
    }
    readTranslations() {
        return __awaiter(this, void 0, void 0, function* () {
            return this.translationFiles
                .filter(filename => filename.endsWith('json'))
                .reduce((prev, curr) => {
                const fileData = fs_1.readFileSync(path_1.default.join(path_1.default.resolve(process.cwd(), 'translations'), curr), 'utf-8');
                prev[curr] = this.markDiffs(this.sortTranslations(JSON.parse(fileData)));
                return prev;
            }, {});
        });
    }
    byName(name) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.markDiffs(JSON.parse(fs_1.readFileSync(path_1.default.join(path_1.default.resolve(process.cwd(), 'translations'), name), 'utf-8')));
        });
    }
}
exports.TranslationsService = TranslationsService;
//# sourceMappingURL=Translations.js.map