"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/cognito/post-confirmation.handler.ts
var post_confirmation_handler_exports = {};
__export(post_confirmation_handler_exports, {
  handler: () => handler
});
module.exports = __toCommonJS(post_confirmation_handler_exports);
var import_client_eventbridge = require("@aws-sdk/client-eventbridge");

// src/workflows/control-plane/user-onboarding/events.ts
var USER_ONBOARDING_EVENT_SOURCE = "openhi.control.user-onboarding";
var PROVISION_DEFAULT_WORKSPACE_DETAIL_TYPE = "ProvisionDefaultWorkspaceRequested";
var buildProvisionDefaultWorkspaceRequestedDetail = (event) => {
  const attrs = event.request?.userAttributes ?? {};
  const cognitoSub = attrs.sub?.trim();
  if (!cognitoSub) {
    return void 0;
  }
  const email = attrs.email?.trim();
  const displayName = email || event.userName || cognitoSub;
  return {
    cognitoSub,
    ...email ? { email } : {},
    displayName,
    trigger: {
      source: "cognito.post-confirmation",
      triggerSource: event.triggerSource,
      userPoolId: event.userPoolId,
      userName: event.userName,
      clientId: event.callerContext?.clientId
    }
  };
};

// src/components/cognito/post-confirmation.handler.ts
var eventBridgeClient = new import_client_eventbridge.EventBridgeClient({});
var handler = async (event, _context) => {
  try {
    const busName = process.env.CONTROL_EVENT_BUS_NAME?.trim();
    const detail = buildProvisionDefaultWorkspaceRequestedDetail(event);
    if (!busName || !detail) {
      console.warn(
        "PostConfirmation onboarding event not published; missing event bus or cognitoSub"
      );
      return event;
    }
    const result = await eventBridgeClient.send(
      new import_client_eventbridge.PutEventsCommand({
        Entries: [
          {
            EventBusName: busName,
            Source: USER_ONBOARDING_EVENT_SOURCE,
            DetailType: PROVISION_DEFAULT_WORKSPACE_DETAIL_TYPE,
            Detail: JSON.stringify(detail)
          }
        ]
      })
    );
    if ((result.FailedEntryCount ?? 0) > 0) {
      console.warn(
        "PostConfirmation onboarding event publication failed",
        result.Entries
      );
    }
  } catch (err) {
    console.warn(
      "PostConfirmation onboarding event publication failed; returning event unchanged",
      err
    );
  }
  return event;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  handler
});
//# sourceMappingURL=post-confirmation.handler.js.map