'use client';
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import { useModal } from '@faceless-ui/modal';
import { extractID } from 'payload/shared';
import React, { Fragment } from 'react';
import { toast } from 'sonner';
import { DeleteMany_v4 } from '../../../elements/DeleteMany/index.js';
import { EditMany_v4 } from '../../../elements/EditMany/index.js';
import { EditFolderAction } from '../../../elements/FolderView/Drawers/EditFolderAction/index.js';
import { MoveItemsToFolderDrawer } from '../../../elements/FolderView/Drawers/MoveToFolder/index.js';
import { ListSelection_v4, ListSelectionButton } from '../../../elements/ListSelection/index.js';
import { PublishMany_v4 } from '../../../elements/PublishMany/index.js';
import { UnpublishMany_v4 } from '../../../elements/UnpublishMany/index.js';
import { useConfig } from '../../../providers/Config/index.js';
import { useFolder } from '../../../providers/Folders/index.js';
import { useRouteCache } from '../../../providers/RouteCache/index.js';
import { useTranslation } from '../../../providers/Translation/index.js';
const moveToFolderDrawerSlug = 'move-to-folder--list';
export const ListSelection = ({ disableBulkDelete, disableBulkEdit, folderAssignedCollections })=>{
    const { clearSelections, currentFolder, folderCollectionSlug, folderFieldName, folderID, getSelectedItems, moveToFolder } = useFolder();
    const { clearRouteCache } = useRouteCache();
    const { config } = useConfig();
    const { t } = useTranslation();
    const { closeModal, openModal } = useModal();
    const items = getSelectedItems();
    const groupedSelections = items.reduce((acc, item)=>{
        if (item) {
            if (acc[item.relationTo]) {
                acc[item.relationTo].ids.push(extractID(item.value));
                acc[item.relationTo].totalCount += 1;
            } else {
                acc[item.relationTo] = {
                    ids: [
                        extractID(item.value)
                    ],
                    totalCount: 1
                };
            }
        }
        return acc;
    }, {});
    const count = items.length;
    const singleNonFolderCollectionSelected = Object.keys(groupedSelections).length === 1 && Object.keys(groupedSelections)[0] !== folderCollectionSlug;
    const collectionConfig = singleNonFolderCollectionSelected ? config.collections.find((collection)=>{
        return collection.slug === Object.keys(groupedSelections)[0];
    }) : null;
    if (count === 0) {
        return null;
    }
    const ids = singleNonFolderCollectionSelected ? groupedSelections[Object.keys(groupedSelections)[0]]?.ids || [] : [];
    return /*#__PURE__*/ _jsxDEV(ListSelection_v4, {
        count: count,
        ListActions: [
            count > 0 && /*#__PURE__*/ _jsxDEV(ListSelectionButton, {
                onClick: ()=>clearSelections(),
                children: t('general:clearAll')
            }, "clear-all", false, {
                fileName: "src/views/CollectionFolder/ListSelection/index.tsx",
                lineNumber: 98,
                columnNumber: 11
            }, void 0)
        ].filter(Boolean),
        SelectionActions: [
            !disableBulkEdit && ids.length && /*#__PURE__*/ _jsxDEV(Fragment, {
                children: [
                    /*#__PURE__*/ _jsxDEV(EditMany_v4, {
                        collection: collectionConfig,
                        count: count,
                        ids: ids,
                        selectAll: false
                    }, void 0, false, {
                        fileName: "src/views/CollectionFolder/ListSelection/index.tsx",
                        lineNumber: 106,
                        columnNumber: 13
                    }, void 0),
                    /*#__PURE__*/ _jsxDEV(PublishMany_v4, {
                        collection: collectionConfig,
                        count: count,
                        ids: ids,
                        selectAll: false
                    }, void 0, false, {
                        fileName: "src/views/CollectionFolder/ListSelection/index.tsx",
                        lineNumber: 107,
                        columnNumber: 13
                    }, void 0),
                    /*#__PURE__*/ _jsxDEV(UnpublishMany_v4, {
                        collection: collectionConfig,
                        count: count,
                        ids: ids,
                        selectAll: false
                    }, void 0, false, {
                        fileName: "src/views/CollectionFolder/ListSelection/index.tsx",
                        lineNumber: 113,
                        columnNumber: 13
                    }, void 0)
                ]
            }, "bulk-actions", true, {
                fileName: "src/views/CollectionFolder/ListSelection/index.tsx",
                lineNumber: 105,
                columnNumber: 11
            }, void 0),
            count === 1 && !singleNonFolderCollectionSelected && /*#__PURE__*/ _jsxDEV(EditFolderAction, {
                folderCollectionSlug: folderCollectionSlug,
                id: groupedSelections[folderCollectionSlug].ids[0]
            }, "edit-folder-action", false, {
                fileName: "src/views/CollectionFolder/ListSelection/index.tsx",
                lineNumber: 122,
                columnNumber: 11
            }, void 0),
            count > 0 ? /*#__PURE__*/ _jsxDEV(React.Fragment, {
                children: [
                    /*#__PURE__*/ _jsxDEV(ListSelectionButton, {
                        onClick: ()=>{
                            openModal(moveToFolderDrawerSlug);
                        },
                        type: "button",
                        children: t('general:move')
                    }, void 0, false, {
                        fileName: "src/views/CollectionFolder/ListSelection/index.tsx",
                        lineNumber: 130,
                        columnNumber: 13
                    }, void 0),
                    /*#__PURE__*/ _jsxDEV(MoveItemsToFolderDrawer, {
                        action: "moveItemsToFolder",
                        drawerSlug: moveToFolderDrawerSlug,
                        folderAssignedCollections: folderAssignedCollections,
                        folderCollectionSlug: folderCollectionSlug,
                        folderFieldName: folderFieldName,
                        fromFolderID: folderID,
                        fromFolderName: currentFolder?.value?._folderOrDocumentTitle,
                        itemsToMove: getSelectedItems(),
                        onConfirm: async ({ id, name })=>{
                            await moveToFolder({
                                itemsToMove: getSelectedItems(),
                                toFolderID: id
                            });
                            if (id) {
                                // moved to folder
                                toast.success(t('folder:itemsMovedToFolder', {
                                    folderName: `"${name}"`,
                                    title: `${count} ${count > 1 ? t('general:items') : t('general:item')}`
                                }));
                            } else {
                                // moved to root
                                toast.success(t('folder:itemsMovedToRoot', {
                                    title: `${count} ${count > 1 ? t('general:items') : t('general:item')}`
                                }));
                            }
                            clearRouteCache();
                            closeModal(moveToFolderDrawerSlug);
                        }
                    }, void 0, false, {
                        fileName: "src/views/CollectionFolder/ListSelection/index.tsx",
                        lineNumber: 139,
                        columnNumber: 13
                    }, void 0)
                ]
            }, moveToFolderDrawerSlug, true, {
                fileName: "src/views/CollectionFolder/ListSelection/index.tsx",
                lineNumber: 129,
                columnNumber: 11
            }, void 0) : null,
            !disableBulkDelete && /*#__PURE__*/ _jsxDEV(DeleteMany_v4, {
                afterDelete: ()=>{
                    clearRouteCache();
                    clearSelections();
                },
                selections: groupedSelections
            }, "bulk-delete", false, {
                fileName: "src/views/CollectionFolder/ListSelection/index.tsx",
                lineNumber: 178,
                columnNumber: 11
            }, void 0)
        ].filter(Boolean)
    }, void 0, false, {
        fileName: "src/views/CollectionFolder/ListSelection/index.tsx",
        lineNumber: 94,
        columnNumber: 5
    }, this);
};

//# sourceMappingURL=index.js.map