"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const environment_component_1 = require("./environment-component");
const deepmerge = __importStar(require("deepmerge"));
const parser_1 = require("../libs/parser");
/**
 * A Plugin to detect WebApp-Components
 * @param props
 */
exports.EnvironmentPlugin = (props) => {
    const path = require('path');
    const result = {
        // identify Isomorphic-App-Components
        applies: (component) => {
            return environment_component_1.isEnvironment(component);
        },
        // convert the component into configuration parts
        // while the component is of Type `any`, its props must be of type `IWebApp`
        process: (component, childConfigs, infrastructureMode) => {
            // when we did not provide a stage, we evaluate all stages, but only with regard to the b
            if (props.stage === undefined) {
                console.log(`No stage specified, load environment ${component.name} in build mode`);
                return {
                    slsConfigs: [],
                    webpackConfigs: [],
                    postBuilds: [],
                    environments: [component]
                };
            }
            else if (props.stage !== component.name) {
                // we ignore any environment that does not match the specified one!
                console.log(`environment ${component.name} does not apply to specified ${props.stage}`);
                return {
                    slsConfigs: [],
                    webpackConfigs: [],
                    postBuilds: []
                };
            }
            console.log("apply environment: ", component);
            return {
                slsConfigs: deepmerge.all([
                    {
                        // set the stage-name
                        provider: {
                            stage: component.name
                        }
                    },
                    component.offlinePort !== undefined && props.parserMode === parser_1.PARSER_MODES.MODE_START ? {
                        provider: {
                            port: component.offlinePort
                        }
                    } : {},
                    // the stage path is valid only
                    component.domain == undefined && props.parserMode === parser_1.PARSER_MODES.MODE_DEPLOY ? {
                        provider: {
                            stage_path: component.name
                        }
                    } : {},
                ]),
                webpackConfigs: [],
                postBuilds: [],
                environments: [component],
                // here, we provide the name of the domain to the upper-level components
                domain: component.domain,
                // and the certArn
                certArn: component.certArn
            };
        }
    };
    return result;
};
//# sourceMappingURL=environment-plugin.js.map