'use client';
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import { useModal } from '@faceless-ui/modal';
import { getTranslation } from '@payloadcms/translations';
import React from 'react';
import { useConfig } from '../../../providers/Config/index.js';
import { useFolder } from '../../../providers/Folders/index.js';
import { useTranslation } from '../../../providers/Translation/index.js';
import { Button } from '../../Button/index.js';
import { DocumentDrawer, useDocumentDrawer } from '../../DocumentDrawer/index.js';
import { Popup, PopupList } from '../../Popup/index.js';
const baseClass = 'create-new-doc-in-folder';
export function ListCreateNewDocInFolderButton({ buttonLabel, buttonSize = 'small', buttonStyle = 'pill', collectionSlugs, folderAssignedCollections, onCreateSuccess, slugPrefix }) {
    const newDocInFolderDrawerSlug = `${slugPrefix}-new-doc-in-folder-drawer`;
    const { i18n } = useTranslation();
    const { closeModal, openModal } = useModal();
    const { config } = useConfig();
    const { folderCollectionConfig, folderCollectionSlug, folderFieldName, folderID } = useFolder();
    const [FolderDocumentDrawer, , { closeDrawer: closeFolderDrawer, openDrawer: openFolderDrawer }] = useDocumentDrawer({
        collectionSlug: folderCollectionSlug
    });
    const [createCollectionSlug, setCreateCollectionSlug] = React.useState();
    const [enabledCollections] = React.useState(()=>collectionSlugs.reduce((acc, collectionSlug)=>{
            const collectionConfig = config.collections.find(({ slug })=>slug === collectionSlug);
            if (collectionConfig) {
                acc.push(collectionConfig);
            }
            return acc;
        }, []));
    if (enabledCollections.length === 0) {
        return null;
    }
    return /*#__PURE__*/ _jsxDEV(React.Fragment, {
        children: [
            enabledCollections.length === 1 ? // If there is only 1 option, do not render a popup
            /*#__PURE__*/ _jsxDEV(Button, {
                buttonStyle: buttonStyle,
                className: `${baseClass}__button`,
                el: "div",
                onClick: ()=>{
                    if (enabledCollections[0].slug === folderCollectionConfig.slug) {
                        openFolderDrawer();
                    } else {
                        setCreateCollectionSlug(enabledCollections[0].slug);
                        openModal(newDocInFolderDrawerSlug);
                    }
                },
                size: buttonSize,
                children: buttonLabel
            }, void 0, false, {
                fileName: "src/elements/ListHeader/TitleActions/ListCreateNewDocInFolderButton.tsx",
                lineNumber: 75,
                columnNumber: 9
            }, this) : /*#__PURE__*/ _jsxDEV(Popup, {
                button: /*#__PURE__*/ _jsxDEV(Button, {
                    buttonStyle: buttonStyle,
                    className: `${baseClass}__popup-button`,
                    el: "div",
                    icon: "chevron",
                    size: buttonSize,
                    children: buttonLabel
                }, void 0, false, {
                    fileName: "src/elements/ListHeader/TitleActions/ListCreateNewDocInFolderButton.tsx",
                    lineNumber: 94,
                    columnNumber: 13
                }, void 0),
                buttonType: "default",
                className: `${baseClass}__action-popup`,
                children: /*#__PURE__*/ _jsxDEV(PopupList.ButtonGroup, {
                    children: enabledCollections.map((collection, index)=>{
                        return /*#__PURE__*/ _jsxDEV(PopupList.Button, {
                            onClick: ()=>{
                                if (collection.slug === folderCollectionConfig.slug) {
                                    openFolderDrawer();
                                } else {
                                    setCreateCollectionSlug(collection.slug);
                                    openModal(newDocInFolderDrawerSlug);
                                }
                            },
                            children: getTranslation(collection.labels.singular, i18n)
                        }, index, false, {
                            fileName: "src/elements/ListHeader/TitleActions/ListCreateNewDocInFolderButton.tsx",
                            lineNumber: 110,
                            columnNumber: 17
                        }, this);
                    })
                }, void 0, false, {
                    fileName: "src/elements/ListHeader/TitleActions/ListCreateNewDocInFolderButton.tsx",
                    lineNumber: 107,
                    columnNumber: 11
                }, this)
            }, void 0, false, {
                fileName: "src/elements/ListHeader/TitleActions/ListCreateNewDocInFolderButton.tsx",
                lineNumber: 92,
                columnNumber: 9
            }, this),
            createCollectionSlug && /*#__PURE__*/ _jsxDEV(DocumentDrawer, {
                collectionSlug: createCollectionSlug,
                drawerSlug: newDocInFolderDrawerSlug,
                initialData: {
                    [folderFieldName]: folderID
                },
                onSave: async ({ doc })=>{
                    await onCreateSuccess({
                        collectionSlug: createCollectionSlug,
                        doc
                    });
                    closeModal(newDocInFolderDrawerSlug);
                },
                redirectAfterCreate: false
            }, void 0, false, {
                fileName: "src/elements/ListHeader/TitleActions/ListCreateNewDocInFolderButton.tsx",
                lineNumber: 130,
                columnNumber: 9
            }, this),
            collectionSlugs.includes(folderCollectionConfig.slug) && /*#__PURE__*/ _jsxDEV(FolderDocumentDrawer, {
                initialData: {
                    [folderFieldName]: folderID,
                    folderType: createCollectionSlug ? folderAssignedCollections || [
                        createCollectionSlug
                    ] : folderAssignedCollections
                },
                onSave: async (result)=>{
                    await onCreateSuccess({
                        collectionSlug: folderCollectionConfig.slug,
                        doc: result.doc
                    });
                    closeFolderDrawer();
                },
                redirectAfterCreate: false
            }, void 0, false, {
                fileName: "src/elements/ListHeader/TitleActions/ListCreateNewDocInFolderButton.tsx",
                lineNumber: 148,
                columnNumber: 9
            }, this)
        ]
    }, void 0, true, {
        fileName: "src/elements/ListHeader/TitleActions/ListCreateNewDocInFolderButton.tsx",
        lineNumber: 72,
        columnNumber: 5
    }, this);
}

//# sourceMappingURL=ListCreateNewDocInFolderButton.js.map