import {
  SEED_SYSTEM_DATA_ACTOR_SYSTEM,
  SEED_SYSTEM_DATA_CONSUMER_NAME,
  SEED_SYSTEM_DATA_CONTROL_BUS_ENV_VAR,
  import_workflows
} from "./chunk-76UM2LQ5.mjs";
import {
  createRoleOperation
} from "./chunk-SD7J3N3C.mjs";
import "./chunk-EUIP2U5F.mjs";
import "./chunk-TRY7JGWO.mjs";
import {
  require_lib
} from "./chunk-KMEWULMX.mjs";
import {
  __toESM
} from "./chunk-LZOMFHX3.mjs";

// src/workflows/control-plane/seed-system-data/seed-system-data.handler.ts
var import_workflows2 = __toESM(require_lib());
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { EventBridgeClient } from "@aws-sdk/client-eventbridge";
import {
  PLATFORM_ROLE_CONCEPTS,
  PLATFORM_ROLE_IDS
} from "@openhi/types";
var errorMessage = (err) => {
  if (err instanceof Error) {
    return err.message;
  }
  return String(err);
};
var idForRoleCode = (code) => {
  for (const key of Object.keys(PLATFORM_ROLE_IDS)) {
    if (PLATFORM_ROLE_CONCEPTS[key].code === code) {
      return PLATFORM_ROLE_IDS[key];
    }
  }
  throw new Error(`No id mapping for role code "${code}".`);
};
var seedSystemRoles = async (context) => {
  for (const concept of Object.values(PLATFORM_ROLE_CONCEPTS)) {
    const id = idForRoleCode(concept.code);
    await createRoleOperation({
      context,
      body: {
        id,
        resource: {
          resourceType: "Role",
          code: concept.code,
          display: concept.display,
          active: true
        }
      }
    });
  }
};
var runSeedSystemData = async (event, deps) => {
  const parsed = (0, import_workflows2.parseWorkflowEvent)(event, import_workflows.PlatformDeploymentCompletedV1);
  const recordResult = await deps.dedupClient.recordIfAbsent({
    consumerName: SEED_SYSTEM_DATA_CONSUMER_NAME,
    eventId: parsed.dedupKey.eventId,
    attempt: parsed.dedupKey.attempt
  });
  if (!recordResult.recorded) {
    return;
  }
  const lastUpdated = parsed.envelope.occurredAt;
  const context = {
    tenantId: "",
    workspaceId: "",
    date: lastUpdated,
    actorId: "platform-deploy-bridge",
    actorName: "Platform Deploy Bridge",
    actorType: "internal-system",
    source: "step-function"
  };
  try {
    await deps.seedRoles(context);
  } catch (err) {
    await deps.dedupClient.markFailed({
      consumerName: SEED_SYSTEM_DATA_CONSUMER_NAME,
      eventId: parsed.dedupKey.eventId,
      attempt: parsed.dedupKey.attempt,
      reason: errorMessage(err)
    });
    throw err;
  }
  const sourcePayload = parsed.envelope.payload;
  await deps.publishSeeded({
    payload: {
      sourceEventId: parsed.envelope.eventId,
      sourceStackId: sourcePayload.stackId,
      seededRecordCount: Object.keys(PLATFORM_ROLE_IDS).length
    },
    envelope: {
      correlationId: parsed.envelope.correlationId,
      causationId: parsed.envelope.eventId
    }
  });
};
var productionDependencies = () => {
  const dynamodb = new DynamoDBClient({});
  const eventBridge = new EventBridgeClient({});
  return {
    dedupClient: (0, import_workflows2.workflowDedupClient)(dynamodb),
    seedRoles: seedSystemRoles,
    publishSeeded: async (input) => {
      const controlBusName = process.env[SEED_SYSTEM_DATA_CONTROL_BUS_ENV_VAR];
      if (!controlBusName) {
        throw new Error(
          `seed-system-data: ${SEED_SYSTEM_DATA_CONTROL_BUS_ENV_VAR} env var is required to publish system-data-seeded events.`
        );
      }
      return (0, import_workflows2.publishWorkflowEvent)(
        eventBridge,
        import_workflows.PlatformSystemDataSeededV1,
        input.payload,
        {
          actor: { system: SEED_SYSTEM_DATA_ACTOR_SYSTEM },
          correlationId: input.envelope.correlationId,
          causationId: input.envelope.causationId
        },
        {
          busNameByPlane: { [import_workflows2.OPENHI_CONTROL_SOURCE]: controlBusName }
        }
      );
    }
  };
};
var handler = async (event) => runSeedSystemData(event, productionDependencies());
export {
  handler,
  runSeedSystemData
};
//# sourceMappingURL=seed-system-data.handler.mjs.map