import { jsxDEV as _jsxDEV, Fragment as _Fragment } from "react/jsx-dev-runtime";
import { APIError, formatErrors, getFolderData } from 'payload';
import { buildFolderWhereConstraints, combineWhereConstraints } from 'payload/shared';
import { FolderFileTable, ItemCardGrid } from // eslint-disable-next-line payload/no-imports-from-exports-dir -- This component is returned via server functions, it must reference the exports dir
'../exports/client/index.js';
export const getFolderResultsComponentAndDataHandler = async (args)=>{
    const { req } = args;
    try {
        const res = await getFolderResultsComponentAndData(args);
        return res;
    } catch (err) {
        req.payload.logger.error({
            err,
            msg: `There was an error getting the folder results component and data`
        });
        return formatErrors(err);
    }
};
/**
 * This function is responsible for fetching folder data, building the results component
 * and returns the data and component together.
 */ export const getFolderResultsComponentAndData = async ({ browseByFolder = false, collectionsToDisplay: activeCollectionSlugs, displayAs, folderAssignedCollections, folderID = undefined, req, sort })=>{
    const { payload } = req;
    if (!payload.config.folders) {
        throw new APIError('Folders are not enabled in the configuration.');
    }
    const emptyQuery = {
        id: {
            exists: false
        }
    };
    let collectionSlug = undefined;
    let documentWhere = Array.isArray(activeCollectionSlugs) && !activeCollectionSlugs.length ? emptyQuery : undefined;
    let folderWhere = Array.isArray(activeCollectionSlugs) && !activeCollectionSlugs.length ? emptyQuery : undefined;
    // todo(perf): - collect promises and resolve them in parallel
    for (const activeCollectionSlug of activeCollectionSlugs){
        if (activeCollectionSlug === payload.config.folders.slug) {
            const folderCollectionConstraints = await buildFolderWhereConstraints({
                collectionConfig: payload.collections[activeCollectionSlug].config,
                folderID,
                localeCode: req?.locale,
                req,
                search: typeof req?.query?.search === 'string' ? req.query.search : undefined,
                sort
            });
            if (folderCollectionConstraints) {
                folderWhere = folderCollectionConstraints;
            }
            folderWhere = combineWhereConstraints([
                folderWhere,
                Array.isArray(folderAssignedCollections) && folderAssignedCollections.length && payload.config.folders.collectionSpecific ? {
                    or: [
                        {
                            folderType: {
                                in: folderAssignedCollections
                            }
                        },
                        // if the folderType is not set, it means it accepts all collections and should appear in the results
                        {
                            folderType: {
                                exists: false
                            }
                        }
                    ]
                } : undefined
            ]);
        } else if (browseByFolder && folderID || !browseByFolder) {
            if (!browseByFolder) {
                collectionSlug = activeCollectionSlug;
            }
            if (!documentWhere) {
                documentWhere = {
                    or: []
                };
            }
            const collectionConstraints = await buildFolderWhereConstraints({
                collectionConfig: payload.collections[activeCollectionSlug].config,
                folderID,
                localeCode: req?.locale,
                req,
                search: typeof req?.query?.search === 'string' ? req.query.search : undefined,
                sort
            });
            if (collectionConstraints) {
                documentWhere.or.push(collectionConstraints);
            }
        }
    }
    const folderData = await getFolderData({
        collectionSlug,
        documentWhere,
        folderID,
        folderWhere,
        req,
        sort
    });
    let FolderResultsComponent = null;
    if (displayAs === 'grid') {
        FolderResultsComponent = /*#__PURE__*/ _jsxDEV("div", {
            children: [
                folderData.subfolders.length ? /*#__PURE__*/ _jsxDEV(_Fragment, {
                    children: /*#__PURE__*/ _jsxDEV(ItemCardGrid, {
                        items: folderData.subfolders,
                        title: 'Folders',
                        type: "folder"
                    }, void 0, false, {
                        fileName: "src/utilities/getFolderResultsComponentAndData.tsx",
                        lineNumber: 169,
                        columnNumber: 13
                    }, this)
                }, void 0, false) : null,
                folderData.documents.length ? /*#__PURE__*/ _jsxDEV(_Fragment, {
                    children: /*#__PURE__*/ _jsxDEV(ItemCardGrid, {
                        items: folderData.documents,
                        subfolderCount: folderData.subfolders.length,
                        title: 'Documents',
                        type: "file"
                    }, void 0, false, {
                        fileName: "src/utilities/getFolderResultsComponentAndData.tsx",
                        lineNumber: 175,
                        columnNumber: 13
                    }, this)
                }, void 0, false) : null
            ]
        }, void 0, true, {
            fileName: "src/utilities/getFolderResultsComponentAndData.tsx",
            lineNumber: 166,
            columnNumber: 7
        }, this);
    } else {
        FolderResultsComponent = /*#__PURE__*/ _jsxDEV(FolderFileTable, {
            showRelationCell: browseByFolder
        }, void 0, false, {
            fileName: "src/utilities/getFolderResultsComponentAndData.tsx",
            lineNumber: 186,
            columnNumber: 30
        }, this);
    }
    return {
        breadcrumbs: folderData.breadcrumbs,
        documents: folderData.documents,
        folderAssignedCollections: folderData.folderAssignedCollections,
        FolderResultsComponent,
        subfolders: folderData.subfolders
    };
};

//# sourceMappingURL=getFolderResultsComponentAndData.js.map