"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useGpt = void 0;
const react_1 = require("react");
const utils_1 = require("../utils.js");
const useGpt = ({ markup, promptPresets, onCustomPromptApply, onPromptPresetClick, onTryAgain, onLike, onDislike, onApplyResult, onUpdate, }) => {
    const [answer, setAnswer] = (0, react_1.useState)();
    const [lastRequestData, setLastRequestData] = (0, react_1.useState)();
    const [customPrompt, setCustomPrompt] = (0, react_1.useState)('');
    const [loading, setLoading] = (0, react_1.useState)(false);
    const [error, setError] = (0, react_1.useState)(false);
    const [feedbackType, setFeedbackType] = (0, react_1.useState)(undefined);
    const [feedbackTypeLoading, setFeedbackTypeLoading] = (0, react_1.useState)(undefined);
    const makeRequest = (0, react_1.useCallback)(async (requestFunction, data) => {
        if (!requestFunction) {
            return;
        }
        let result;
        try {
            setLoading(true);
            setError(false);
            setLastRequestData(data);
            result = await requestFunction(data);
            if (result) {
                setAnswer(result);
                setFeedbackType(undefined);
            }
        }
        catch (error) {
            console.error(error);
            setError(true);
        }
        finally {
            setLoading(false);
            setTimeout(() => {
                // hack for popup rerender
                // When a lot of text is entered into the GPT popup,
                // it expands and goes beyond the boundaries. However,
                // the popup does not handle height changes.
                window.dispatchEvent(new CustomEvent('scroll'));
            });
            onUpdate?.(result);
        }
    }, [onUpdate]);
    const handleLike = (0, react_1.useCallback)(async () => {
        if (!onLike || !lastRequestData) {
            return;
        }
        try {
            setFeedbackType(undefined);
            setFeedbackTypeLoading('like');
            await onLike(lastRequestData);
            setFeedbackType('like');
        }
        catch (error) {
            console.error(error);
        }
        finally {
            setFeedbackTypeLoading(undefined);
        }
    }, [lastRequestData, onLike]);
    const handleDislike = (0, react_1.useCallback)(async () => {
        if (!onDislike || !lastRequestData) {
            return;
        }
        try {
            setFeedbackType(undefined);
            setFeedbackTypeLoading('dislike');
            await onDislike(lastRequestData);
            setFeedbackType('dislike');
        }
        catch (error) {
            console.error(error);
        }
        finally {
            setFeedbackTypeLoading(undefined);
        }
    }, [lastRequestData, onDislike]);
    const handleCustomPromptApply = (0, react_1.useCallback)(async () => {
        if (!customPrompt) {
            return;
        }
        const gptRequestData = {
            markup,
            customPrompt,
        };
        await makeRequest(onCustomPromptApply, gptRequestData);
    }, [customPrompt, makeRequest, markup, onCustomPromptApply]);
    const handleCustomPromptKeyPress = (0, react_1.useCallback)(async (event) => {
        if (!(0, utils_1.isEnter)(event)) {
            return;
        }
        await handleCustomPromptApply();
    }, [handleCustomPromptApply]);
    const handlePresetClick = (0, react_1.useCallback)(async (data) => {
        const gptRequestData = {
            markup,
            promptData: data,
        };
        await makeRequest(onPromptPresetClick, gptRequestData);
    }, [makeRequest, markup, onPromptPresetClick]);
    const handleTryAgain = (0, react_1.useCallback)(async () => {
        if (!lastRequestData) {
            return;
        }
        await makeRequest(onTryAgain, lastRequestData);
    }, [lastRequestData, makeRequest, onTryAgain]);
    const handleApplyResult = (0, react_1.useCallback)(() => {
        onApplyResult(answer?.rawText ?? '');
    }, [answer?.rawText, onApplyResult]);
    const handleFreshStart = (0, react_1.useCallback)(() => {
        setError(false);
        setLastRequestData(undefined);
        setAnswer(undefined);
        setCustomPrompt('');
        setFeedbackType(undefined);
        onUpdate?.(undefined);
    }, [onUpdate]);
    const showTryAgainButton = Boolean(lastRequestData && onTryAgain && !loading);
    const showAnswer = Boolean(answer && !loading && !error);
    const showError = error && !loading;
    const showAnswerActions = (onLike || onDislike) && showAnswer;
    let mode = 'custom-and-presets';
    if (onCustomPromptApply && !promptPresets?.length) {
        mode = 'only-custom';
    }
    else if (!onCustomPromptApply && promptPresets?.length) {
        mode = 'only-presets';
    }
    return {
        answer,
        customPrompt,
        lastRequestData,
        loading,
        error,
        mode,
        feedbackType,
        feedbackTypeLoading,
        handleLike,
        handleDislike,
        handleCustomPromptUpdate: setCustomPrompt,
        handleCustomPromptKeyPress,
        handleCustomPromptApply,
        handlePresetClick,
        handleTryAgain,
        handleFreshStart,
        handleApplyResult,
        showTryAgainButton,
        showAnswer,
        showError,
        showAnswerActions,
    };
};
exports.useGpt = useGpt;
//# sourceMappingURL=useGpt.js.map
