import {
  require_lib
} from "./chunk-KMEWULMX.mjs";
import {
  __toESM
} from "./chunk-LZOMFHX3.mjs";

// src/data/operations/control/control-event-publisher.ts
var import_workflows = __toESM(require_lib());
import { EventBridgeClient } from "@aws-sdk/client-eventbridge";
var CONTROL_EVENT_BUS_NAME_ENV_VAR = "CONTROL_EVENT_BUS_NAME";
var cachedClient;
function getClient() {
  if (!cachedClient) {
    cachedClient = new EventBridgeClient({
      region: process.env.AWS_REGION ?? "us-east-1"
    });
  }
  return cachedClient;
}
function actorFromContext(context) {
  return {
    ohi_tid: context.tenantId,
    ohi_wid: context.workspaceId,
    ohi_uid: context.actorId,
    ohi_uname: context.actorName
  };
}
async function publishControlEvent(entry, payload, context) {
  const busName = process.env[CONTROL_EVENT_BUS_NAME_ENV_VAR];
  if (!busName) {
    return;
  }
  try {
    await (0, import_workflows.publishWorkflowEvent)(
      getClient(),
      entry,
      payload,
      { actor: actorFromContext(context) },
      { busNameByPlane: { [import_workflows.OPENHI_CONTROL_SOURCE]: busName } }
    );
  } catch (err) {
    console.error(`control-event publish failed for ${entry.detailType}:`, err);
  }
}
async function publishMembershipCreated(context, detail) {
  await publishControlEvent(import_workflows.ControlPlaneMembershipCreatedV1, detail, context);
}
async function publishMembershipDeleted(context, detail) {
  await publishControlEvent(import_workflows.ControlPlaneMembershipDeletedV1, detail, context);
}
async function publishRoleAssignmentCreated(context, detail) {
  await publishControlEvent(
    import_workflows.ControlPlaneRoleAssignmentCreatedV1,
    detail,
    context
  );
}
async function publishRoleAssignmentDeleted(context, detail) {
  await publishControlEvent(
    import_workflows.ControlPlaneRoleAssignmentDeletedV1,
    detail,
    context
  );
}
async function publishWorkspaceCreated(context, detail) {
  await publishControlEvent(import_workflows.ControlPlaneWorkspaceCreatedV1, detail, context);
}
async function publishWorkspaceDeleted(context, detail) {
  await publishControlEvent(import_workflows.ControlPlaneWorkspaceDeletedV1, detail, context);
}
function extractRoleLevel(resource) {
  const code = resource?.code;
  const first = code?.coding?.[0]?.code;
  return typeof first === "string" && first.length > 0 ? first : void 0;
}

export {
  publishMembershipCreated,
  publishMembershipDeleted,
  publishRoleAssignmentCreated,
  publishRoleAssignmentDeleted,
  publishWorkspaceCreated,
  publishWorkspaceDeleted,
  extractRoleLevel
};
//# sourceMappingURL=chunk-BUAYVN3C.mjs.map