"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.renderWebsite = void 0;
var _clientEventbridge = require("@webiny/aws-sdk/client-eventbridge");
var _utils = require("@webiny/cli-plugin-deploy-pulumi/utils");
/**
 * On every deployment of the Website project application, this plugin ensures all pages created
 * with the Webiny Page Builder application are re-rendered.
 */
const renderWebsite = renderWebsiteParams => {
  return {
    type: "hook-after-deploy",
    name: "hook-after-deploy-website-render",
    async hook(params, context) {
      if (params.inputs.build === false) {
        context.info(`"--no-build" argument detected - skipping Website re-rendering.`);
        return;
      }

      // No need to re-render the website if we're doing a preview.
      if (params.inputs.preview) {
        return;
      }
      if (!renderWebsiteParams.prerender(params)) {
        context.info("Skipping complete website rendering.");
        return;
      }
      const coreOutput = (0, _utils.getStackOutput)({
        folder: "apps/core",
        env: params.env,
        variant: params.variant
      });
      context.info("Issuing a complete website rendering job...");
      try {
        const client = new _clientEventbridge.EventBridgeClient({
          region: coreOutput["region"]
        });
        const result = await client.send(new _clientEventbridge.PutEventsCommand({
          Entries: [{
            Source: "webiny-cli",
            EventBusName: coreOutput["eventBusArn"],
            DetailType: "RenderPages",
            Detail: JSON.stringify({
              path: "*",
              tenant: "*"
            })
          }]
        }));
        const entry = result.Entries?.[0];
        if (entry?.ErrorMessage) {
          throw new Error(entry.ErrorMessage);
        }
        context.success("Website rendering job successfully issued.");
        context.info("Please note that it can take a couple of minutes for the website to be fully updated.");
      } catch (e) {
        context.error(`An error occurred while issuing a website rendering job!`);
        console.log(e);
      }
    }
  };
};
exports.renderWebsite = renderWebsite;

//# sourceMappingURL=renderWebsite.js.map