import { DURABLE_EVENTS_INTERNAL_SCHEDULE_DEFAULT_DELAY, getConfig } from "../config/index.js";
import { getPeriodicSyncTagRegistration } from "./manager.js";
import { DAY_MS, HOUR_MS, MONTH_MS } from "../data/index.js";
import { isNumberString } from "../is.js";
export async function getPeriodicSyncSchedule(manager) {
    if (!manager) {
        const { periodicSync } = await import("./manager.js");
        return getPeriodicSyncSchedule(periodicSync);
    }
    const config = getConfig();
    const fn = config.getPeriodicSyncSchedule ?? getDefaultPeriodicSyncSchedule;
    return fn(manager);
}
function getCronExpressionFromInterval(interval) {
    const config = getConfig();
    if (config.getCronExpressionFromInterval) {
        return config.getCronExpressionFromInterval(interval);
    }
    if (interval < HOUR_MS) {
        return "0,15,30,45 * * * *";
    }
    if (interval <= DAY_MS) {
        return "0 0 * * *";
    }
    if (interval <= MONTH_MS) {
        return "0 0 1 * *";
    }
    return undefined;
}
export async function getDefaultDurableEventScheduleForPeriodicSyncTag(tag) {
    const { minInterval, createdAt } = await getPeriodicSyncTagRegistration(tag);
    if (!minInterval) {
        // Default Periodic Sync
        let delay = 60000;
        if (isNumberString(DURABLE_EVENTS_INTERNAL_SCHEDULE_DEFAULT_DELAY)) {
            delay = +DURABLE_EVENTS_INTERNAL_SCHEDULE_DEFAULT_DELAY;
        }
        return {
            delay,
            repeat: true
        };
    }
    const cron = getCronExpressionFromInterval(minInterval);
    if (cron) {
        return {
            cron
        };
    }
    return {
        delay: minInterval,
        repeat: true
    };
}
export async function getDefaultPeriodicSyncSchedule(manager) {
    const tags = await manager.getTags();
    return await Promise.all(tags.map(async (tag) => {
        return {
            tag,
            schedule: await getDefaultDurableEventScheduleForPeriodicSyncTag(tag),
        };
    }));
}
//# sourceMappingURL=schedule.js.map