import { addDurableEvent, deleteDurableEvent, getDurableEvent } from "../../data/index.js";
import { getConfig } from "../../config/index.js";
import { dispatchSchedule } from "./update.js";
const { DURABLE_EVENTS_IMMEDIATE } = process.env;
async function hasDurableEvent(event) {
    const existing = await getDurableEvent(event);
    return !!existing;
}
export async function dispatchEvent(event) {
    let durable = event;
    if (!event.virtual) {
        if (!event.durableEventId || !(await hasDurableEvent(event))) {
            durable = await addDurableEvent(event);
        }
    }
    const config = getConfig();
    if (config.dispatchEvent) {
        await config.dispatchEvent(durable);
    }
    else if (durable.virtual) {
        const { dispatchScheduledDurableEvents } = await import("./dispatch-scheduled.js");
        await dispatchScheduledDurableEvents({
            event
        });
    }
    else if (durable.schedule) {
        await dispatchSchedule(durable);
    }
    return durable;
}
export async function deleteDispatchEvent(event) {
    const { deleteDispatchQStash, isQStash } = await import("./qstash.v1.js");
    if (isQStash()) {
        await deleteDispatchQStash(event);
    }
    await deleteDurableEvent(event);
}
//# sourceMappingURL=event.js.map