import executeAccess from '../../auth/executeAccess.js';
import { combineQueries } from '../../database/combineQueries.js';
import { validateQueryPaths } from '../../database/queryValidation/validateQueryPaths.js';
import { afterRead } from '../../fields/hooks/afterRead/index.js';
import { killTransaction } from '../../utilities/killTransaction.js';
import sanitizeInternalFields from '../../utilities/sanitizeInternalFields.js';
import { buildVersionGlobalFields } from '../../versions/buildGlobalFields.js';
export const findVersionsOperation = async (args)=>{
    const { depth, globalConfig, limit, overrideAccess, page, pagination = true, populate, req: { fallbackLocale, locale, payload }, req, select, showHiddenFields, sort, where } = args;
    const versionFields = buildVersionGlobalFields(payload.config, globalConfig);
    try {
        // /////////////////////////////////////
        // Access
        // /////////////////////////////////////
        const accessResults = !overrideAccess ? await executeAccess({
            req
        }, globalConfig.access.readVersions) : true;
        await validateQueryPaths({
            globalConfig,
            overrideAccess,
            req,
            versionFields,
            where
        });
        const fullWhere = combineQueries(where, accessResults);
        // /////////////////////////////////////
        // Find
        // /////////////////////////////////////
        const paginatedDocs = await payload.db.findGlobalVersions({
            global: globalConfig.slug,
            limit: limit ?? 10,
            locale,
            page: page || 1,
            pagination,
            req,
            select,
            sort,
            where: fullWhere
        });
        // /////////////////////////////////////
        // afterRead - Fields
        // /////////////////////////////////////
        let result = {
            ...paginatedDocs,
            docs: await Promise.all(paginatedDocs.docs.map(async (data)=>{
                return {
                    ...data,
                    version: await afterRead({
                        collection: null,
                        context: req.context,
                        depth,
                        doc: {
                            ...data.version,
                            // Patch globalType onto version doc
                            globalType: globalConfig.slug
                        },
                        draft: undefined,
                        fallbackLocale,
                        findMany: true,
                        global: globalConfig,
                        locale,
                        overrideAccess,
                        populate,
                        req,
                        select,
                        showHiddenFields
                    })
                };
            }))
        };
        // /////////////////////////////////////
        // afterRead - Global
        // /////////////////////////////////////
        result = {
            ...result,
            docs: await Promise.all(result.docs.map(async (doc)=>{
                const docRef = doc;
                await globalConfig.hooks.afterRead.reduce(async (priorHook, hook)=>{
                    await priorHook;
                    docRef.version = await hook({
                        context: req.context,
                        doc: doc.version,
                        findMany: true,
                        global: globalConfig,
                        query: fullWhere,
                        req
                    }) || doc.version;
                }, Promise.resolve());
                return docRef;
            }))
        };
        // /////////////////////////////////////
        // Return results
        // /////////////////////////////////////
        result = {
            ...result,
            docs: result.docs.map((doc)=>sanitizeInternalFields(doc))
        };
        return result;
    } catch (error) {
        await killTransaction(req);
        throw error;
    }
};

//# sourceMappingURL=findVersions.js.map