"use strict";
/**
 * Created by frank.zickert on 05.04.19.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("./index");
/**
 * loads the specified configuration into a Javascript object
 *
 * @param configFilePath
 */
function loadConfiguration(configFilePath, infrastructureMode) {
    return loadConfigurationFromModule(require(configFilePath), infrastructureMode);
}
exports.loadConfiguration = loadConfiguration;
function loadConfigurationFromModule(configModule, infrastructureMode) {
    return exports.loadInfrastructureComponent(configModule.default, infrastructureMode);
}
exports.loadConfigurationFromModule = loadConfigurationFromModule;
/**
 * Modes of loading, make sure that during RUNTIME mode, only things are used that exist in webpack- web-mode!
 *
 * @type {{COMPILATION: string, RUNTIME: string}}
 */
exports.INFRASTRUCTURE_MODES = {
    /**
     * When packing the application
     */
    COMPILATION: "COMPILATION",
    /**
     * when the app actually runs
     */
    RUNTIME: "RUNTIME"
};
/**
 * Loads an InfrastructureComponent (of any type)
 */
exports.loadInfrastructureComponent = (component, infrastructureMode) => {
    try {
        //console.log("parseInfrastructureComponent: ", component);
        // first load the children!
        const children = index_1.getChildrenArray(component.props).map(child => exports.loadInfrastructureComponent(child, infrastructureMode));
        // overwrite the children in the props
        const props = Object.assign({}, component.props, {
            children: children
        });
        // now load the component at hand
        const params = Object.assign({
            infrastructureMode: infrastructureMode,
        }, props);
        const result = component.type(params);
        //console.log("parsed InfrastructureComponent: ", result);
        if (result.infrastructureType == undefined) {
            console.warn("not an infrastructure-component: ", result);
            return undefined;
        }
        //console.log("parsed: ", parsed);
        return result;
    }
    catch (error) {
        console.error("NOT an infrastructure component --> ", error);
        return undefined;
    }
    ;
};
/**
 * Get the searched object from the configuration, if it exists
 *
 * This function runs on the compiled/webpacked bundle (Plugins removed!!)
 *
 * @param component
 */
function extractObject(component, infrastructureType, instanceId) {
    if (component !== undefined &&
        component.infrastructureType === infrastructureType &&
        component.instanceId === instanceId) {
        return component;
    }
    else {
        return index_1.getChildrenArray(component).reduce((found, child) => found !== undefined ? found : extractObject(child, infrastructureType, instanceId), undefined);
    }
}
exports.extractObject = extractObject;
//# sourceMappingURL=loader.js.map