"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var igd_core_1 = require("@intelligentgraphics/igd.core");
var Logger_1 = require("../Logger");
/**
 * Old EServerType enum. DO NOT EDIT OR REMOVE.
 *
 * @enum {number}
 */
var EServerType;
(function (EServerType) {
    EServerType[EServerType["EAIWS"] = 0] = "EAIWS";
    EServerType[EServerType["IGXE"] = 1] = "IGXE";
    EServerType[EServerType["XCALIBUR"] = 2] = "XCALIBUR";
    EServerType[EServerType["CIS"] = 3] = "CIS";
})(EServerType || (EServerType = {}));
var ALPHA_NUMERIC = /^[a-z0-9-_\@]+$/i;
/**
 * Parses a raw igxr object.
 * Ensures proper format and merges settings.
 *
 * @param {IGXR.IGXR_Object} { Servers, Manufacturers, FormatVersion }
 */
exports.parseIGXR = function (_a) {
    var Servers = _a.Servers, Manufacturers = _a.Manufacturers, FormatVersion = _a.FormatVersion;
    if (FormatVersion < igd_core_1.IGXR.CURRENT_VERSION) {
        // TODO: write converter from old version into CURRENT_VERSION
        throw new Error("Unsupported IGXR version " + FormatVersion);
    }
    for (var serverId in Servers) {
        var server = Servers[serverId];
        if (typeof server.Type === "number") {
            server.Type = EServerType[server.Type];
        }
        if (typeof server.Settings !== "object") {
            throw new Error("Missing server settings for " + serverId);
        }
    }
    // checking manufacturers
    for (var manufacturerId in Manufacturers) {
        if (!manufacturerId.match(ALPHA_NUMERIC)) {
            throw new Error("Non-alphanumeric character in manufacturer id " +
                manufacturerId);
        }
        var manufacturer = Manufacturers[manufacturerId];
        if (manufacturer.Server) {
            if (!Servers[manufacturer.Server]) {
                throw new Error("Unknown server " + manufacturer.Server + " of manufacturer " + manufacturerId);
            }
            manufacturer.Settings = tslib_1.__assign(tslib_1.__assign({}, Servers[manufacturer.Server].Settings), manufacturer.Settings);
        }
        for (var catalogId in manufacturer.Catalogs) {
            if (!catalogId.match(ALPHA_NUMERIC)) {
                throw new Error("Non-alphanumeric character in catalog id " + catalogId);
            }
            var catalog = manufacturer.Catalogs[catalogId];
            if (!catalog.Server) {
                catalog.Server = manufacturer.Server;
            }
            if (!catalog.Server || !Servers[catalog.Server]) {
                throw new Error("Unknown server " + catalog.Server + " of catalog " + manufacturerId + "." + catalogId);
            }
            if (manufacturer.Hidden) {
                catalog.Hidden = true;
            }
            // merge down server settings
            catalog.Settings = tslib_1.__assign(tslib_1.__assign({}, manufacturer.Settings), catalog.Settings);
        }
    }
};
/**
 * Adds an IGXR to another IGXR object.
 *
 * @param {IGXR.IGXR_Object} current
 * @param {IGXR.IGXR_Object} addition
 */
exports.addIGXR = function (current, addition) {
    exports.parseIGXR(addition);
    for (var server in addition.Servers) {
        if (!current.Servers[server]) {
            current.Servers[server] = addition.Servers[server];
            continue;
        }
        var tLogMsg = 'skipping already defined server "' + server + '".';
        Logger_1.default.warn("IGXRLoader:", tLogMsg);
    }
    for (var manufacturer in addition.Manufacturers) {
        if (!current.Manufacturers[manufacturer]) {
            current.Manufacturers[manufacturer] =
                addition.Manufacturers[manufacturer];
            continue;
        }
        for (var c in addition.Manufacturers[manufacturer].Catalogs) {
            if (!current.Manufacturers[manufacturer].Catalogs[c]) {
                current.Manufacturers[manufacturer].Catalogs[c] =
                    addition.Manufacturers[manufacturer].Catalogs[c];
                continue;
            }
            var tLogMsg = 'skipping already defined catalog "' +
                manufacturer +
                "." +
                c +
                '".';
            Logger_1.default.warn("IGXRLoader:", tLogMsg);
        }
    }
};
