"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToolbarImagePopup = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const uikit_1 = require("@gravity-ui/uikit");
const ImageForm_1 = require("../../../forms/ImageForm.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 ToolbarImagePopup = ({ className, anchorElement, hide, onSubmit, focus, onClick, uploadImages, onSuccessUpload, imageTitle, 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, anchorElement: anchorElement, onOpenChange: handleCancel, placement: placement, className: className, children: (0, jsx_runtime_1.jsx)(ImageForm_1.ImageForm, { autoFocus: true, onAttach: uploadImages &&
                ((files) => {
                    showLoading();
                    (0, upload_1.batchUploadFiles)(files, uploadImages).then((res) => {
                        hideLoading();
                        hide();
                        onSuccessUpload?.(res);
                    }, (err) => {
                        hideLoading();
                        toaster.add({
                            theme: 'danger',
                            name: 'toolbar_image_upload',
                            title: (0, forms_1.i18n)('image_upload_failed'),
                            content: String(err),
                        });
                    });
                }), onCancel: handleCancel, onSubmit: (data) => {
                hide();
                focus();
                onSubmit(data);
                onClick?.('addImage');
            }, loading: loading, imageTitle: imageTitle, uploadHint: uploadHint }) }));
};
exports.ToolbarImagePopup = ToolbarImagePopup;
//# sourceMappingURL=ToolbarImagePopup.js.map
