"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getThemesList = void 0;
const system_settings_1 = require("@ewizardjs/system-settings");
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
function getThemesList(contentPath) {
    return __awaiter(this, void 0, void 0, function* () {
        const { themes, themesManifest, settings } = (yield (0, system_settings_1.get)(contentPath)).path;
        const settingsPath = (0, path_1.join)(contentPath, settings);
        if (yield isOldThemeSettings(settingsPath)) {
            return [];
        }
        const themesManifestPath = (0, path_1.dirname)(themesManifest) === '.'
            ? (0, path_1.join)(contentPath, themes, themesManifest)
            : (0, path_1.join)(contentPath, themesManifest);
        try {
            const themesJson = yield (0, fs_extra_1.readJson)(themesManifestPath);
            return themesJson.themes;
        }
        catch (_a) {
            return [];
        }
    });
}
exports.getThemesList = getThemesList;
function isOldThemeSettings(settingsPath) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            const { theme } = yield (0, fs_extra_1.readJson)(settingsPath);
            return typeof theme === 'string';
        }
        catch (e) {
            throw new Error(`Not valid content ${e}`);
        }
    });
}
//# sourceMappingURL=getThemesList.js.map