"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createSyncSystemPlugins = void 0;
var _index = require("@webiny/cli-plugin-deploy-pulumi/plugins/index.js");
var _clientDynamodb = require("@webiny/aws-sdk/client-dynamodb");
var _getSyncSystemOutput = require("@webiny/pulumi-aws/apps/syncSystem/getSyncSystemOutput.js");
var _index2 = require("@webiny/cli-plugin-deploy-pulumi/utils/index.js");
var _createSystemName = require("@webiny/api-sync-system/utils/createSystemName.js");
const getStacks = async params => {
  const {
    env,
    variant
  } = params;
  const syncSystem = (0, _getSyncSystemOutput.getSyncSystemOutput)({
    env
  });
  if (!syncSystem) {
    return {
      syncSystem: null
    };
  }
  const core = (0, _index2.getStackOutput)({
    env,
    variant,
    folder: "apps/core"
  });
  if (!core) {
    return {
      syncSystem,
      core: null
    };
  }
  return {
    syncSystem,
    core
  };
};
/**
 * Note that the plugins will only have effect if the Sync System is deployed in same env as the API system.
 */
const createSyncSystemPlugins = () => {
  return [
  /**
   * After deployment of API system, we need to write the deployment information to the Sync System DynamoDB table.
   * This way Sync System will know which deployments exist.
   */
  (0, _index.createAfterDeployPlugin)(async ({
    env,
    variant
  }) => {
    const {
      syncSystem,
      core
    } = await getStacks({
      env,
      variant
    });
    if (!syncSystem || !core) {
      return;
    }
    const client = (0, _clientDynamodb.getDocumentClient)({
      region: syncSystem.region
    });
    const tableName = syncSystem.dynamoDbName;
    if (!tableName) {
      throw new Error("Sync System DynamoDB table name is not defined.");
    }
    const item = {
      name: (0, _createSystemName.createSystemName)({
        env,
        variant
      }),
      env,
      variant,
      version: process.env.WEBINY_VERSION,
      region: core.region,
      s3Id: core.fileManagerBucketId,
      s3Arn: core.fileManagerBucketArn,
      primaryDynamoDbArn: core.primaryDynamodbTableArn,
      primaryDynamoDbName: core.primaryDynamodbTableName,
      primaryDynamoDbHashKey: core.primaryDynamodbTableHashKey,
      primaryDynamoDbRangeKey: core.primaryDynamodbTableRangeKey,
      elasticsearchDynamodbTableArn: core.elasticsearchDynamodbTableArn,
      elasticsearchDynamodbTableName: core.elasticsearchDynamodbTableName,
      logDynamodbTableArn: core.logDynamodbTableArn,
      logDynamodbTableName: core.logDynamodbTableName,
      cognitoUserPoolId: core.cognitoUserPoolId
    };
    const cmd = new _clientDynamodb.PutCommand({
      TableName: tableName,
      Item: {
        PK: `DEPLOYMENT#${env}#${variant || "unknown"}`,
        SK: `default`,
        GSI1_PK: "DEPLOYMENTS",
        GSI1_SK: `${env}#${variant || "unknown"}`,
        item
      }
    });
    try {
      await client.send(cmd);
    } catch (ex) {
      console.error("Error while writing to Sync System DynamoDB table.");
      console.log(ex.message);
      throw ex;
    }
  }),
  /**
   * After destroy of the deployment, we need to delete the deployment information from the Sync System DynamoDB table.
   */
  (0, _index.createAfterDestroyPlugin)(async ({
    env,
    variant
  }) => {
    const {
      syncSystem,
      core
    } = await getStacks({
      env,
      variant
    });
    if (!syncSystem || !core) {
      return;
    }
    const client = (0, _clientDynamodb.getDocumentClient)({
      region: syncSystem.region
    });
    const tableName = syncSystem.dynamoDbName;
    if (!tableName) {
      throw new Error("Sync System DynamoDB table name is not defined.");
    }
    const cmd = new _clientDynamodb.DeleteCommand({
      TableName: tableName,
      Key: {
        PK: `DEPLOYMENT#${env}#${variant || "unknown"}`,
        SK: `default`
      }
    });
    try {
      await client.send(cmd);
    } catch (ex) {
      console.error("Error while deleting from Sync System DynamoDB table.");
      console.log(ex.message);
      throw ex;
    }
  })];
};
exports.createSyncSystemPlugins = createSyncSystemPlugins;

//# sourceMappingURL=plugins.js.map