'use client';
import { jsxDEV as _jsxDEV, Fragment as _Fragment } from "react/jsx-dev-runtime";
import { useDndMonitor } from '@dnd-kit/core';
import { getTranslation } from '@payloadcms/translations';
import { useRouter } from 'next/navigation.js';
import React, { Fragment } from 'react';
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 { FilterFolderTypePill } from '../../elements/FolderView/FilterFolderTypePill/index.js';
import { FolderFileTable } from '../../elements/FolderView/FolderFileTable/index.js';
import { ItemCardGrid } from '../../elements/FolderView/ItemCardGrid/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 { 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 { useRouteTransition } from '../../providers/RouteTransition/index.js';
import { useTranslation } from '../../providers/Translation/index.js';
import { useWindowInfo } from '../../providers/WindowInfo/index.js';
import { ListSelection } from '../CollectionFolder/ListSelection/index.js';
import './index.scss';
const baseClass = 'folder-list';
export function DefaultBrowseByFolderView({ activeCollectionFolderSlugs, allCollectionFolderSlugs, allowCreateCollectionSlugs, baseFolderPath, breadcrumbs, documents, folderFieldName, folderID, FolderResultsComponent, search, subfolders, ...restOfProps }) {
    return /*#__PURE__*/ _jsxDEV(FolderProvider, {
        activeCollectionFolderSlugs: activeCollectionFolderSlugs,
        allCollectionFolderSlugs: allCollectionFolderSlugs,
        allowCreateCollectionSlugs: allowCreateCollectionSlugs,
        baseFolderPath: baseFolderPath,
        breadcrumbs: breadcrumbs,
        documents: documents,
        folderFieldName: folderFieldName,
        folderID: folderID,
        FolderResultsComponent: FolderResultsComponent,
        search: search,
        subfolders: subfolders,
        children: /*#__PURE__*/ _jsxDEV(BrowseByFolderViewInContext, {
            ...restOfProps
        }, void 0, false, {
            fileName: "src/views/BrowseByFolder/index.tsx",
            lineNumber: 67,
            columnNumber: 7
        }, this)
    }, void 0, false, {
        fileName: "src/views/BrowseByFolder/index.tsx",
        lineNumber: 54,
        columnNumber: 5
    }, this);
}
function BrowseByFolderViewInContext(props) {
    const { AfterFolderList, AfterFolderListTable, BeforeFolderList, BeforeFolderListTable, Description, disableBulkDelete, disableBulkEdit, folderAssignedCollections, viewPreference } = props;
    const router = useRouter();
    const { getEntityConfig } = useConfig();
    const { i18n, t } = useTranslation();
    const drawerDepth = useEditDepth();
    const { setStepNav } = useStepNav();
    const { startRouteTransition } = useRouteTransition();
    const { clearRouteCache } = useRouteCache();
    const { breakpoints: { s: smallBreak } } = useWindowInfo();
    const { setPreference } = usePreferences();
    const { activeCollectionFolderSlugs: visibleCollectionSlugs, allowCreateCollectionSlugs, breadcrumbs, documents, dragOverlayItem, folderCollectionConfig, folderID, folderType, getFolderRoute, getSelectedItems, moveToFolder, refineFolderData, search, selectedItemKeys, setIsDragging, subfolders } = useFolder();
    const [activeView, setActiveView] = React.useState(viewPreference || 'grid');
    const [searchPlaceholder] = React.useState(()=>{
        const locationLabel = breadcrumbs.length === 0 ? getTranslation(folderCollectionConfig.labels?.plural, i18n) : breadcrumbs[breadcrumbs.length - 1].name;
        return t('folder:searchByNameInFolder', {
            folderName: locationLabel
        });
    });
    const onDragEnd = React.useCallback(async (event)=>{
        if (!event.over) {
            return;
        }
        if (event.over.data.current.type === 'folder' && 'id' in event.over.data.current) {
            await moveToFolder({
                itemsToMove: getSelectedItems(),
                toFolderID: event.over.data.current.id || null
            });
            clearRouteCache();
        }
    }, [
        moveToFolder,
        getSelectedItems,
        clearRouteCache
    ]);
    const totalDocsAndSubfolders = documents.length + subfolders.length;
    const listHeaderTitle = !breadcrumbs.length ? t('folder:browseByFolder') : breadcrumbs[breadcrumbs.length - 1].name;
    const noResultsLabel = visibleCollectionSlugs.reduce((acc, slug, index, array)=>{
        const collectionConfig = getEntityConfig({
            collectionSlug: slug
        });
        if (index === 0) {
            return getTranslation(collectionConfig.labels?.plural, i18n);
        }
        if (index === array.length - 1) {
            return `${acc} ${t('general:or').toLowerCase()} ${getTranslation(collectionConfig.labels?.plural, i18n)}`;
        }
        return `${acc}, ${getTranslation(collectionConfig.labels?.plural, i18n)}`;
    }, '');
    const handleSetViewType = React.useCallback((view)=>{
        void setPreference('browse-by-folder', {
            viewPreference: view
        });
        setActiveView(view);
    }, [
        setPreference
    ]);
    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/BrowseByFolder/index.tsx",
                                lineNumber: 189,
                                columnNumber: 19
                            }, this),
                            t('folder:browseByFolder')
                        ]
                    }, "root", true, {
                        fileName: "src/views/BrowseByFolder/index.tsx",
                        lineNumber: 188,
                        columnNumber: 17
                    }, this)
                } : {
                    label: /*#__PURE__*/ _jsxDEV(DroppableBreadcrumb, {
                        className: [
                            `${baseClass}__step-nav-droppable`,
                            `${baseClass}__step-nav-icon-label`
                        ].filter(Boolean).join(' '),
                        id: null,
                        onClick: ()=>{
                            startRouteTransition(()=>{
                                router.push(getFolderRoute(null));
                            });
                        },
                        children: [
                            /*#__PURE__*/ _jsxDEV(ColoredFolderIcon, {}, void 0, false, {
                                fileName: "src/views/BrowseByFolder/index.tsx",
                                lineNumber: 211,
                                columnNumber: 19
                            }, this),
                            t('folder:browseByFolder')
                        ]
                    }, "root", true, {
                        fileName: "src/views/BrowseByFolder/index.tsx",
                        lineNumber: 196,
                        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(()=>{
                                    router.push(getFolderRoute(crumb.id));
                                });
                            },
                            children: crumb.name
                        }, crumb.id, false, {
                            fileName: "src/views/BrowseByFolder/index.tsx",
                            lineNumber: 222,
                            columnNumber: 17
                        }, this)
                    };
                })
            ]);
        }
    }, [
        breadcrumbs,
        drawerDepth,
        getFolderRoute,
        router,
        setStepNav,
        startRouteTransition,
        t
    ]);
    const nonFolderCollectionSlugs = allowCreateCollectionSlugs.filter((slug)=>slug !== folderCollectionConfig.slug);
    return /*#__PURE__*/ _jsxDEV(Fragment, {
        children: [
            /*#__PURE__*/ _jsxDEV(DndEventListener, {
                onDragEnd: onDragEnd,
                setIsDragging: setIsDragging
            }, void 0, false, {
                fileName: "src/views/BrowseByFolder/index.tsx",
                lineNumber: 247,
                columnNumber: 7
            }, this),
            /*#__PURE__*/ _jsxDEV("div", {
                className: `${baseClass} ${baseClass}--folders`,
                children: [
                    BeforeFolderList,
                    /*#__PURE__*/ _jsxDEV(Gutter, {
                        className: `${baseClass}__wrap`,
                        children: [
                            /*#__PURE__*/ _jsxDEV(ListHeader, {
                                Actions: [
                                    !smallBreak && /*#__PURE__*/ _jsxDEV(ListSelection, {
                                        disableBulkDelete: disableBulkDelete,
                                        disableBulkEdit: disableBulkEdit,
                                        folderAssignedCollections: folderAssignedCollections
                                    }, "list-selection", false, {
                                        fileName: "src/views/BrowseByFolder/index.tsx",
                                        lineNumber: 254,
                                        columnNumber: 17
                                    }, void 0)
                                ].filter(Boolean),
                                AfterListHeaderContent: Description,
                                title: listHeaderTitle,
                                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 : [],
                                        onCreateSuccess: clearRouteCache,
                                        slugPrefix: "create-document--header-pill"
                                    }, "create-new-button", false, {
                                        fileName: "src/views/BrowseByFolder/index.tsx",
                                        lineNumber: 266,
                                        columnNumber: 17
                                    }, void 0)
                                ].filter(Boolean)
                            }, void 0, false, {
                                fileName: "src/views/BrowseByFolder/index.tsx",
                                lineNumber: 251,
                                columnNumber: 11
                            }, this),
                            /*#__PURE__*/ _jsxDEV(SearchBar, {
                                Actions: [
                                    /*#__PURE__*/ _jsxDEV(SortByPill, {}, "sort-by-pill", false, {
                                        fileName: "src/views/BrowseByFolder/index.tsx",
                                        lineNumber: 283,
                                        columnNumber: 15
                                    }, void 0),
                                    folderID && /*#__PURE__*/ _jsxDEV(FilterFolderTypePill, {}, "collection-type", false, {
                                        fileName: "src/views/BrowseByFolder/index.tsx",
                                        lineNumber: 284,
                                        columnNumber: 27
                                    }, void 0),
                                    /*#__PURE__*/ _jsxDEV(ToggleViewButtons, {
                                        activeView: activeView,
                                        setActiveView: handleSetViewType
                                    }, "toggle-view-buttons", false, {
                                        fileName: "src/views/BrowseByFolder/index.tsx",
                                        lineNumber: 285,
                                        columnNumber: 15
                                    }, void 0),
                                    /*#__PURE__*/ _jsxDEV(CurrentFolderActions, {}, "current-folder-actions", false, {
                                        fileName: "src/views/BrowseByFolder/index.tsx",
                                        lineNumber: 290,
                                        columnNumber: 15
                                    }, void 0)
                                ].filter(Boolean),
                                label: searchPlaceholder,
                                onSearchChange: (search)=>refineFolderData({
                                        query: {
                                            search
                                        },
                                        updateURL: true
                                    }),
                                searchQueryParam: search
                            }, void 0, false, {
                                fileName: "src/views/BrowseByFolder/index.tsx",
                                lineNumber: 281,
                                columnNumber: 11
                            }, this),
                            BeforeFolderListTable,
                            totalDocsAndSubfolders > 0 && /*#__PURE__*/ _jsxDEV(_Fragment, {
                                children: activeView === 'grid' ? /*#__PURE__*/ _jsxDEV("div", {
                                    children: [
                                        subfolders.length ? /*#__PURE__*/ _jsxDEV(_Fragment, {
                                            children: /*#__PURE__*/ _jsxDEV(ItemCardGrid, {
                                                items: subfolders,
                                                title: 'Folders',
                                                type: "folder"
                                            }, void 0, false, {
                                                fileName: "src/views/BrowseByFolder/index.tsx",
                                                lineNumber: 303,
                                                columnNumber: 23
                                            }, this)
                                        }, void 0, false) : null,
                                        documents.length ? /*#__PURE__*/ _jsxDEV(_Fragment, {
                                            children: /*#__PURE__*/ _jsxDEV(ItemCardGrid, {
                                                items: documents,
                                                subfolderCount: subfolders.length,
                                                title: 'Documents',
                                                type: "file"
                                            }, void 0, false, {
                                                fileName: "src/views/BrowseByFolder/index.tsx",
                                                lineNumber: 309,
                                                columnNumber: 23
                                            }, this)
                                        }, void 0, false) : null
                                    ]
                                }, void 0, true, {
                                    fileName: "src/views/BrowseByFolder/index.tsx",
                                    lineNumber: 300,
                                    columnNumber: 17
                                }, this) : /*#__PURE__*/ _jsxDEV(FolderFileTable, {}, void 0, false, {
                                    fileName: "src/views/BrowseByFolder/index.tsx",
                                    lineNumber: 319,
                                    columnNumber: 17
                                }, this)
                            }, void 0, false),
                            totalDocsAndSubfolders === 0 && /*#__PURE__*/ _jsxDEV(NoListResults, {
                                Actions: [
                                    allowCreateCollectionSlugs.includes(folderCollectionConfig.slug) && /*#__PURE__*/ _jsxDEV(ListCreateNewDocInFolderButton, {
                                        buttonLabel: `${t('general:create')} ${getTranslation(folderCollectionConfig.labels?.singular, i18n).toLowerCase()}`,
                                        collectionSlugs: [
                                            folderCollectionConfig.slug
                                        ],
                                        folderAssignedCollections: Array.isArray(folderType) ? folderType : [],
                                        onCreateSuccess: clearRouteCache,
                                        slugPrefix: "create-folder--no-results"
                                    }, "create-folder", false, {
                                        fileName: "src/views/BrowseByFolder/index.tsx",
                                        lineNumber: 327,
                                        columnNumber: 19
                                    }, void 0),
                                    folderID && nonFolderCollectionSlugs.length > 0 && /*#__PURE__*/ _jsxDEV(ListCreateNewDocInFolderButton, {
                                        buttonLabel: `${t('general:create')} ${t('general:document').toLowerCase()}`,
                                        collectionSlugs: nonFolderCollectionSlugs,
                                        folderAssignedCollections: Array.isArray(folderType) ? folderType : [],
                                        onCreateSuccess: clearRouteCache,
                                        slugPrefix: "create-document--no-results"
                                    }, "create-document", false, {
                                        fileName: "src/views/BrowseByFolder/index.tsx",
                                        lineNumber: 337,
                                        columnNumber: 19
                                    }, void 0)
                                ].filter(Boolean),
                                Message: /*#__PURE__*/ _jsxDEV("p", {
                                    children: i18n.t('general:noResults', {
                                        label: noResultsLabel
                                    })
                                }, void 0, false, {
                                    fileName: "src/views/BrowseByFolder/index.tsx",
                                    lineNumber: 348,
                                    columnNumber: 17
                                }, void 0)
                            }, void 0, false, {
                                fileName: "src/views/BrowseByFolder/index.tsx",
                                lineNumber: 324,
                                columnNumber: 13
                            }, this),
                            AfterFolderListTable
                        ]
                    }, void 0, true, {
                        fileName: "src/views/BrowseByFolder/index.tsx",
                        lineNumber: 250,
                        columnNumber: 9
                    }, this),
                    AfterFolderList
                ]
            }, void 0, true, {
                fileName: "src/views/BrowseByFolder/index.tsx",
                lineNumber: 248,
                columnNumber: 7
            }, this),
            selectedItemKeys.size > 0 && dragOverlayItem && /*#__PURE__*/ _jsxDEV(DragOverlaySelection, {
                item: dragOverlayItem,
                selectedCount: selectedItemKeys.size
            }, void 0, false, {
                fileName: "src/views/BrowseByFolder/index.tsx",
                lineNumber: 361,
                columnNumber: 9
            }, this)
        ]
    }, void 0, true, {
        fileName: "src/views/BrowseByFolder/index.tsx",
        lineNumber: 246,
        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