import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2026 GoodData Corporation
import { useCallback, useId, useRef, useState } from "react";
import cx from "classnames";
import { Dialog } from "../../Dialog/Dialog.js";
import { Spinner } from "../../Spinner/Spinner.js";
import { bem } from "../@utils/bem.js";
import { UiButton } from "../UiButton/UiButton.js";
import { UiIconButton } from "../UiIconButton/UiIconButton.js";
const { b, e } = bem("gd-ui-kit-generate-text-dialog");
/**
 * Generic AI-generated text action with icon button + editable dialog.
 *
 * @internal
 */
export function UiGenerateTextDialog({ buttonLabel, buttonDataTestId, buttonIcon = "ai", buttonSize = "small", buttonVariant = "tertiary", dialogTitle, dialogDisclaimer, acceptLabel, declineLabel, noteLabel, textAreaAriaLabel, initialText, textAreaRows = 7, onGenerate, onAccept, }) {
    const dialogTitleId = useId();
    const dialogDescriptionId = useId();
    const [isDialogOpen, setIsDialogOpen] = useState(false);
    const [generatedText, setGeneratedText] = useState("");
    const [generatedNote, setGeneratedNote] = useState("");
    const [isGenerating, setIsGenerating] = useState(false);
    const latestRequestIdRef = useRef(0);
    const closeDialog = useCallback(() => {
        latestRequestIdRef.current += 1;
        setIsDialogOpen(false);
        setIsGenerating(false);
    }, []);
    const openDialog = useCallback(() => {
        const requestId = latestRequestIdRef.current + 1;
        latestRequestIdRef.current = requestId;
        setIsDialogOpen(true);
        setIsGenerating(true);
        setGeneratedText(initialText ?? "");
        setGeneratedNote("");
        void Promise.resolve()
            .then(onGenerate)
            .then((result) => {
            if (latestRequestIdRef.current !== requestId) {
                return;
            }
            setGeneratedText(result.text ?? "");
            setGeneratedNote(result.note ?? "");
        })
            .catch(() => {
            // Error handling is delegated to caller.
        })
            .finally(() => {
            if (latestRequestIdRef.current !== requestId) {
                return;
            }
            setIsGenerating(false);
        });
    }, [initialText, onGenerate]);
    const handleTextChange = useCallback((event) => {
        setGeneratedText(event.target.value);
    }, []);
    const accept = useCallback(() => {
        onAccept(generatedText);
        closeDialog();
    }, [closeDialog, generatedText, onAccept]);
    return (_jsxs(_Fragment, { children: [
            _jsx(UiIconButton, { icon: buttonIcon, size: buttonSize, variant: buttonVariant, dataTestId: buttonDataTestId, label: buttonLabel, onClick: openDialog }), isDialogOpen ? (_jsx(Dialog, { displayCloseButton: true, closeOnEscape: true, onClose: closeDialog, onCancel: closeDialog, accessibilityConfig: {
                    titleElementId: dialogTitleId,
                    descriptionElementId: dialogDescriptionId,
                    isModal: true,
                }, className: b(), children: _jsxs("div", { className: e("content"), children: [
                        _jsx("h2", { id: dialogTitleId, className: e("title"), children: dialogTitle }), _jsx("p", { id: dialogDescriptionId, className: e("disclaimer"), children: dialogDisclaimer }), _jsxs("div", { className: e("textarea-wrapper", { isLoading: isGenerating }), children: [
                                _jsx("textarea", { className: cx("gd-input-field", "s-textarea-field", e("textarea", { isLoading: isGenerating })), value: generatedText, onChange: handleTextChange, disabled: isGenerating, rows: textAreaRows, "aria-label": textAreaAriaLabel }), isGenerating ? (_jsx("div", { className: e("textarea-loader"), "aria-hidden": true, children: _jsx(Spinner, {}) })) : null] }), generatedNote ? (_jsxs("p", { className: e("note"), children: [
                                _jsxs("span", { className: e("note-label"), children: [noteLabel, ": "] }), generatedNote] })) : null, _jsxs("div", { className: e("actions"), children: [
                                _jsx(UiButton, { variant: "secondary", label: declineLabel, onClick: closeDialog }), _jsx(UiButton, { variant: "primary", label: acceptLabel, onClick: accept, isDisabled: isGenerating })
                            ] })
                    ] }) })) : null] }));
}
//# sourceMappingURL=UiGenerateTextDialog.js.map