import { createProxy } from "../util/index.js";
export const EventBus = 
/* @__PURE__ */ createProxy("EventBus");
import { EventBridgeClient, PutEventsCommand, } from "@aws-sdk/client-eventbridge";
import { z } from "zod";
const client = new EventBridgeClient({});
export function createEventBuilder(props) {
    return function createEvent(type, properties) {
        const propertiesSchema = z.object(properties);
        const metadataSchema = props.metadata
            ? z.object(props.metadata)
            : undefined;
        const publish = async (properties, metadata) => {
            console.log("publishing", type, properties);
            await client.send(new PutEventsCommand({
                Entries: [
                    {
                        // @ts-expect-error
                        EventBusName: EventBus[props.bus].eventBusName,
                        Source: "console",
                        Detail: JSON.stringify({
                            properties: propertiesSchema.parse(properties),
                            metadata: (() => {
                                if (metadataSchema) {
                                    return metadataSchema.parse(metadata);
                                }
                                if (props.metadataFn) {
                                    return props.metadataFn();
                                }
                            })(),
                        }),
                        DetailType: type,
                    },
                ],
            }));
        };
        return {
            publish: publish,
            type,
            shape: {
                metadata: {},
                properties: {},
                metadataFn: {},
            },
        };
    };
}
export function EventHandler(_events, cb) {
    return async (event) => {
        await cb({
            type: event["detail-type"],
            properties: event.detail.properties,
            metadata: event.detail.metadata,
            attempts: event.attempts ?? 0,
        });
    };
}
