"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createBlueGreenApp = createBlueGreenApp;
var _createBlueGreenPulumiApp = require("@webiny/pulumi-aws/apps/blueGreen/createBlueGreenPulumiApp");
var _plugins = require("@webiny/cli-plugin-deploy-pulumi/plugins");
var _index = require("@webiny/cli-plugin-deploy-pulumi/utils/index.js");
var _chalk = require("chalk");
const builtInPlugins = [(0, _plugins.createBeforeDeployPlugin)(async ({
  env,
  variant
}, context) => {
  if (!variant?.length) {
    return;
  }
  const message = `Cannot deploy Blue / Green system environment (${env}) with a variant (${variant}).`;
  context.error(message);
  throw new Error(message);
}), (0, _plugins.createAfterDeployPlugin)(async ({
  env
}) => {
  const bg = (0, _index.getStackOutput)({
    folder: "apps/blueGreen",
    env,
    /**
     * Blue / Green system cannot have any variants.
     */
    variant: undefined
  });
  const domains = Array.isArray(bg.domains) ? bg.domains : [];
  const environments = (bg.environments || []).reduce((items, item) => {
    const index = items.findIndex(i => i.name === item.name);
    if (index >= 0) {
      items[index].domains[item.type] = item.target;
      return items;
    }
    items.push({
      name: item.name,
      env: item.env,
      variant: item.variant,
      domains: {
        [item.type]: item.target
      }
    });
    return items;
  }, []);
  const output = ["", (0, _chalk.green)(`Blue / Green Router`), `‣ Environment name: ${(0, _chalk.blue)(env)}`, `‣ CloudFront domain: ${bg.distributionDomain}`, `‣ CloudFront URL: ${bg.distributionUrl}`, "", `‣ Domains attached: `, ...domains.map(domain => `  - https://${domain}`), "", `‣ Environments: `, ...environments.map(item => {
    const envVariant = `env: ${item.env}${item.variant ? ` / variant: ${item.variant}` : ""}`;
    return [`  - ${(0, _chalk.blue)(item.name)} (${envVariant})`, ...Object.keys(item.domains).map(type => {
      return `    - ${type}: https://${item.domains[type]}`;
    })].join("\n");
  }), ""];
  console.log(output.join("\n"));
})];
function createBlueGreenApp(projectAppParams) {
  const plugins = projectAppParams.plugins ? [...projectAppParams.plugins] : [];
  return {
    id: "blueGreen",
    name: "Blue / Green System",
    description: "Your project's Blue / Green system.",
    cli: {
      // Default args for the "yarn webiny watch ..." command (we don't need the deploy option while developing).
      watch: {
        // We disable local development for all AWS Lambda functions.
        // This can be changed down the line by passing another set of values
        // to the "watch" command (for example `-f ps-render-subscriber`).
        function: "none"
      }
    },
    pulumi: (0, _createBlueGreenPulumiApp.createBlueGreenPulumiApp)(projectAppParams),
    plugins: plugins.concat([builtInPlugins])
  };
}

//# sourceMappingURL=createBlueGreenApp.js.map