"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/backend/release-notes/release-notes-trigger.lambda.ts
var release_notes_trigger_lambda_exports = {};
__export(release_notes_trigger_lambda_exports, {
  handler: () => handler
});
module.exports = __toCommonJS(release_notes_trigger_lambda_exports);
var aws = __toESM(require("aws-sdk"));

// src/backend/shared/env.lambda-shared.ts
var import_process = require("process");
function requireEnv(name) {
  const result = import_process.env[name];
  if (!result) {
    throw new Error(
      `No value specified for required environment variable: ${name}`
    );
  }
  return result;
}

// src/backend/release-notes/release-notes-trigger.lambda.ts
var SLEEP_TIME = 1e3 * 5;
var handler = async (event) => {
  const SFN_ARN = requireEnv("SFN_ARN");
  const WORKER_QUEUE_URL = requireEnv("WORKER_QUEUE_URL");
  const sqs = new aws.SQS();
  let messages = [];
  console.log("attempting to send messages");
  const requests = event.Records.map((record, index) => {
    if (index > 0 && index % 10 === 0) {
      return sqs.sendMessageBatch({
        Entries: messages,
        QueueUrl: WORKER_QUEUE_URL
      }).promise();
      messages = [];
    } else {
      console.log("message => ", record.body);
      messages.push({ Id: record.messageId, MessageBody: record.body });
      return false;
    }
  }).filter(Boolean);
  if (messages.length) {
    requests.push(
      sqs.sendMessageBatch({
        Entries: messages,
        QueueUrl: WORKER_QUEUE_URL
      }).promise()
    );
  }
  await Promise.all(requests);
  console.log("messages sent");
  if (await isStepFunctionAlreadyRunning()) {
    await new Promise((resolve) => {
      console.log("sleeping");
      setTimeout(() => {
        console.log("Sleeping done for ", SLEEP_TIME);
        resolve();
      }, SLEEP_TIME);
    });
    if (await isStepFunctionAlreadyRunning())
      return;
  }
  const sfn = new aws.StepFunctions();
  const invocation = await sfn.startExecution({
    stateMachineArn: SFN_ARN
  }).promise();
  console.log("invocation done", invocation);
  return;
};
var isStepFunctionAlreadyRunning = async () => {
  const SFN_ARN = requireEnv("SFN_ARN");
  const sfn = new aws.StepFunctions();
  const invocations = await sfn.listExecutions({
    stateMachineArn: SFN_ARN,
    statusFilter: "RUNNING"
  }).promise();
  return invocations.executions.length > 0;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  handler
});
//# sourceMappingURL=index.js.map
