"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToolbarFilePopup = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const uikit_1 = require("@gravity-ui/uikit");
const FileForm_1 = require("../../../forms/FileForm.js");
const forms_1 = require("../../../i18n/forms/index.js");
const hooks_1 = require("../../../react-utils/hooks.js");
const upload_1 = require("../../../utils/upload.js");
const placement = ['bottom-start', 'top-start', 'bottom-end', 'top-end'];
const ToolbarFilePopup = ({ className, hide, anchorElement, onSubmit, focus, onClick, uploadHandler, onSuccessUpload, uploadHint, }) => {
    const toaster = (0, uikit_1.useToaster)();
    const [loading, showLoading, hideLoading] = (0, hooks_1.useBooleanState)(false);
    const handleCancel = (0, react_1.useCallback)(() => {
        hide();
        focus();
    }, [focus, hide]);
    return ((0, jsx_runtime_1.jsx)(uikit_1.Popup, { open: true, modal: true, onOpenChange: handleCancel, anchorElement: anchorElement, placement: placement, className: className, children: (0, jsx_runtime_1.jsx)(FileForm_1.FileForm, { autoFocus: true, onAttach: uploadHandler &&
                ((files) => {
                    showLoading();
                    (0, upload_1.batchUploadFiles)(files, uploadHandler).then((res) => {
                        hideLoading();
                        hide();
                        onSuccessUpload?.(res);
                    }, (error) => {
                        hideLoading();
                        toaster.add({
                            theme: 'danger',
                            name: 'toolbar_file_upload',
                            title: (0, forms_1.i18n)('file_upload_failed'),
                            content: String(error),
                        });
                    });
                }), onCancel: handleCancel, onSubmit: (data) => {
                hide();
                focus();
                onSubmit(data);
                onClick?.('addFile');
            }, loading: loading, uploadHint: uploadHint }) }));
};
exports.ToolbarFilePopup = ToolbarFilePopup;
//# sourceMappingURL=ToolbarFilePopup.js.map
