"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 __asyncValues = (this && this.__asyncValues) || function (o) {
    if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
    var m = o[Symbol.asyncIterator], i;
    return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
    function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
    function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_extra_1 = __importDefault(require("fs-extra"));
const _fsUtils_1 = require("../utils/_fsUtils");
const _directorys_1 = __importDefault(require("../utils/_directorys"));
const localeKeys = [];
const traverseObjectTree = (obj, pointer) => __awaiter(void 0, void 0, void 0, function* () {
    var e_1, _a;
    try {
        for (var _b = __asyncValues(Object.keys(obj)), _c; _c = yield _b.next(), !_c.done;) {
            const key = _c.value;
            const value = obj[key];
            if (typeof value === 'object') {
                yield traverseObjectTree(value, key);
            }
            if (typeof value === 'string') {
                localeKeys.push(`${pointer}.${key}`);
            }
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
        }
        finally { if (e_1) throw e_1.error; }
    }
});
const auditLocales = (spinner) => __awaiter(void 0, void 0, void 0, function* () {
    var e_2, _d, e_3, _e, e_4, _f;
    const { localesRoot } = _directorys_1.default;
    spinner.stopAndPersist({
        symbol: '🤟',
        text: `Reading default locales from ${localesRoot}`,
    });
    const locales = yield fs_extra_1.default.readdir(localesRoot);
    const files = [...yield (0, _fsUtils_1.auditDirectoryFiles)()];
    try {
        for (var locales_1 = __asyncValues(locales), locales_1_1; locales_1_1 = yield locales_1.next(), !locales_1_1.done;) {
            const locale = locales_1_1.value;
            if (locale.includes('.default')) {
                spinner.stopAndPersist({
                    symbol: '🤟',
                    text: `Auditing ${locale}`,
                });
                const localeContent = yield fs_extra_1.default.readFile(`${localesRoot}/${locale}`);
                const localeSchema = JSON.parse(localeContent.toString());
                yield traverseObjectTree(localeSchema, 0);
            }
        }
    }
    catch (e_2_1) { e_2 = { error: e_2_1 }; }
    finally {
        try {
            if (locales_1_1 && !locales_1_1.done && (_d = locales_1.return)) yield _d.call(locales_1);
        }
        finally { if (e_2) throw e_2.error; }
    }
    const keys = [...localeKeys];
    const keysInUse = [];
    try {
        for (var files_1 = __asyncValues(files), files_1_1; files_1_1 = yield files_1.next(), !files_1_1.done;) {
            const file = files_1_1.value;
            if (file.includes('.liquid')) {
                const fileBuffer = yield fs_extra_1.default.readFile(file);
                const fileContent = fileBuffer.toString();
                try {
                    for (var keys_1 = (e_4 = void 0, __asyncValues(keys)), keys_1_1; keys_1_1 = yield keys_1.next(), !keys_1_1.done;) {
                        const key = keys_1_1.value;
                        if (fileContent.includes(key)) {
                            keysInUse.push(key);
                        }
                    }
                }
                catch (e_4_1) { e_4 = { error: e_4_1 }; }
                finally {
                    try {
                        if (keys_1_1 && !keys_1_1.done && (_f = keys_1.return)) yield _f.call(keys_1);
                    }
                    finally { if (e_4) throw e_4.error; }
                }
            }
        }
    }
    catch (e_3_1) { e_3 = { error: e_3_1 }; }
    finally {
        try {
            if (files_1_1 && !files_1_1.done && (_e = files_1.return)) yield _e.call(files_1);
        }
        finally { if (e_3) throw e_3.error; }
    }
    return keys.map((key) => ![...new Set(keysInUse)].includes(key) && key).filter(Boolean);
});
exports.default = auditLocales;
