"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GptDialog = exports.cnGptDialog = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const icons_1 = require("@gravity-ui/icons");
const uikit_1 = require("@gravity-ui/uikit");
const classname_1 = require("../../../../classname.js");
const dialog_1 = require("../../../../i18n/gpt/dialog/index.js");
const GPT_1 = tslib_1.__importDefault(require("../../../../icons/GPT.js"));
const useAutoFocus_1 = require("../../../../react-utils/useAutoFocus.js");
const ErrorScreen_1 = require("../ErrorScreen/ErrorScreen.js");
const IconRefuge_1 = require("../IconRefuge/IconRefuge.js");
const PresetList_1 = require("../PresetList/PresetList.js");
const constants_1 = require("../constants.js");
const useGpt_1 = require("../hooks/useGpt.js");
const useGptHotKeys_1 = require("../hooks/useGptHotKeys.js");
const utils_1 = require("../utils.js");
const LoadingScreen_1 = require("./LoadingScreen/LoadingScreen.js");
require("./GptDialog.css");
exports.cnGptDialog = (0, classname_1.cn)('gpt-dialog');
const GptDialog = ({ markup, answerRender, promptPresets, disablePromptPresets, customPromptPlaceholder, disabledPromptPlaceholder, onCustomPromptApply, onPromptPresetClick, onTryAgain, onApplyResult, onClose, onLike, onDislike, onUpdate, gptAlertProps, }) => {
    const { answer, customPrompt, loading, mode, feedbackType, feedbackTypeLoading, handleLike, handleDislike, handleCustomPromptUpdate, handleCustomPromptKeyPress, handleCustomPromptApply, handlePresetClick, handleTryAgain, handleFreshStart, handleApplyResult, showAnswer, showError, showAnswerActions, showTryAgainButton, } = (0, useGpt_1.useGpt)({
        markup,
        promptPresets,
        onLike,
        onDislike,
        onCustomPromptApply,
        onPromptPresetClick,
        onTryAgain,
        onApplyResult,
        onUpdate,
    });
    const gptAlert = gptAlertProps;
    const customPromptContainerRef = (0, react_1.useRef)(null);
    const [showedGptAlert, setShowedGptAlert] = (0, react_1.useState)(gptAlert?.showedGptAlert);
    const { alertMessage, alertTheme, alertClassName } = (0, utils_1.getAlertGptInfo)(gptAlert);
    const onCloseAlert = (0, react_1.useCallback)(() => {
        if (gptAlert) {
            gptAlert.showedGptAlert = false;
        }
        gptAlert?.onCloseGptAlert?.();
        setShowedGptAlert(false);
    }, [gptAlert]);
    (0, useAutoFocus_1.useAutoFocus)(customPromptContainerRef, [showAnswer]);
    (0, useGptHotKeys_1.useGptHotKeys)(constants_1.gptHotKeys.tryAgainGpt, handleTryAgain);
    (0, useGptHotKeys_1.useGptHotKeys)(constants_1.gptHotKeys.freshStartGpt, handleFreshStart);
    (0, useGptHotKeys_1.useGptHotKeys)(constants_1.gptHotKeys.applyResultGpt, handleApplyResult);
    const replaceButtonText = (0, utils_1.getDisableReplaceButtonText)(disablePromptPresets);
    const inputPlaceholderText = (0, utils_1.getInputPlaceHolder)(disablePromptPresets, disabledPromptPlaceholder, customPromptPlaceholder);
    const tryAgainButton = ((0, jsx_runtime_1.jsx)(uikit_1.ActionTooltip, { hotkey: constants_1.gptHotKeys.tryAgainGpt, title: (0, dialog_1.i18n)('try-again'), children: (0, jsx_runtime_1.jsxs)(uikit_1.Button, { className: (0, exports.cnGptDialog)('try-again-button'), view: "normal", size: "m", onClick: handleTryAgain, children: [(0, jsx_runtime_1.jsx)(uikit_1.Icon, { data: icons_1.ArrowRotateLeft, size: 16 }), (0, dialog_1.i18n)('try-again')] }) }));
    let content = null;
    if (loading) {
        content = (0, jsx_runtime_1.jsx)(LoadingScreen_1.LoadingScreen, {});
    }
    else if (showError) {
        content = (0, jsx_runtime_1.jsx)(ErrorScreen_1.ErrorScreen, { onRetry: handleTryAgain, onStartAgain: handleFreshStart });
    }
    else {
        content = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)("div", { className: (0, exports.cnGptDialog)('header'), children: [(0, jsx_runtime_1.jsxs)("div", { className: (0, exports.cnGptDialog)('header-top'), children: [(0, jsx_runtime_1.jsx)(IconRefuge_1.IconRefuge, { containerClassName: (0, exports.cnGptDialog)('gpt-icon'), data: GPT_1.default, refugeSize: 28, size: 16 }), (mode === 'only-custom' || mode === 'custom-and-presets') &&
                                    (showAnswer ? ((0, jsx_runtime_1.jsx)("span", { className: (0, exports.cnGptDialog)('answer-title'), children: (0, dialog_1.i18n)('answer-title') })) : ((0, jsx_runtime_1.jsxs)("div", { className: (0, exports.cnGptDialog)('custom-prompt'), children: [(0, jsx_runtime_1.jsx)(uikit_1.TextInput, { controlRef: customPromptContainerRef, view: "clear", size: "m", className: (0, exports.cnGptDialog)('custom-prompt-input'), placeholder: inputPlaceholderText, onKeyPress: handleCustomPromptKeyPress, onUpdate: handleCustomPromptUpdate }), (0, jsx_runtime_1.jsx)(uikit_1.Button, { className: (0, exports.cnGptDialog)('custom-prompt-ask-button'), view: "normal", size: "s", disabled: !customPrompt, onClick: handleCustomPromptApply, children: (0, jsx_runtime_1.jsx)(uikit_1.Icon, { data: icons_1.ArrowRight, size: 16 }) })] }))), mode === 'only-presets' && ((0, jsx_runtime_1.jsx)("div", { className: (0, exports.cnGptDialog)('alone-presets'), children: (0, jsx_runtime_1.jsx)("span", { className: (0, exports.cnGptDialog)('alone-presets-text'), children: (0, dialog_1.i18n)('only-presets-title') }) }))] }), (mode === 'custom-and-presets' || mode === 'only-presets') &&
                            ((showTryAgainButton && ((0, jsx_runtime_1.jsxs)("div", { className: (0, exports.cnGptDialog)('header-bottom'), children: [tryAgainButton, (0, jsx_runtime_1.jsx)(uikit_1.ActionTooltip, { title: (0, dialog_1.i18n)('fresh-start-button'), hotkey: constants_1.gptHotKeys.freshStartGpt, children: (0, jsx_runtime_1.jsx)(uikit_1.Button, { className: (0, exports.cnGptDialog)('back-to-start'), view: "normal", size: "m", onClick: handleFreshStart, children: (0, dialog_1.i18n)('fresh-start-button') }) })] }))) ||
                                (!disablePromptPresets && ((0, jsx_runtime_1.jsx)("div", { className: (0, exports.cnGptDialog)('header-bottom'), children: (0, jsx_runtime_1.jsx)(PresetList_1.PresetList, { disablePromptPresets: disablePromptPresets, promptPresets: promptPresets, onPresetClick: handlePresetClick }) }))) ||
                                (disablePromptPresets && showedGptAlert && ((0, jsx_runtime_1.jsx)(uikit_1.Alert, { theme: alertTheme, message: alertMessage, align: "center", className: (0, exports.cnGptDialog)('description-alert', alertClassName), onClose: onCloseAlert }))))] }), showAnswer && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: (0, exports.cnGptDialog)('body'), children: (0, jsx_runtime_1.jsx)("div", { className: (0, exports.cnGptDialog)('answer'), children: answerRender(answer) }) }), (0, jsx_runtime_1.jsxs)("div", { className: (0, exports.cnGptDialog)('footer'), children: [showAnswerActions && ((0, jsx_runtime_1.jsxs)("div", { className: (0, exports.cnGptDialog)('answer-actions'), children: [(0, jsx_runtime_1.jsx)(uikit_1.Button, { className: (0, exports.cnGptDialog)('like-button'), view: "normal", size: "m", onClick: handleLike, selected: feedbackType === 'like', loading: feedbackTypeLoading === 'like', children: (0, jsx_runtime_1.jsx)(uikit_1.Icon, { data: icons_1.ThumbsUp, size: 16 }) }), (0, jsx_runtime_1.jsx)(uikit_1.Button, { className: (0, exports.cnGptDialog)('dislike-button'), view: "normal", size: "m", onClick: handleDislike, selected: feedbackType === 'dislike', loading: feedbackTypeLoading === 'dislike', children: (0, jsx_runtime_1.jsx)(uikit_1.Icon, { data: icons_1.ThumbsDown, size: 16 }) }), feedbackType && ((0, jsx_runtime_1.jsx)("span", { className: (0, exports.cnGptDialog)('feedback-message'), children: (0, dialog_1.i18n)('feedback-message') }))] })), (0, jsx_runtime_1.jsx)(uikit_1.ActionTooltip, { title: (0, dialog_1.i18n)('close-button'), hotkey: constants_1.gptHotKeys.closeGpt, children: (0, jsx_runtime_1.jsx)(uikit_1.Button, { className: (0, exports.cnGptDialog)('close-button'), view: "flat", size: "m", onClick: onClose, children: (0, dialog_1.i18n)('close-button') }) }), (0, jsx_runtime_1.jsx)(uikit_1.ActionTooltip, { title: replaceButtonText, hotkey: constants_1.gptHotKeys.applyResultGpt, children: (0, jsx_runtime_1.jsx)(uikit_1.Button, { className: (0, exports.cnGptDialog)('apply-button'), view: "action", size: "m", onClick: handleApplyResult, children: replaceButtonText }) })] })] }))] }));
    }
    return (0, jsx_runtime_1.jsxs)("div", { className: (0, exports.cnGptDialog)(), children: [" ", content, " "] });
};
exports.GptDialog = GptDialog;
//# sourceMappingURL=GptDialog.js.map
