import { combineQueries } from '../database/combineQueries.js';
import { appendVersionToQueryKey } from './drafts/appendVersionToQueryKey.js';
export const getLatestCollectionVersion = async ({ id, config, payload, published, query, req })=>{
    let latestVersion;
    const whereQuery = published ? {
        and: [
            {
                parent: {
                    equals: id
                }
            },
            {
                'version._status': {
                    equals: 'published'
                }
            }
        ]
    } : {
        and: [
            {
                parent: {
                    equals: id
                }
            },
            {
                latest: {
                    equals: true
                }
            }
        ]
    };
    if (config.versions?.drafts) {
        const { docs } = await payload.db.findVersions({
            collection: config.slug,
            limit: 1,
            pagination: false,
            req,
            sort: '-updatedAt',
            where: combineQueries(appendVersionToQueryKey(query.where), whereQuery)
        });
        [latestVersion] = docs;
    }
    if (!latestVersion) {
        if (!published) {
            const doc = await payload.db.findOne({
                ...query,
                req
            });
            return doc;
        }
        return undefined;
    }
    latestVersion.version.id = id;
    return latestVersion.version;
};

//# sourceMappingURL=getLatestCollectionVersion.js.map