import { useCallback, useState } from 'react';
import { isEnter } from "../utils.js";
export const useGpt = ({ markup, promptPresets, onCustomPromptApply, onPromptPresetClick, onTryAgain, onLike, onDislike, onApplyResult, onUpdate, }) => {
    const [answer, setAnswer] = useState();
    const [lastRequestData, setLastRequestData] = useState();
    const [customPrompt, setCustomPrompt] = useState('');
    const [loading, setLoading] = useState(false);
    const [error, setError] = useState(false);
    const [feedbackType, setFeedbackType] = useState(undefined);
    const [feedbackTypeLoading, setFeedbackTypeLoading] = useState(undefined);
    const makeRequest = 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 = 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 = 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 = useCallback(async () => {
        if (!customPrompt) {
            return;
        }
        const gptRequestData = {
            markup,
            customPrompt,
        };
        await makeRequest(onCustomPromptApply, gptRequestData);
    }, [customPrompt, makeRequest, markup, onCustomPromptApply]);
    const handleCustomPromptKeyPress = useCallback(async (event) => {
        if (!isEnter(event)) {
            return;
        }
        await handleCustomPromptApply();
    }, [handleCustomPromptApply]);
    const handlePresetClick = useCallback(async (data) => {
        const gptRequestData = {
            markup,
            promptData: data,
        };
        await makeRequest(onPromptPresetClick, gptRequestData);
    }, [makeRequest, markup, onPromptPresetClick]);
    const handleTryAgain = useCallback(async () => {
        if (!lastRequestData) {
            return;
        }
        await makeRequest(onTryAgain, lastRequestData);
    }, [lastRequestData, makeRequest, onTryAgain]);
    const handleApplyResult = useCallback(() => {
        onApplyResult(answer?.rawText ?? '');
    }, [answer?.rawText, onApplyResult]);
    const handleFreshStart = 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,
    };
};
//# sourceMappingURL=useGpt.js.map
