import { jsx as _jsx } from "react/jsx-runtime";
import { useCallback } from 'react';
import { Popup, useToaster } from '@gravity-ui/uikit';
import { FileForm } from "../../../forms/FileForm.js";
import { i18n } from "../../../i18n/forms/index.js";
import { useBooleanState } from "../../../react-utils/hooks.js";
import { batchUploadFiles, } from "../../../utils/upload.js";
const placement = ['bottom-start', 'top-start', 'bottom-end', 'top-end'];
export const ToolbarFilePopup = ({ className, hide, anchorElement, onSubmit, focus, onClick, uploadHandler, onSuccessUpload, }) => {
    const toaster = useToaster();
    const [loading, showLoading, hideLoading] = useBooleanState(false);
    const handleCancel = useCallback(() => {
        hide();
        focus();
    }, [focus, hide]);
    return (_jsx(Popup, { open: true, modal: true, onOpenChange: handleCancel, anchorElement: anchorElement, placement: placement, className: className, children: _jsx(FileForm, { autoFocus: true, onAttach: uploadHandler &&
                ((files) => {
                    showLoading();
                    batchUploadFiles(files, uploadHandler).then((res) => {
                        hideLoading();
                        hide();
                        onSuccessUpload?.(res);
                    }, (error) => {
                        hideLoading();
                        toaster.add({
                            theme: 'danger',
                            name: 'toolbar_file_upload',
                            title: i18n('file_upload_failed'),
                            content: String(error),
                        });
                    });
                }), onCancel: handleCancel, onSubmit: (data) => {
                hide();
                focus();
                onSubmit(data);
                onClick?.('addFile');
            }, loading: loading }) }));
};
//# sourceMappingURL=ToolbarFilePopup.js.map
