"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 plugin_1 = require("../libs/plugin");
const storage_component_1 = require("./storage-component");
const parser_1 = require("../libs/parser");
const deepmerge = __importStar(require("deepmerge"));
/**
 * A Plugin to detect Storage-Components
 */
exports.StoragePlugin = (props) => {
    const path = require('path');
    const result = {
        // identify Components
        applies: (component) => {
            return storage_component_1.isStorage(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) => {
            const fs = require("fs");
            const path = require("path");
            function createFolder() {
                // start the sls-config
                if (props.parserMode === parser_1.PARSER_MODES.MODE_START) {
                    const targetFolder = ".s3";
                    //check if folder needs to be created or integrated
                    if (!fs.existsSync(targetFolder)) {
                        fs.mkdirSync(targetFolder, { recursive: true });
                    }
                    fs.chmodSync(targetFolder, 0o777);
                }
            }
            /**
             * setup a local S3, only if we start it locally
             */
            const localS3Config = props.parserMode !== parser_1.PARSER_MODES.MODE_START ? {} : {
                plugins: ["serverless-s3-local"],
                /* see: https://www.npmjs.com/package/serverless-dynamodb-local */
                custom: {
                    "s3": {
                        port: 3002,
                        directory: path.join(require("../../../infrastructure-scripts/dist/infra-comp-utils/system-libs").currentAbsolutePath(), ".s3")
                    }
                },
                provider: {
                    staticBucket: "${self:provider.stackName}",
                }
            };
            return {
                slsConfigs: deepmerge.all([
                    localS3Config
                ].concat(childConfigs.map(config => config.slsConfigs))),
                // add the server config
                webpackConfigs: childConfigs.reduce((result, config) => result.concat(config.webpackConfigs), []),
                postBuilds: childConfigs.reduce((result, config) => result.concat(config.postBuilds), [createFolder]),
                iamRoleStatements: plugin_1.forwardChildIamRoleStatements(childConfigs)
            };
        }
    };
    return result;
};
//# sourceMappingURL=storage-plugin.js.map