'use client';
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import { useModal } from '@faceless-ui/modal';
import { validateMimeType } from 'payload/shared';
import React, { useEffect } from 'react';
import { toast } from 'sonner';
import { useEffectEvent } from '../../hooks/useEffectEvent.js';
import { useConfig } from '../../providers/Config/index.js';
import { EditDepthProvider } from '../../providers/EditDepth/index.js';
import { useTranslation } from '../../providers/Translation/index.js';
import { UploadControlsProvider } from '../../providers/UploadControls/index.js';
import { Drawer, useDrawerDepth } from '../Drawer/index.js';
import { AddFilesView } from './AddFilesView/index.js';
import { AddingFilesView } from './AddingFilesView/index.js';
import { FormsManagerProvider, useFormsManager } from './FormsManager/index.js';
const drawerSlug = 'bulk-upload-drawer-slug';
function DrawerContent() {
    const { addFiles, forms, isInitializing } = useFormsManager();
    const { closeModal } = useModal();
    const { collectionSlug, drawerSlug } = useBulkUpload();
    const { getEntityConfig } = useConfig();
    const { t } = useTranslation();
    const uploadCollection = getEntityConfig({
        collectionSlug
    });
    const uploadConfig = uploadCollection?.upload;
    const uploadMimeTypes = uploadConfig?.mimeTypes;
    const onDrop = React.useCallback((acceptedFiles)=>{
        const fileTransfer = new DataTransfer();
        for (const candidateFile of acceptedFiles){
            if (uploadMimeTypes === undefined || uploadMimeTypes.length === 0 || validateMimeType(candidateFile.type, uploadMimeTypes)) {
                fileTransfer.items.add(candidateFile);
            }
        }
        if (fileTransfer.files.length === 0) {
            toast.error(t('error:invalidFileType'));
        } else {
            void addFiles(fileTransfer.files);
        }
    }, [
        addFiles,
        t,
        uploadMimeTypes
    ]);
    if (!collectionSlug) {
        return null;
    }
    if (!forms.length && !isInitializing) {
        return /*#__PURE__*/ _jsxDEV(AddFilesView, {
            acceptMimeTypes: uploadMimeTypes?.join(', '),
            onCancel: ()=>closeModal(drawerSlug),
            onDrop: onDrop
        }, void 0, false, {
            fileName: "src/elements/BulkUpload/index.tsx",
            lineNumber: 60,
            columnNumber: 7
        }, this);
    } else {
        return /*#__PURE__*/ _jsxDEV(AddingFilesView, {}, void 0, false, {
            fileName: "src/elements/BulkUpload/index.tsx",
            lineNumber: 67,
            columnNumber: 12
        }, this);
    }
}
export function BulkUploadDrawer() {
    const { drawerSlug, onCancel, setInitialFiles, setInitialForms, setOnCancel, setOnSuccess, setSelectableCollections, setSuccessfullyUploaded, successfullyUploaded } = useBulkUpload();
    const { modalState } = useModal();
    const previousModalStateRef = React.useRef(modalState);
    /**
   * This is used to trigger onCancel when the drawer is closed (=> forms reset, as FormsManager is unmounted)
   */ const onModalStateChanged = useEffectEvent((modalState)=>{
        const previousModalState = previousModalStateRef.current[drawerSlug];
        const currentModalState = modalState[drawerSlug];
        if (typeof currentModalState === 'undefined' && typeof previousModalState === 'undefined') {
            return;
        }
        if (previousModalState?.isOpen !== currentModalState?.isOpen) {
            if (!currentModalState?.isOpen) {
                if (!successfullyUploaded) {
                    // It's only cancelled if successfullyUploaded is not set. Otherwise, this would simply be a modal close after success
                    // => do not call cancel, just reset everything
                    if (typeof onCancel === 'function') {
                        onCancel();
                    }
                }
                // Reset everything to defaults
                setInitialFiles(undefined);
                setInitialForms(undefined);
                setOnCancel(()=>()=>null);
                setOnSuccess(()=>()=>null);
                setSelectableCollections(null);
                setSuccessfullyUploaded(false);
            }
        }
        previousModalStateRef.current = modalState;
    });
    useEffect(()=>{
        onModalStateChanged(modalState);
    }, [
        modalState
    ]);
    return /*#__PURE__*/ _jsxDEV(Drawer, {
        gutter: false,
        Header: null,
        slug: drawerSlug,
        children: /*#__PURE__*/ _jsxDEV(FormsManagerProvider, {
            children: /*#__PURE__*/ _jsxDEV(UploadControlsProvider, {
                children: /*#__PURE__*/ _jsxDEV(EditDepthProvider, {
                    children: /*#__PURE__*/ _jsxDEV(DrawerContent, {}, void 0, false, {
                        fileName: "src/elements/BulkUpload/index.tsx",
                        lineNumber: 132,
                        columnNumber: 13
                    }, this)
                }, void 0, false, {
                    fileName: "src/elements/BulkUpload/index.tsx",
                    lineNumber: 131,
                    columnNumber: 11
                }, this)
            }, void 0, false, {
                fileName: "src/elements/BulkUpload/index.tsx",
                lineNumber: 130,
                columnNumber: 9
            }, this)
        }, void 0, false, {
            fileName: "src/elements/BulkUpload/index.tsx",
            lineNumber: 129,
            columnNumber: 7
        }, this)
    }, void 0, false, {
        fileName: "src/elements/BulkUpload/index.tsx",
        lineNumber: 128,
        columnNumber: 5
    }, this);
}
/**
   * Like initialFiles, but allows manually providing initial form state or the form ID for each file
   */ /**
       * ID of the form that created this document
       */ /**
   * An array of collection slugs that can be selected in the collection dropdown (if applicable)
   * @default null - collection cannot be selected
   */ /**
   * Set the collections that can be selected in the collection dropdown (if applicable)
   *
   * @default null - collection cannot be selected
   */ const Context = /*#__PURE__*/ React.createContext({
    collectionSlug: '',
    drawerSlug: '',
    initialFiles: undefined,
    initialForms: [],
    maxFiles: undefined,
    onCancel: ()=>null,
    onSuccess: ()=>null,
    selectableCollections: null,
    setCollectionSlug: ()=>null,
    setInitialFiles: ()=>null,
    setInitialForms: ()=>null,
    setMaxFiles: ()=>null,
    setOnCancel: ()=>null,
    setOnSuccess: ()=>null,
    setSelectableCollections: ()=>null,
    setSuccessfullyUploaded: ()=>false,
    successfullyUploaded: false
});
export function BulkUploadProvider({ children, drawerSlugPrefix }) {
    const [selectableCollections, setSelectableCollections] = React.useState(null);
    const [collection, setCollection] = React.useState();
    const [onSuccessFunction, setOnSuccessFunction] = React.useState();
    const [onCancelFunction, setOnCancelFunction] = React.useState();
    const [initialFiles, setInitialFiles] = React.useState(undefined);
    const [initialForms, setInitialForms] = React.useState(undefined);
    const [maxFiles, setMaxFiles] = React.useState(undefined);
    const [successfullyUploaded, setSuccessfullyUploaded] = React.useState(false);
    const drawerSlug = `${drawerSlugPrefix ? `${drawerSlugPrefix}-` : ''}${useBulkUploadDrawerSlug()}`;
    const setOnSuccess = (onSuccess)=>{
        setOnSuccessFunction(()=>onSuccess);
    };
    const setOnCancel = (onCancel)=>{
        setOnCancelFunction(()=>onCancel);
    };
    return /*#__PURE__*/ _jsxDEV(Context, {
        value: {
            collectionSlug: collection,
            drawerSlug,
            initialFiles,
            initialForms,
            maxFiles,
            onCancel: ()=>{
                if (typeof onCancelFunction === 'function') {
                    onCancelFunction();
                }
            },
            onSuccess: (newDocs, errorCount)=>{
                if (typeof onSuccessFunction === 'function') {
                    onSuccessFunction(newDocs, errorCount);
                }
            },
            selectableCollections,
            setCollectionSlug: setCollection,
            setInitialFiles,
            setInitialForms,
            setMaxFiles,
            setOnCancel,
            setOnSuccess,
            setSelectableCollections,
            setSuccessfullyUploaded,
            successfullyUploaded
        },
        children: /*#__PURE__*/ _jsxDEV(React.Fragment, {
            children: [
                children,
                /*#__PURE__*/ _jsxDEV(BulkUploadDrawer, {}, void 0, false, {
                    fileName: "src/elements/BulkUpload/index.tsx",
                    lineNumber: 260,
                    columnNumber: 9
                }, this)
            ]
        }, void 0, true, {
            fileName: "src/elements/BulkUpload/index.tsx",
            lineNumber: 258,
            columnNumber: 7
        }, this)
    }, void 0, false, {
        fileName: "src/elements/BulkUpload/index.tsx",
        lineNumber: 229,
        columnNumber: 5
    }, this);
}
export const useBulkUpload = ()=>React.use(Context);
export function useBulkUploadDrawerSlug() {
    const depth = useDrawerDepth();
    return `${drawerSlug}-${depth || 1}`;
}

//# sourceMappingURL=index.js.map