import { jsxDEV as _jsxDEV, Fragment as _Fragment } from "react/jsx-dev-runtime";
import { useModal } from '@faceless-ui/modal';
import { getTranslation } from '@payloadcms/translations';
import { useRouter } from 'next/navigation.js';
import { formatAdminURL } from 'payload/shared';
import React from 'react';
import { toast } from 'sonner';
import { Dots } from '../../../icons/Dots/index.js';
import { useConfig } from '../../../providers/Config/index.js';
import { useFolder } from '../../../providers/Folders/index.js';
import { useRouteCache } from '../../../providers/RouteCache/index.js';
import { useRouteTransition } from '../../../providers/RouteTransition/index.js';
import { useTranslation } from '../../../providers/Translation/index.js';
import { ConfirmationModal } from '../../ConfirmationModal/index.js';
import { useDocumentDrawer } from '../../DocumentDrawer/index.js';
import { Popup, PopupList } from '../../Popup/index.js';
import { Translation } from '../../Translation/index.js';
import { MoveItemsToFolderDrawer } from '../Drawers/MoveToFolder/index.js';
const moveToFolderDrawerSlug = 'move-to-folder--current-folder';
const confirmDeleteDrawerSlug = 'confirm-many-delete';
const baseClass = 'current-folder-actions';
export function CurrentFolderActions({ className }) {
    const router = useRouter();
    const { startRouteTransition } = useRouteTransition();
    const { breadcrumbs, currentFolder, folderCollectionConfig, folderCollectionSlug, folderFieldName, folderID, getFolderRoute, moveToFolder } = useFolder();
    const [FolderDocumentDrawer, , { closeDrawer: closeFolderDrawer, openDrawer: openFolderDrawer }] = useDocumentDrawer({
        id: folderID,
        collectionSlug: folderCollectionSlug
    });
    const { clearRouteCache } = useRouteCache();
    const { config } = useConfig();
    const { routes } = config;
    const { closeModal, openModal } = useModal();
    const { i18n, t } = useTranslation();
    const deleteCurrentFolder = React.useCallback(async ()=>{
        await fetch(formatAdminURL({
            apiRoute: routes.api,
            path: `/${folderCollectionSlug}/${folderID}?depth=0`
        }), {
            credentials: 'include',
            method: 'DELETE'
        });
        startRouteTransition(()=>{
            router.push(getFolderRoute(breadcrumbs[breadcrumbs.length - 2]?.id || null));
        });
    }, [
        breadcrumbs,
        folderCollectionSlug,
        folderID,
        getFolderRoute,
        router,
        routes.api,
        startRouteTransition
    ]);
    if (!folderID) {
        return null;
    }
    return /*#__PURE__*/ _jsxDEV(_Fragment, {
        children: [
            /*#__PURE__*/ _jsxDEV(Popup, {
                button: /*#__PURE__*/ _jsxDEV(Dots, {}, void 0, false, {
                    fileName: "src/elements/FolderView/CurrentFolderActions/index.tsx",
                    lineNumber: 83,
                    columnNumber: 17
                }, void 0),
                className: [
                    baseClass,
                    className
                ].filter(Boolean).join(' '),
                render: ()=>/*#__PURE__*/ _jsxDEV(PopupList.ButtonGroup, {
                        children: [
                            /*#__PURE__*/ _jsxDEV(PopupList.Button, {
                                onClick: ()=>{
                                    openFolderDrawer();
                                },
                                children: t('general:editLabel', {
                                    label: getTranslation(folderCollectionConfig.labels.singular, i18n)
                                })
                            }, void 0, false, {
                                fileName: "src/elements/FolderView/CurrentFolderActions/index.tsx",
                                lineNumber: 87,
                                columnNumber: 13
                            }, void 0),
                            /*#__PURE__*/ _jsxDEV(PopupList.Button, {
                                onClick: ()=>{
                                    openModal(moveToFolderDrawerSlug);
                                },
                                children: t('folder:moveFolder')
                            }, void 0, false, {
                                fileName: "src/elements/FolderView/CurrentFolderActions/index.tsx",
                                lineNumber: 96,
                                columnNumber: 13
                            }, void 0),
                            /*#__PURE__*/ _jsxDEV(PopupList.Button, {
                                onClick: ()=>{
                                    openModal(confirmDeleteDrawerSlug);
                                },
                                children: t('folder:deleteFolder')
                            }, void 0, false, {
                                fileName: "src/elements/FolderView/CurrentFolderActions/index.tsx",
                                lineNumber: 103,
                                columnNumber: 13
                            }, void 0)
                        ]
                    }, void 0, true, {
                        fileName: "src/elements/FolderView/CurrentFolderActions/index.tsx",
                        lineNumber: 86,
                        columnNumber: 11
                    }, void 0)
            }, void 0, false, {
                fileName: "src/elements/FolderView/CurrentFolderActions/index.tsx",
                lineNumber: 82,
                columnNumber: 7
            }, this),
            /*#__PURE__*/ _jsxDEV(MoveItemsToFolderDrawer, {
                action: "moveItemToFolder",
                drawerSlug: moveToFolderDrawerSlug,
                folderAssignedCollections: currentFolder?.value.folderType,
                folderCollectionSlug: folderCollectionSlug,
                folderFieldName: folderFieldName,
                fromFolderID: currentFolder?.value.id,
                fromFolderName: currentFolder?.value._folderOrDocumentTitle,
                itemsToMove: [
                    currentFolder
                ],
                onConfirm: async ({ id, name })=>{
                    await moveToFolder({
                        itemsToMove: [
                            currentFolder
                        ],
                        toFolderID: id
                    });
                    if (id) {
                        // moved to folder
                        toast.success(t('folder:itemHasBeenMoved', {
                            folderName: `"${name}"`,
                            title: currentFolder.value._folderOrDocumentTitle
                        }));
                    } else {
                        // moved to root
                        toast.success(t('folder:itemHasBeenMovedToRoot', {
                            title: currentFolder.value._folderOrDocumentTitle
                        }));
                    }
                    closeModal(moveToFolderDrawerSlug);
                },
                title: currentFolder.value._folderOrDocumentTitle
            }, void 0, false, {
                fileName: "src/elements/FolderView/CurrentFolderActions/index.tsx",
                lineNumber: 113,
                columnNumber: 7
            }, this),
            /*#__PURE__*/ _jsxDEV(ConfirmationModal, {
                body: /*#__PURE__*/ _jsxDEV(Translation, {
                    elements: {
                        '1': ({ children })=>/*#__PURE__*/ _jsxDEV("strong", {
                                children: children
                            }, void 0, false, {
                                fileName: "src/elements/FolderView/CurrentFolderActions/index.tsx",
                                lineNumber: 152,
                                columnNumber: 38
                            }, void 0)
                    },
                    i18nKey: "general:aboutToDelete",
                    t: t,
                    variables: {
                        label: getTranslation(folderCollectionConfig.labels.singular, i18n),
                        title: currentFolder.value._folderOrDocumentTitle
                    }
                }, void 0, false, {
                    fileName: "src/elements/FolderView/CurrentFolderActions/index.tsx",
                    lineNumber: 150,
                    columnNumber: 11
                }, void 0),
                confirmingLabel: t('general:deleting'),
                heading: t('general:confirmDeletion'),
                modalSlug: confirmDeleteDrawerSlug,
                onConfirm: deleteCurrentFolder
            }, void 0, false, {
                fileName: "src/elements/FolderView/CurrentFolderActions/index.tsx",
                lineNumber: 148,
                columnNumber: 7
            }, this),
            /*#__PURE__*/ _jsxDEV(FolderDocumentDrawer, {
                onSave: ()=>{
                    closeFolderDrawer();
                    clearRouteCache();
                }
            }, void 0, false, {
                fileName: "src/elements/FolderView/CurrentFolderActions/index.tsx",
                lineNumber: 168,
                columnNumber: 7
            }, this)
        ]
    }, void 0, true);
}

//# sourceMappingURL=index.js.map