'use client';
import { jsxDEV as _jsxDEV, Fragment as _Fragment } from "react/jsx-dev-runtime";
import { getTranslation } from '@payloadcms/translations';
import { useRouter } from 'next/navigation.js';
import { formatAdminURL, formatFilesize } from 'payload/shared';
import React, { Fragment, useEffect } from 'react';
import { useBulkUpload } from '../../elements/BulkUpload/index.js';
import { Button } from '../../elements/Button/index.js';
import { Gutter } from '../../elements/Gutter/index.js';
import { ListControls } from '../../elements/ListControls/index.js';
import { useListDrawerContext } from '../../elements/ListDrawer/Provider.js';
import { useModal } from '../../elements/Modal/index.js';
import { NoListResults } from '../../elements/NoListResults/index.js';
import { PageControls } from '../../elements/PageControls/index.js';
import { RenderCustomComponent } from '../../elements/RenderCustomComponent/index.js';
import { SelectMany } from '../../elements/SelectMany/index.js';
import { useStepNav } from '../../elements/StepNav/index.js';
import { StickyToolbar } from '../../elements/StickyToolbar/index.js';
import { RelationshipProvider } from '../../elements/Table/RelationshipProvider/index.js';
import { ViewDescription } from '../../elements/ViewDescription/index.js';
import { useControllableState } from '../../hooks/useControllableState.js';
import { useConfig } from '../../providers/Config/index.js';
import { useListQuery } from '../../providers/ListQuery/index.js';
import { SelectionProvider } from '../../providers/Selection/index.js';
import { TableColumnsProvider } from '../../providers/TableColumns/index.js';
import { useTranslation } from '../../providers/Translation/index.js';
import { useWindowInfo } from '../../providers/WindowInfo/index.js';
import { ListSelection } from '../../views/List/ListSelection/index.js';
import { CollectionListHeader } from './ListHeader/index.js';
import './index.scss';
const baseClass = 'collection-list';
export function DefaultListView(props) {
    const { AfterList, AfterListTable, beforeActions, BeforeList, BeforeListTable, collectionSlug, columnState, Description, disableBulkDelete, disableBulkEdit, disableQueryPresets, enableRowSelections, hasCreatePermission: hasCreatePermissionFromProps, hasDeletePermission, hasTrashPermission, listMenuItems, newDocumentURL, queryPreset, queryPresetPermissions, renderedFilters, resolvedFilterOptions, Table: InitialTable, viewType } = props;
    const [Table] = useControllableState(InitialTable);
    const { allowCreate, createNewDrawerSlug, isInDrawer, onBulkSelect } = useListDrawerContext();
    const hasCreatePermission = allowCreate !== undefined ? allowCreate && hasCreatePermissionFromProps : hasCreatePermissionFromProps;
    const { config: { routes: { admin: adminRoute }, serverURL }, getEntityConfig } = useConfig();
    const router = useRouter();
    const { data, isGroupingBy } = useListQuery();
    const { openModal } = useModal();
    const { drawerSlug: bulkUploadDrawerSlug, setCollectionSlug, setOnSuccess } = useBulkUpload();
    const collectionConfig = getEntityConfig({
        collectionSlug
    });
    const { labels, upload } = collectionConfig;
    const isUploadCollection = Boolean(upload);
    const isBulkUploadEnabled = isUploadCollection && collectionConfig.upload.bulkUpload;
    const isTrashEnabled = Boolean(collectionConfig.trash);
    const { i18n } = useTranslation();
    const { setStepNav } = useStepNav();
    const { breakpoints: { s: smallBreak } } = useWindowInfo();
    const docs = React.useMemo(()=>{
        if (isUploadCollection) {
            return data.docs.map((doc)=>{
                return {
                    ...doc,
                    filesize: formatFilesize(doc.filesize)
                };
            });
        } else {
            return data?.docs;
        }
    }, [
        data?.docs,
        isUploadCollection
    ]);
    const openBulkUpload = React.useCallback(()=>{
        setCollectionSlug(collectionSlug);
        openModal(bulkUploadDrawerSlug);
        setOnSuccess(()=>router.refresh());
    }, [
        router,
        collectionSlug,
        bulkUploadDrawerSlug,
        openModal,
        setCollectionSlug,
        setOnSuccess
    ]);
    useEffect(()=>{
        if (!isInDrawer) {
            const baseLabel = {
                label: getTranslation(labels?.plural, i18n),
                url: isTrashEnabled && viewType === 'trash' ? formatAdminURL({
                    adminRoute,
                    path: `/collections/${collectionSlug}`
                }) : undefined
            };
            const trashLabel = {
                label: i18n.t('general:trash')
            };
            const navItems = isTrashEnabled && viewType === 'trash' ? [
                baseLabel,
                trashLabel
            ] : [
                baseLabel
            ];
            setStepNav(navItems);
        }
    }, [
        adminRoute,
        setStepNav,
        serverURL,
        labels,
        isInDrawer,
        isTrashEnabled,
        viewType,
        i18n,
        collectionSlug
    ]);
    return /*#__PURE__*/ _jsxDEV(Fragment, {
        children: [
            /*#__PURE__*/ _jsxDEV(TableColumnsProvider, {
                collectionSlug: collectionSlug,
                columnState: columnState,
                children: /*#__PURE__*/ _jsxDEV("div", {
                    className: `${baseClass} ${baseClass}--${collectionSlug}`,
                    children: /*#__PURE__*/ _jsxDEV(SelectionProvider, {
                        docs: docs,
                        totalDocs: data?.totalDocs,
                        children: [
                            BeforeList,
                            /*#__PURE__*/ _jsxDEV(Gutter, {
                                className: `${baseClass}__wrap`,
                                children: [
                                    /*#__PURE__*/ _jsxDEV(CollectionListHeader, {
                                        collectionConfig: collectionConfig,
                                        Description: Description || collectionConfig?.admin?.description ? /*#__PURE__*/ _jsxDEV("div", {
                                            className: `${baseClass}__sub-header`,
                                            children: /*#__PURE__*/ _jsxDEV(RenderCustomComponent, {
                                                CustomComponent: Description,
                                                Fallback: /*#__PURE__*/ _jsxDEV(ViewDescription, {
                                                    collectionSlug: collectionSlug,
                                                    description: collectionConfig?.admin?.description
                                                }, void 0, false, {
                                                    fileName: "src/views/List/index.tsx",
                                                    lineNumber: 173,
                                                    columnNumber: 27
                                                }, void 0)
                                            }, void 0, false, {
                                                fileName: "src/views/List/index.tsx",
                                                lineNumber: 170,
                                                columnNumber: 23
                                            }, void 0)
                                        }, void 0, false, {
                                            fileName: "src/views/List/index.tsx",
                                            lineNumber: 169,
                                            columnNumber: 21
                                        }, void 0) : undefined,
                                        disableBulkDelete: disableBulkDelete,
                                        disableBulkEdit: disableBulkEdit,
                                        hasCreatePermission: hasCreatePermission,
                                        hasDeletePermission: hasDeletePermission,
                                        hasTrashPermission: hasTrashPermission,
                                        i18n: i18n,
                                        isBulkUploadEnabled: isBulkUploadEnabled && !upload.hideFileInputOnCreate,
                                        isTrashEnabled: isTrashEnabled,
                                        newDocumentURL: newDocumentURL,
                                        openBulkUpload: openBulkUpload,
                                        smallBreak: smallBreak,
                                        viewType: viewType
                                    }, void 0, false, {
                                        fileName: "src/views/List/index.tsx",
                                        lineNumber: 165,
                                        columnNumber: 15
                                    }, this),
                                    /*#__PURE__*/ _jsxDEV(ListControls, {
                                        beforeActions: enableRowSelections && typeof onBulkSelect === 'function' ? beforeActions ? [
                                            ...beforeActions,
                                            /*#__PURE__*/ _jsxDEV(SelectMany, {
                                                onClick: onBulkSelect
                                            }, "select-many", false, {
                                                fileName: "src/views/List/index.tsx",
                                                lineNumber: 199,
                                                columnNumber: 44
                                            }, void 0)
                                        ] : [
                                            /*#__PURE__*/ _jsxDEV(SelectMany, {
                                                onClick: onBulkSelect
                                            }, "select-many", false, {
                                                fileName: "src/views/List/index.tsx",
                                                lineNumber: 200,
                                                columnNumber: 26
                                            }, void 0)
                                        ] : beforeActions,
                                        collectionConfig: collectionConfig,
                                        collectionSlug: collectionSlug,
                                        disableQueryPresets: collectionConfig?.enableQueryPresets !== true || disableQueryPresets,
                                        listMenuItems: listMenuItems,
                                        queryPreset: queryPreset,
                                        queryPresetPermissions: queryPresetPermissions,
                                        renderedFilters: renderedFilters,
                                        resolvedFilterOptions: resolvedFilterOptions
                                    }, void 0, false, {
                                        fileName: "src/views/List/index.tsx",
                                        lineNumber: 195,
                                        columnNumber: 15
                                    }, this),
                                    BeforeListTable,
                                    docs?.length > 0 && /*#__PURE__*/ _jsxDEV("div", {
                                        className: `${baseClass}__tables`,
                                        children: /*#__PURE__*/ _jsxDEV(RelationshipProvider, {
                                            children: Table
                                        }, void 0, false, {
                                            fileName: "src/views/List/index.tsx",
                                            lineNumber: 217,
                                            columnNumber: 19
                                        }, this)
                                    }, void 0, false, {
                                        fileName: "src/views/List/index.tsx",
                                        lineNumber: 216,
                                        columnNumber: 17
                                    }, this),
                                    docs?.length === 0 && /*#__PURE__*/ _jsxDEV(NoListResults, {
                                        Actions: hasCreatePermission && newDocumentURL && viewType !== 'trash' ? [
                                            isInDrawer ? /*#__PURE__*/ _jsxDEV(Button, {
                                                el: "button",
                                                onClick: ()=>openModal(createNewDrawerSlug),
                                                children: i18n.t('general:createNewLabel', {
                                                    label: getTranslation(labels?.singular, i18n)
                                                })
                                            }, "create", false, {
                                                fileName: "src/views/List/index.tsx",
                                                lineNumber: 226,
                                                columnNumber: 29
                                            }, void 0) : /*#__PURE__*/ _jsxDEV(Button, {
                                                el: "link",
                                                to: newDocumentURL,
                                                children: i18n.t('general:createNewLabel', {
                                                    label: getTranslation(labels?.singular, i18n)
                                                })
                                            }, "create", false, {
                                                fileName: "src/views/List/index.tsx",
                                                lineNumber: 236,
                                                columnNumber: 29
                                            }, void 0)
                                        ] : [],
                                        Message: viewType === 'trash' ? /*#__PURE__*/ _jsxDEV("p", {
                                            children: i18n.t('general:noTrashResults', {
                                                label: getTranslation(labels?.plural, i18n)
                                            })
                                        }, void 0, false, {
                                            fileName: "src/views/List/index.tsx",
                                            lineNumber: 247,
                                            columnNumber: 23
                                        }, void 0) : /*#__PURE__*/ _jsxDEV(_Fragment, {
                                            children: [
                                                /*#__PURE__*/ _jsxDEV("h3", {
                                                    children: i18n.t('general:noResultsFound')
                                                }, void 0, false, {
                                                    fileName: "src/views/List/index.tsx",
                                                    lineNumber: 254,
                                                    columnNumber: 25
                                                }, void 0),
                                                /*#__PURE__*/ _jsxDEV("p", {
                                                    children: i18n.t('general:noResultsDescription')
                                                }, void 0, false, {
                                                    fileName: "src/views/List/index.tsx",
                                                    lineNumber: 255,
                                                    columnNumber: 25
                                                }, void 0)
                                            ]
                                        }, void 0, true)
                                    }, void 0, false, {
                                        fileName: "src/views/List/index.tsx",
                                        lineNumber: 221,
                                        columnNumber: 17
                                    }, this),
                                    AfterListTable,
                                    docs?.length > 0 && !isGroupingBy && /*#__PURE__*/ _jsxDEV(PageControls, {
                                        AfterPageControls: smallBreak ? /*#__PURE__*/ _jsxDEV("div", {
                                            className: `${baseClass}__list-selection`,
                                            children: [
                                                /*#__PURE__*/ _jsxDEV(ListSelection, {
                                                    collectionConfig: collectionConfig,
                                                    disableBulkDelete: disableBulkDelete,
                                                    disableBulkEdit: disableBulkEdit,
                                                    label: getTranslation(collectionConfig.labels.plural, i18n),
                                                    showSelectAllAcrossPages: !isGroupingBy
                                                }, void 0, false, {
                                                    fileName: "src/views/List/index.tsx",
                                                    lineNumber: 267,
                                                    columnNumber: 25
                                                }, void 0),
                                                /*#__PURE__*/ _jsxDEV("div", {
                                                    className: `${baseClass}__list-selection-actions`,
                                                    children: enableRowSelections && typeof onBulkSelect === 'function' ? beforeActions ? [
                                                        ...beforeActions,
                                                        /*#__PURE__*/ _jsxDEV(SelectMany, {
                                                            onClick: onBulkSelect
                                                        }, "select-many", false, {
                                                            fileName: "src/views/List/index.tsx",
                                                            lineNumber: 279,
                                                            columnNumber: 35
                                                        }, void 0)
                                                    ] : [
                                                        /*#__PURE__*/ _jsxDEV(SelectMany, {
                                                            onClick: onBulkSelect
                                                        }, "select-many", false, {
                                                            fileName: "src/views/List/index.tsx",
                                                            lineNumber: 281,
                                                            columnNumber: 34
                                                        }, void 0)
                                                    ] : beforeActions
                                                }, void 0, false, {
                                                    fileName: "src/views/List/index.tsx",
                                                    lineNumber: 274,
                                                    columnNumber: 25
                                                }, void 0)
                                            ]
                                        }, void 0, true, {
                                            fileName: "src/views/List/index.tsx",
                                            lineNumber: 266,
                                            columnNumber: 23
                                        }, void 0) : null,
                                        collectionConfig: collectionConfig
                                    }, void 0, false, {
                                        fileName: "src/views/List/index.tsx",
                                        lineNumber: 263,
                                        columnNumber: 17
                                    }, this)
                                ]
                            }, void 0, true, {
                                fileName: "src/views/List/index.tsx",
                                lineNumber: 164,
                                columnNumber: 13
                            }, this),
                            AfterList
                        ]
                    }, void 0, true, {
                        fileName: "src/views/List/index.tsx",
                        lineNumber: 162,
                        columnNumber: 11
                    }, this)
                }, void 0, false, {
                    fileName: "src/views/List/index.tsx",
                    lineNumber: 161,
                    columnNumber: 9
                }, this)
            }, void 0, false, {
                fileName: "src/views/List/index.tsx",
                lineNumber: 160,
                columnNumber: 7
            }, this),
            docs?.length > 0 && isGroupingBy && data.totalPages > 1 && /*#__PURE__*/ _jsxDEV(StickyToolbar, {
                children: /*#__PURE__*/ _jsxDEV(PageControls, {
                    collectionConfig: collectionConfig
                }, void 0, false, {
                    fileName: "src/views/List/index.tsx",
                    lineNumber: 297,
                    columnNumber: 11
                }, this)
            }, void 0, false, {
                fileName: "src/views/List/index.tsx",
                lineNumber: 296,
                columnNumber: 9
            }, this)
        ]
    }, void 0, true, {
        fileName: "src/views/List/index.tsx",
        lineNumber: 159,
        columnNumber: 5
    }, this);
}

//# sourceMappingURL=index.js.map