'use client';
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import { useDndMonitor } from '@dnd-kit/core';
import { getTranslation } from '@payloadcms/translations';
import { useRouter } from 'next/navigation.js';
import { formatAdminURL } from 'payload/shared';
import React, { Fragment } from 'react';
import { DefaultListViewTabs } from '../../elements/DefaultListViewTabs/index.js';
import { DroppableBreadcrumb } from '../../elements/FolderView/Breadcrumbs/index.js';
import { ColoredFolderIcon } from '../../elements/FolderView/ColoredFolderIcon/index.js';
import { CurrentFolderActions } from '../../elements/FolderView/CurrentFolderActions/index.js';
import { DragOverlaySelection } from '../../elements/FolderView/DragOverlaySelection/index.js';
import { SortByPill } from '../../elements/FolderView/SortByPill/index.js';
import { ToggleViewButtons } from '../../elements/FolderView/ToggleViewButtons/index.js';
import { Gutter } from '../../elements/Gutter/index.js';
import { ListHeader } from '../../elements/ListHeader/index.js';
import { ListBulkUploadButton, ListCreateNewDocInFolderButton } from '../../elements/ListHeader/TitleActions/index.js';
import { NoListResults } from '../../elements/NoListResults/index.js';
import { SearchBar } from '../../elements/SearchBar/index.js';
import { useStepNav } from '../../elements/StepNav/index.js';
import { useConfig } from '../../providers/Config/index.js';
import { useEditDepth } from '../../providers/EditDepth/index.js';
import { FolderProvider, useFolder } from '../../providers/Folders/index.js';
import { usePreferences } from '../../providers/Preferences/index.js';
import { useRouteCache } from '../../providers/RouteCache/index.js';
import './index.scss';
import { useRouteTransition } from '../../providers/RouteTransition/index.js';
import { useTranslation } from '../../providers/Translation/index.js';
import { useWindowInfo } from '../../providers/WindowInfo/index.js';
import { ListSelection } from './ListSelection/index.js';
const baseClass = 'collection-folder-list';
export function DefaultCollectionFolderView({ allCollectionFolderSlugs: folderCollectionSlugs, allowCreateCollectionSlugs, baseFolderPath, breadcrumbs, documents, folderFieldName, folderID, FolderResultsComponent, search, sort, subfolders, ...restOfProps }) {
    return /*#__PURE__*/ _jsxDEV(FolderProvider, {
        allCollectionFolderSlugs: folderCollectionSlugs,
        allowCreateCollectionSlugs: allowCreateCollectionSlugs,
        baseFolderPath: baseFolderPath,
        breadcrumbs: breadcrumbs,
        documents: documents,
        folderFieldName: folderFieldName,
        folderID: folderID,
        FolderResultsComponent: FolderResultsComponent,
        search: search,
        sort: sort,
        subfolders: subfolders,
        children: /*#__PURE__*/ _jsxDEV(CollectionFolderViewInContext, {
            ...restOfProps
        }, void 0, false, {
            fileName: "src/views/CollectionFolder/index.tsx",
            lineNumber: 69,
            columnNumber: 7
        }, this)
    }, void 0, false, {
        fileName: "src/views/CollectionFolder/index.tsx",
        lineNumber: 56,
        columnNumber: 5
    }, this);
}
function CollectionFolderViewInContext(props) {
    const { AfterFolderList, AfterFolderListTable, BeforeFolderList, BeforeFolderListTable, collectionSlug, Description, disableBulkDelete, disableBulkEdit, search, viewPreference } = props;
    const { config, getEntityConfig } = useConfig();
    const { i18n, t } = useTranslation();
    const drawerDepth = useEditDepth();
    const { setStepNav } = useStepNav();
    const { setPreference } = usePreferences();
    const { allowCreateCollectionSlugs, breadcrumbs, documents, dragOverlayItem, folderCollectionConfig, folderCollectionSlug, FolderResultsComponent, folderType, getSelectedItems, moveToFolder, refineFolderData, selectedItemKeys, setIsDragging, subfolders } = useFolder();
    const router = useRouter();
    const { startRouteTransition } = useRouteTransition();
    const { clearRouteCache } = useRouteCache();
    const collectionConfig = getEntityConfig({
        collectionSlug
    });
    const { labels, upload } = collectionConfig;
    const isUploadCollection = Boolean(upload);
    const isBulkUploadEnabled = isUploadCollection && collectionConfig.upload.bulkUpload;
    const { breakpoints: { s: smallBreak } } = useWindowInfo();
    const onDragEnd = React.useCallback(async (event)=>{
        if (!event.over) {
            return;
        }
        if (event.over.data.current.type === 'folder' && 'id' in event.over.data.current) {
            try {
                await moveToFolder({
                    itemsToMove: getSelectedItems(),
                    toFolderID: event.over.data.current.id
                });
            } catch (error) {
                // eslint-disable-next-line no-console
                console.error('Error moving items:', error);
            }
            clearRouteCache();
        }
    }, [
        moveToFolder,
        getSelectedItems,
        clearRouteCache
    ]);
    const handleSetViewType = React.useCallback(async (view)=>{
        await setPreference(`${collectionSlug}-collection-folder`, {
            viewPreference: view
        });
        clearRouteCache();
    }, [
        collectionSlug,
        setPreference,
        clearRouteCache
    ]);
    React.useEffect(()=>{
        if (!drawerDepth) {
            setStepNav([
                !breadcrumbs.length ? {
                    label: /*#__PURE__*/ _jsxDEV("div", {
                        className: `${baseClass}__step-nav-icon-label`,
                        children: [
                            /*#__PURE__*/ _jsxDEV(ColoredFolderIcon, {}, void 0, false, {
                                fileName: "src/views/CollectionFolder/index.tsx",
                                lineNumber: 177,
                                columnNumber: 19
                            }, this),
                            getTranslation(labels?.plural, i18n)
                        ]
                    }, "root", true, {
                        fileName: "src/views/CollectionFolder/index.tsx",
                        lineNumber: 176,
                        columnNumber: 17
                    }, this)
                } : {
                    label: /*#__PURE__*/ _jsxDEV(DroppableBreadcrumb, {
                        className: [
                            `${baseClass}__step-nav-droppable`,
                            `${baseClass}__step-nav-icon-label`
                        ].filter(Boolean).join(' '),
                        id: null,
                        onClick: ()=>{
                            startRouteTransition(()=>{
                                if (config.folders) {
                                    router.push(formatAdminURL({
                                        adminRoute: config.routes.admin,
                                        path: `/collections/${collectionSlug}/${config.folders.slug}`
                                    }));
                                }
                            });
                        },
                        children: [
                            /*#__PURE__*/ _jsxDEV(ColoredFolderIcon, {}, void 0, false, {
                                fileName: "src/views/CollectionFolder/index.tsx",
                                lineNumber: 206,
                                columnNumber: 19
                            }, this),
                            getTranslation(labels?.plural, i18n)
                        ]
                    }, "root", true, {
                        fileName: "src/views/CollectionFolder/index.tsx",
                        lineNumber: 184,
                        columnNumber: 17
                    }, this)
                },
                ...breadcrumbs.map((crumb, crumbIndex)=>{
                    return {
                        label: crumbIndex === breadcrumbs.length - 1 ? crumb.name : /*#__PURE__*/ _jsxDEV(DroppableBreadcrumb, {
                            className: `${baseClass}__step-nav-droppable`,
                            id: crumb.id,
                            onClick: ()=>{
                                startRouteTransition(()=>{
                                    if (config.folders) {
                                        router.push(formatAdminURL({
                                            adminRoute: config.routes.admin,
                                            path: `/collections/${collectionSlug}/${config.folders.slug}/${crumb.id}`
                                        }));
                                    }
                                });
                            },
                            children: crumb.name
                        }, crumb.id, false, {
                            fileName: "src/views/CollectionFolder/index.tsx",
                            lineNumber: 217,
                            columnNumber: 17
                        }, this)
                    };
                })
            ]);
        }
    }, [
        breadcrumbs,
        collectionSlug,
        config.folders,
        config.routes.admin,
        config.serverURL,
        drawerDepth,
        i18n,
        labels?.plural,
        router,
        setStepNav,
        startRouteTransition
    ]);
    const totalDocsAndSubfolders = documents.length + subfolders.length;
    return /*#__PURE__*/ _jsxDEV(Fragment, {
        children: [
            /*#__PURE__*/ _jsxDEV(DndEventListener, {
                onDragEnd: onDragEnd,
                setIsDragging: setIsDragging
            }, void 0, false, {
                fileName: "src/views/CollectionFolder/index.tsx",
                lineNumber: 259,
                columnNumber: 7
            }, this),
            /*#__PURE__*/ _jsxDEV("div", {
                className: `${baseClass} ${baseClass}--${collectionSlug}`,
                children: [
                    BeforeFolderList,
                    /*#__PURE__*/ _jsxDEV(Gutter, {
                        className: `${baseClass}__wrap`,
                        children: [
                            /*#__PURE__*/ _jsxDEV(ListHeader, {
                                Actions: [
                                    !smallBreak && /*#__PURE__*/ _jsxDEV(ListSelection, {
                                        disableBulkDelete: disableBulkDelete,
                                        disableBulkEdit: collectionConfig.disableBulkEdit ?? disableBulkEdit,
                                        folderAssignedCollections: Array.isArray(folderType) ? folderType : [
                                            collectionSlug
                                        ]
                                    }, "list-selection", false, {
                                        fileName: "src/views/CollectionFolder/index.tsx",
                                        lineNumber: 267,
                                        columnNumber: 17
                                    }, void 0),
                                    /*#__PURE__*/ _jsxDEV(DefaultListViewTabs, {
                                        collectionConfig: collectionConfig,
                                        config: config,
                                        viewType: "folders"
                                    }, "default-list-actions", false, {
                                        fileName: "src/views/CollectionFolder/index.tsx",
                                        lineNumber: 276,
                                        columnNumber: 15
                                    }, void 0)
                                ].filter(Boolean),
                                AfterListHeaderContent: Description,
                                title: getTranslation(labels?.plural, i18n),
                                TitleActions: [
                                    allowCreateCollectionSlugs.length && /*#__PURE__*/ _jsxDEV(ListCreateNewDocInFolderButton, {
                                        buttonLabel: allowCreateCollectionSlugs.length > 1 ? t('general:createNew') : `${t('general:create')} ${getTranslation(folderCollectionConfig.labels?.singular, i18n).toLowerCase()}`,
                                        collectionSlugs: allowCreateCollectionSlugs,
                                        folderAssignedCollections: Array.isArray(folderType) ? folderType : [
                                            collectionSlug
                                        ],
                                        onCreateSuccess: clearRouteCache,
                                        slugPrefix: "create-document--header-pill"
                                    }, "create-new-button", false, {
                                        fileName: "src/views/CollectionFolder/index.tsx",
                                        lineNumber: 287,
                                        columnNumber: 17
                                    }, void 0),
                                    /*#__PURE__*/ _jsxDEV(ListBulkUploadButton, {
                                        collectionSlug: collectionSlug,
                                        hasCreatePermission: allowCreateCollectionSlugs.includes(collectionSlug),
                                        isBulkUploadEnabled: isBulkUploadEnabled
                                    }, "bulk-upload-button", false, {
                                        fileName: "src/views/CollectionFolder/index.tsx",
                                        lineNumber: 302,
                                        columnNumber: 15
                                    }, void 0)
                                ].filter(Boolean)
                            }, void 0, false, {
                                fileName: "src/views/CollectionFolder/index.tsx",
                                lineNumber: 264,
                                columnNumber: 11
                            }, this),
                            /*#__PURE__*/ _jsxDEV(SearchBar, {
                                Actions: [
                                    /*#__PURE__*/ _jsxDEV(SortByPill, {}, "sort-by-pill", false, {
                                        fileName: "src/views/CollectionFolder/index.tsx",
                                        lineNumber: 312,
                                        columnNumber: 15
                                    }, void 0),
                                    /*#__PURE__*/ _jsxDEV(ToggleViewButtons, {
                                        activeView: viewPreference,
                                        setActiveView: handleSetViewType
                                    }, "toggle-view-buttons", false, {
                                        fileName: "src/views/CollectionFolder/index.tsx",
                                        lineNumber: 313,
                                        columnNumber: 15
                                    }, void 0),
                                    /*#__PURE__*/ _jsxDEV(CurrentFolderActions, {}, "current-folder-actions", false, {
                                        fileName: "src/views/CollectionFolder/index.tsx",
                                        lineNumber: 318,
                                        columnNumber: 15
                                    }, void 0)
                                ].filter(Boolean),
                                label: t('general:searchBy', {
                                    label: t('general:name')
                                }),
                                onSearchChange: (search)=>refineFolderData({
                                        query: {
                                            search
                                        },
                                        updateURL: true
                                    }),
                                searchQueryParam: search
                            }, void 0, false, {
                                fileName: "src/views/CollectionFolder/index.tsx",
                                lineNumber: 310,
                                columnNumber: 11
                            }, this),
                            BeforeFolderListTable,
                            totalDocsAndSubfolders > 0 && FolderResultsComponent,
                            totalDocsAndSubfolders === 0 && /*#__PURE__*/ _jsxDEV(NoListResults, {
                                Actions: [
                                    allowCreateCollectionSlugs.includes(folderCollectionSlug) && /*#__PURE__*/ _jsxDEV(ListCreateNewDocInFolderButton, {
                                        buttonLabel: `${t('general:create')} ${getTranslation(folderCollectionConfig.labels?.singular, i18n).toLowerCase()}`,
                                        collectionSlugs: [
                                            folderCollectionConfig.slug
                                        ],
                                        folderAssignedCollections: Array.isArray(folderType) ? folderType : [
                                            collectionSlug
                                        ],
                                        onCreateSuccess: clearRouteCache,
                                        slugPrefix: "create-folder--no-results"
                                    }, "create-folder", false, {
                                        fileName: "src/views/CollectionFolder/index.tsx",
                                        lineNumber: 332,
                                        columnNumber: 19
                                    }, void 0),
                                    allowCreateCollectionSlugs.includes(collectionSlug) && /*#__PURE__*/ _jsxDEV(ListCreateNewDocInFolderButton, {
                                        buttonLabel: `${t('general:create')} ${t('general:document').toLowerCase()}`,
                                        collectionSlugs: [
                                            collectionSlug
                                        ],
                                        folderAssignedCollections: Array.isArray(folderType) ? folderType : [
                                            collectionSlug
                                        ],
                                        onCreateSuccess: clearRouteCache,
                                        slugPrefix: "create-document--no-results"
                                    }, "create-document", false, {
                                        fileName: "src/views/CollectionFolder/index.tsx",
                                        lineNumber: 344,
                                        columnNumber: 19
                                    }, void 0)
                                ].filter(Boolean),
                                Message: /*#__PURE__*/ _jsxDEV("p", {
                                    children: i18n.t('general:noResults', {
                                        label: `${getTranslation(labels?.plural, i18n)} ${t('general:or').toLowerCase()} ${getTranslation(folderCollectionConfig.labels?.plural, i18n)}`
                                    })
                                }, void 0, false, {
                                    fileName: "src/views/CollectionFolder/index.tsx",
                                    lineNumber: 357,
                                    columnNumber: 17
                                }, void 0)
                            }, void 0, false, {
                                fileName: "src/views/CollectionFolder/index.tsx",
                                lineNumber: 329,
                                columnNumber: 13
                            }, this),
                            AfterFolderListTable
                        ]
                    }, void 0, true, {
                        fileName: "src/views/CollectionFolder/index.tsx",
                        lineNumber: 263,
                        columnNumber: 9
                    }, this),
                    AfterFolderList
                ]
            }, void 0, true, {
                fileName: "src/views/CollectionFolder/index.tsx",
                lineNumber: 261,
                columnNumber: 7
            }, this),
            selectedItemKeys.size > 0 && dragOverlayItem && /*#__PURE__*/ _jsxDEV(DragOverlaySelection, {
                item: dragOverlayItem,
                selectedCount: selectedItemKeys.size
            }, void 0, false, {
                fileName: "src/views/CollectionFolder/index.tsx",
                lineNumber: 373,
                columnNumber: 9
            }, this)
        ]
    }, void 0, true, {
        fileName: "src/views/CollectionFolder/index.tsx",
        lineNumber: 258,
        columnNumber: 5
    }, this);
}
function DndEventListener({ onDragEnd, setIsDragging }) {
    useDndMonitor({
        onDragCancel () {
            setIsDragging(false);
        },
        onDragEnd (event) {
            setIsDragging(false);
            onDragEnd(event);
        },
        onDragStart () {
            setIsDragging(true);
        }
    });
    return null;
}

//# sourceMappingURL=index.js.map