import { hasDraftsEnabled } from 'payload/shared';
export const handleStaleDataCheck = async ({ id, collectionSlug, globalSlug, originalUpdatedAt, req })=>{
    let currentUpdatedAt;
    try {
        if (collectionSlug && id) {
            const collection = req.payload.config.collections.find((c)=>c.slug === collectionSlug);
            const collectionHasDrafts = collection ? hasDraftsEnabled(collection) : false;
            // Fetch current document to compare updatedAt
            const currentDoc = await req.payload.findByID({
                id,
                collection: collectionSlug,
                depth: 0,
                draft: collectionHasDrafts,
                overrideAccess: false,
                select: {
                    updatedAt: true
                },
                user: req.user
            });
            currentUpdatedAt = currentDoc?.updatedAt;
        } else if (globalSlug) {
            const global = req.payload.config.globals.find((g)=>g.slug === globalSlug);
            const globalHasDrafts = global ? hasDraftsEnabled(global) : false;
            // Fetch current global to compare updatedAt
            const currentGlobal = await req.payload.findGlobal({
                slug: globalSlug,
                depth: 0,
                draft: globalHasDrafts,
                overrideAccess: false,
                select: {
                    updatedAt: true
                },
                user: req.user
            });
            currentUpdatedAt = currentGlobal?.updatedAt;
        }
        // Compare timestamps
        const isStale = currentUpdatedAt && currentUpdatedAt !== originalUpdatedAt;
        return {
            currentUpdatedAt,
            isStale: Boolean(isStale)
        };
    } catch (err) {
        // If we can't fetch the document, assume it's not stale
        req.payload.logger.error({
            err,
            msg: 'Error checking for stale data'
        });
        return {
            currentUpdatedAt: originalUpdatedAt,
            isStale: false
        };
    }
};

//# sourceMappingURL=handleStaleDataCheck.js.map