/**
 * Opens the Agents panel and seeds it with context from a review comment.
 * Safe to call with an undefined editContext; it will no-op.
 */
export function openAiAgentForComment(comment, editContext) {
    if (!editContext)
        return;
    const hasAgentsPanel = editContext.configuration?.editor?.sidebars?.some((sidebar) => sidebar.id === "agents-panel");
    if (!hasAgentsPanel) {
        return;
    }
    // Respect quota if available
    if (editContext.isQuotaExceeded) {
        editContext.showToast?.("AI quota exceeded. Please try again later or adjust settings.");
        return;
    }
    const selectedText = (() => {
        if (typeof comment.rangeStart === "number" &&
            typeof comment.rangeEnd === "number" &&
            comment.fieldValue) {
            try {
                return comment.fieldValue.substring(Math.max(0, comment.rangeStart), Math.max(comment.rangeStart, comment.rangeEnd));
            }
            catch {
                return undefined;
            }
        }
        return undefined;
    })();
    const promptParts = [];
    const text = (comment.text || "").trim();
    if (text)
        promptParts.push(`Comment: "${text}"`);
    if (selectedText)
        promptParts.push(`Selected text: "${selectedText}"`);
    const initialPrompt = promptParts.length > 0
        ? `Please help resolve this review comment. ${promptParts.join(" | ")}`
        : "Please help resolve this review comment.";
    try {
        if (editContext.isMobile) {
            editContext.openSidebar("agents-panel");
        }
        else {
            editContext.setShowAgentsPanel(true);
        }
        const initialMetadata = {
            items: [
                {
                    id: comment.mainItemId ||
                        editContext.currentItemDescriptor?.id,
                    language: comment.language,
                    version: comment.version,
                    name: editContext.item?.name,
                },
            ],
            components: editContext.selection && editContext.selection.length
                ? editContext.selection.map((componentId) => ({
                    componentId,
                    pageItem: {
                        id: comment.mainItemId ||
                            editContext.currentItemDescriptor?.id,
                        language: comment.language,
                        version: comment.version,
                        name: editContext.item?.name,
                    },
                }))
                : undefined,
            field: comment.fieldId && comment.itemId
                ? {
                    fieldId: comment.fieldId,
                    fieldName: comment.fieldName,
                    item: {
                        id: comment.itemId,
                        language: comment.language,
                        version: comment.version,
                        name: comment.itemName,
                    },
                }
                : undefined,
            comment: {
                id: comment.id,
                text: comment.text,
                fieldName: comment.fieldName,
                itemName: comment.itemName,
                author: comment.author,
                selectedText: selectedText,
                rangeStart: comment.rangeStart,
                rangeEnd: comment.rangeEnd,
            },
            additionalData: {
                initialPrompt,
                profileId: editContext.parheliaSettings?.commentResolveProfileId || undefined,
                profileName: editContext.parheliaSettings?.commentResolveProfileName || undefined,
            },
        };
        // Ask Agents UI to open a new terminal seeded with this metadata
        const dispatchEvent = () => {
            try {
                window.dispatchEvent(new CustomEvent("editor:addNewAgent", {
                    detail: { metadata: initialMetadata },
                }));
            }
            catch { }
        };
        if (editContext.isMobile) {
            // 500ms delay on mobile to ensure the sidebar is mounted and listening.
            // Layout transitions on mobile can take time.
            setTimeout(dispatchEvent, 500);
        }
        else {
            dispatchEvent();
        }
    }
    catch (e) {
        console.error("Failed to start agent for comment:", e);
        editContext.showToast?.("Failed to start AI agent for this comment.");
    }
}
//# sourceMappingURL=commentAi.js.map