import { IGXR } from "@intelligentgraphics/igd.core";
import Logger from "../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 = {}));
const ALPHA_NUMERIC = /^[a-z0-9-_\@]+$/i;
/**
 * Parses a raw igxr object.
 * Ensures proper format and merges settings.
 *
 * @param {IGXR.IGXR_Object} { Servers, Manufacturers, FormatVersion }
 */
export const parseIGXR = ({ Servers, Manufacturers, FormatVersion, }) => {
    if (FormatVersion < IGXR.CURRENT_VERSION) {
        // TODO: write converter from old version into CURRENT_VERSION
        throw new Error("Unsupported IGXR version " + FormatVersion);
    }
    for (const serverId in Servers) {
        const 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 (const manufacturerId in Manufacturers) {
        if (!manufacturerId.match(ALPHA_NUMERIC)) {
            throw new Error("Non-alphanumeric character in manufacturer id " +
                manufacturerId);
        }
        const manufacturer = Manufacturers[manufacturerId];
        if (manufacturer.Server) {
            if (!Servers[manufacturer.Server]) {
                throw new Error(`Unknown server ${manufacturer.Server} of manufacturer ${manufacturerId}`);
            }
            manufacturer.Settings = {
                ...Servers[manufacturer.Server].Settings,
                ...manufacturer.Settings,
            };
        }
        for (const catalogId in manufacturer.Catalogs) {
            if (!catalogId.match(ALPHA_NUMERIC)) {
                throw new Error("Non-alphanumeric character in catalog id " + catalogId);
            }
            const 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 = {
                ...manufacturer.Settings,
                ...catalog.Settings,
            };
        }
    }
};
/**
 * Adds an IGXR to another IGXR object.
 *
 * @param {IGXR.IGXR_Object} current
 * @param {IGXR.IGXR_Object} addition
 */
export const addIGXR = (current, addition) => {
    parseIGXR(addition);
    for (const server in addition.Servers) {
        if (!current.Servers[server]) {
            current.Servers[server] = addition.Servers[server];
            continue;
        }
        const tLogMsg = 'skipping already defined server "' + server + '".';
        Logger.warn("IGXRLoader:", tLogMsg);
    }
    for (const manufacturer in addition.Manufacturers) {
        if (!current.Manufacturers[manufacturer]) {
            current.Manufacturers[manufacturer] =
                addition.Manufacturers[manufacturer];
            continue;
        }
        for (const c in addition.Manufacturers[manufacturer].Catalogs) {
            if (!current.Manufacturers[manufacturer].Catalogs[c]) {
                current.Manufacturers[manufacturer].Catalogs[c] =
                    addition.Manufacturers[manufacturer].Catalogs[c];
                continue;
            }
            const tLogMsg = 'skipping already defined catalog "' +
                manufacturer +
                "." +
                c +
                '".';
            Logger.warn("IGXRLoader:", tLogMsg);
        }
    }
};
