"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 path_1 = __importDefault(require("path"));
const _fsUtils_1 = require("../utils/_fsUtils");
const auditSettingsSchema = (spinner) => __awaiter(void 0, void 0, void 0, function* () {
    var e_1, _a, e_2, _b;
    const settingsPath = path_1.default.resolve('./shop/src/config/settings_schema.json');
    spinner.stopAndPersist({
        symbol: '🥭',
        text: `Reading settings_schema.json from ${settingsPath}`,
    });
    const settingsContent = yield fs_extra_1.default.readFile(settingsPath);
    const settingsSchema = JSON.parse(settingsContent.toString());
    const files = [...yield (0, _fsUtils_1.auditDirectoryFiles)()];
    const existingSettings = [];
    const settings = settingsSchema.map((schema) => schema && schema.settings);
    const unusedSettings = [];
    const flattened = [...settings.flat()];
    const flattenedSettings = [...flattened.map((value) => value && value.id).filter(Boolean)];
    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;
            spinner.stopAndPersist({
                symbol: '🥭',
                text: `Auditing ${file}`,
            });
            const setting = yield (0, _fsUtils_1.auditFile)(file, flattenedSettings);
            existingSettings.push(setting);
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (files_1_1 && !files_1_1.done && (_a = files_1.return)) yield _a.call(files_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    const settingsInUse = [...new Set(existingSettings.flat().filter(Boolean))];
    try {
        for (var flattenedSettings_1 = __asyncValues(flattenedSettings), flattenedSettings_1_1; flattenedSettings_1_1 = yield flattenedSettings_1.next(), !flattenedSettings_1_1.done;) {
            const setting = flattenedSettings_1_1.value;
            const settingString = `settings.${setting}`;
            if (!settingsInUse.includes(settingString)) {
                unusedSettings.push(settingString);
            }
        }
    }
    catch (e_2_1) { e_2 = { error: e_2_1 }; }
    finally {
        try {
            if (flattenedSettings_1_1 && !flattenedSettings_1_1.done && (_b = flattenedSettings_1.return)) yield _b.call(flattenedSettings_1);
        }
        finally { if (e_2) throw e_2.error; }
    }
    spinner.stopAndPersist({
        symbol: unusedSettings.length >= 1 ? '⛔️' : '🥭',
        text: `There are ${unusedSettings.length} settings unused`,
    });
    return unusedSettings;
});
exports.default = auditSettingsSchema;
