import { useCallback } from "react";
import { useEditContext } from "../../client/editContext";
import { loadManualToc } from "../../services/manualService";
function isDirectlyNavigableManualSection(section) {
    return !!section.hasContent || section.children.length === 0;
}
function resolveFirstNavigableManualSection(section) {
    if (isDirectlyNavigableManualSection(section) ||
        section.children.length === 0) {
        return section;
    }
    return resolveFirstNavigableManualSection(section.children[0]);
}
function findManualSectionIdByTitle(sections, title) {
    const normalizedTitle = title.trim().toLowerCase();
    if (!normalizedTitle)
        return null;
    for (const section of sections) {
        if (section.title?.trim().toLowerCase() === normalizedTitle) {
            return resolveFirstNavigableManualSection(section).id;
        }
        const childMatch = findManualSectionIdByTitle(section.children, normalizedTitle);
        if (childMatch)
            return childMatch;
    }
    return null;
}
export function useAgentQuickActions(input) {
    const { commands, focusPromptAtEnd, isExecuting, setActivePlaceholderInput, setInputPlaceholder, setPrompt, } = input;
    const editContext = useEditContext();
    const stopIfExecuting = useCallback(() => {
        if (!isExecuting)
            return;
        try {
            void commands.stopRun();
        }
        catch { }
    }, [commands, isExecuting]);
    const openManualQuickAction = useCallback(async (action) => {
        const manualSectionId = action.manualSectionId?.trim();
        const manualSectionTitle = action.manualSectionTitle?.trim();
        if (!manualSectionId && !manualSectionTitle)
            return false;
        if (!editContext)
            return false;
        let resolvedSectionId = manualSectionId || null;
        if (!resolvedSectionId && manualSectionTitle) {
            try {
                const toc = await loadManualToc();
                resolvedSectionId = findManualSectionIdByTitle(toc, manualSectionTitle);
            }
            catch (error) {
                console.warn("Failed to load manual table of contents:", error);
            }
        }
        if (!resolvedSectionId) {
            console.warn("Unable to resolve manual quick action target:", action);
            return false;
        }
        editContext.setSelectedHelpSectionId(resolvedSectionId);
        editContext.setHelpTerminalActiveTab("manual");
        editContext.setShowHelpTerminal(true);
        return true;
    }, [editContext]);
    const handleSummaryQuickAction = useCallback(async (action) => {
        if (await openManualQuickAction(action))
            return;
        const text = (action.prompt || action.value || action.label || "").trim();
        if (!text)
            return;
        stopIfExecuting();
        void commands.sendQuickMessage(text);
    }, [commands, openManualQuickAction, stopIfExecuting]);
    const handleMessageQuickAction = useCallback(async (action) => {
        if (await openManualQuickAction(action))
            return;
        const text = (action.prompt || action.value || action.label || "").trim();
        if (!text)
            return;
        if (/\{\{([^{}]+)\}\}|<<([^<>]+)>>/.test(text)) {
            setActivePlaceholderInput({
                text,
                behavior: action.behavior,
            });
            return;
        }
        if (action.behavior === "compose") {
            setPrompt(text);
            setInputPlaceholder(action.placeholder || "Review and edit, then press Enter to send");
            focusPromptAtEnd();
            return;
        }
        stopIfExecuting();
        void commands.sendQuickMessage(text);
    }, [
        commands,
        focusPromptAtEnd,
        setActivePlaceholderInput,
        setInputPlaceholder,
        setPrompt,
        openManualQuickAction,
        stopIfExecuting,
    ]);
    return {
        handleSummaryQuickAction,
        handleMessageQuickAction,
    };
}
//# sourceMappingURL=useAgentQuickActions.js.map