"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 });
const path_1 = require("path");
const fs_extra_1 = require("fs-extra");
const system_settings_1 = require("@ewizardjs/system-settings");
const settings_1 = require("@ewizardjs/settings");
const content_checker_1 = require("@ewizardjs/content-checker");
const Logger_1 = require("../modules/Logger");
const I18n_1 = require("../modules/I18n");
const references_api_1 = require("@ewizardjs/references-api");
function updateReferencesJson(presentationPath) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            const DEFAULT_PATHS = (0, system_settings_1.getDefaultPaths)();
            const OLD_REFERENCES_PATH = 'common/i18n/references.json';
            const DEFAULT_REFERENCES_PATH = DEFAULT_PATHS.common.references;
            const contentSystemSettings = yield (0, system_settings_1.get)(presentationPath);
            const { version } = yield (0, content_checker_1.getFrameworkInfo)(presentationPath);
            const contentSupportsReferences = version
                ? (0, references_api_1.isReferencesSupported)(contentSystemSettings.content.type, contentSystemSettings.content.subtype, version)
                : false;
            if (contentSupportsReferences) {
                const currentReferencesPath = contentSystemSettings.path.common.references;
                const references = yield getReferencesList(presentationPath, currentReferencesPath);
                if (!references) {
                    return;
                }
                if (currentReferencesPath === OLD_REFERENCES_PATH) {
                    yield replaceReferencesJson(presentationPath, DEFAULT_REFERENCES_PATH, references, currentReferencesPath);
                }
                else {
                    yield writeReferencesJson(presentationPath, currentReferencesPath, references);
                }
            }
        }
        catch (e) {
            Logger_1.default.error(e.message || e);
        }
    });
}
exports.default = updateReferencesJson;
function getReferencesList(presentationPath, referencesPath) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            const referencesJson = yield (0, fs_extra_1.readJson)((0, path_1.join)(presentationPath, referencesPath));
            const contentSettings = yield (0, settings_1.getRaw)(presentationPath);
            return (0, references_api_1.getReferences)(referencesJson, contentSettings);
        }
        catch (e) {
            Logger_1.default.info(I18n_1.default.info('CANT_FIND_REFERENCES_JSON', referencesPath));
            return;
        }
    });
}
function writeReferencesJson(presentationPath, targetJsonPath, references) {
    return __awaiter(this, void 0, void 0, function* () {
        yield (0, fs_extra_1.outputFile)((0, path_1.join)(presentationPath, targetJsonPath), JSON.stringify(references, null, 4));
        yield (0, system_settings_1.setByPath)(presentationPath, 'path.common.references', targetJsonPath);
    });
}
function replaceReferencesJson(presentationPath, targetReferencesPath, references, currentReferencesPath) {
    return __awaiter(this, void 0, void 0, function* () {
        yield writeReferencesJson(presentationPath, targetReferencesPath, references);
        if (currentReferencesPath !== targetReferencesPath) {
            Logger_1.default.info(I18n_1.default.info('REFERENCES_JSON_REPLACED'));
            yield (0, fs_extra_1.remove)((0, path_1.join)(presentationPath, currentReferencesPath));
        }
    });
}
//# sourceMappingURL=updateReferencesJson.js.map