import { getBackgroundStore } from "./store.js";
import { v4 } from "uuid";
import { isLocking, lock } from "../storage/lock.js";
import { getExpiresAt } from "../expiring-kv.js";
export * from "./store.js";
export * from "./types.js";
export const BACKGROUND_STATIC = "background-static";
export const BACKGROUND_LOCK = "background-lock";
export const DEFAULT_BACKGROUND_EXPIRES_IN = 2 * 60 * 1000; // Default 2 minutes, longest vercel time is 1 minute AFAIK
export function getBackground(data) {
    return getIdentifiedBackground(BACKGROUND_STATIC, data);
}
export async function getIdentifiedBackground(backgroundId, data) {
    const done = await lock(BACKGROUND_LOCK);
    const store = await getBackgroundStore();
    const backgroundKey = v4();
    return get();
    async function get() {
        const existing = await store.get(backgroundId);
        if (existing && !isExpired(existing) && !isLocking() && !process.env.IS_LOCAL) {
            throw new Error(`Background task scheduled and not yet expired, please try again at ${existing.expiresAt}`);
        }
        const background = {
            ...data,
            backgroundId,
            backgroundKey,
            createdAt: new Date().toISOString(),
            expiresAt: data?.expiresAt || getExpiresAt(DEFAULT_BACKGROUND_EXPIRES_IN)
        };
        await store.set(backgroundId, background);
        // Make sure we stored the document that we were thought we were storing
        let stored = await store.get(backgroundId);
        if (stored?.backgroundKey !== backgroundKey) {
            throw new Error("Failed to schedule background task");
        }
        return createBackgroundCompleteFn(background);
    }
    function createBackgroundCompleteFn(background) {
        return async (update) => {
            const current = await store.get(backgroundId);
            if (current?.backgroundKey !== backgroundKey) {
                return;
            }
            const completedAt = new Date().toISOString();
            // Set expiry now so next background task can run when requested
            const updated = {
                ...background,
                ...update,
                expiresAt: completedAt,
                completedAt
            };
            await store.set(backgroundId, updated);
            await done();
        };
    }
    function isExpired(document) {
        const expiresAt = new Date(document.expiresAt).getTime();
        const now = Date.now();
        return expiresAt < now;
    }
}
//# sourceMappingURL=index.js.map