import { virtual } from "../virtual/virtual.js";
import { DURABLE_EVENTS_INDEX_SCHEDULE, getConfig, KEY_VALUE_STORE_INDEX, DURABLE_EVENTS_IMMEDIATE } from "../../config/index.js";
import { getDurableEventStore, listDurableEventTypes } from "../../data/index.js";
export function isDurableEventDefaultSchedule() {
    return !!DURABLE_EVENTS_IMMEDIATE;
}
export async function dispatchDefaultSchedule(event) {
    const { dispatchScheduledDurableEvents } = await import("./dispatch-scheduled.js");
    const { dispatchInternalSchedule, isInternalSchedule } = await import("./internal.js");
    const { dispatchQStash, isQStash } = await import("./qstash.js");
    if (event.schedule?.immediate) {
        await dispatchScheduledDurableEvents({
            event
        });
    }
    else if (isInternalSchedule()) {
        await dispatchInternalSchedule(event);
    }
    else if (isQStash()) {
        await dispatchQStash(event);
    }
    else if (event.schedule) {
        await dispatchInternalSchedule(event);
    }
    else {
        await dispatchScheduledDurableEvents({
            event
        });
    }
}
export async function dispatchSchedule(event) {
    const config = getConfig();
    const fn = config.dispatchSchedule ?? dispatchDefaultSchedule;
    return fn(event);
}
function isDurableEventIndexScheduleEnabled() {
    return DURABLE_EVENTS_INDEX_SCHEDULE && KEY_VALUE_STORE_INDEX;
}
/**
 * Note that this is only usable if KEY_VALUE_STORE_INDEX is enabled
 */
async function dispatchDefaultIndexSchedules() {
    for (const type of await listDurableEventTypes()) {
        for await (const event of getDurableEventStore({
            type
        })) {
            if (!event.schedule)
                continue;
            await dispatchSchedule(event);
        }
    }
}
async function dispatchDefaultSchedules() {
    if (isDurableEventIndexScheduleEnabled()) {
        await dispatchDefaultIndexSchedules();
    }
}
export async function dispatchSchedules() {
    const config = getConfig();
    const fn = config.dispatchSchedules ?? dispatchDefaultSchedules;
    return fn();
}
export const removeScheduleVirtualFunction = virtual(dispatchSchedules);
export function isScheduleRepeating(schedule) {
    return !!schedule.cron || schedule.repeat;
}
export async function deleteDispatchSchedule(event) {
    const config = getConfig();
    const fn = config.deleteDispatchSchedule ?? deleteDefaultDispatchSchedule;
    return fn(event);
}
export async function clearDispatchSchedule() {
    const config = getConfig();
    const fn = config.clearDispatchSchedule ?? clearDefaultDispatchSchedule;
    return fn();
}
async function deleteDefaultDispatchSchedule(event) {
    const { deleteDispatchInternalSchedule } = await import("./internal.js");
    const { deleteDispatchQStash, isQStash } = await import("./qstash.js");
    if (isQStash()) {
        await deleteDispatchQStash(event);
    }
    else {
        await deleteDispatchInternalSchedule(event);
    }
}
async function clearDefaultDispatchSchedule() {
    const { clearDispatchInternalSchedule } = await import("./internal.js");
    const { clearDispatchQStash, isQStash } = await import("./qstash.js");
    if (isQStash()) {
        await clearDispatchQStash();
    }
    else {
        await clearDispatchInternalSchedule();
    }
}
//# sourceMappingURL=update.js.map