import { useCallback } from "react";
import { getComponentById } from "../../componentTreeHelper";
export function useAgentEditorContext(input) {
    const { editContext, fieldsContext } = input;
    const collectVisibleTestIds = useCallback(() => {
        try {
            const elements = document.querySelectorAll("[data-testid]");
            const visibleTestIds = [];
            for (const element of Array.from(elements)) {
                if (!(element instanceof HTMLElement))
                    continue;
                const style = window.getComputedStyle(element);
                const isVisible = style.display !== "none" &&
                    style.visibility !== "hidden" &&
                    style.opacity !== "0" &&
                    element.offsetWidth > 0 &&
                    element.offsetHeight > 0;
                if (isVisible) {
                    const testId = element.getAttribute("data-testid");
                    if (testId && !visibleTestIds.includes(testId)) {
                        visibleTestIds.push(testId);
                    }
                }
            }
            return visibleTestIds.sort();
        }
        catch (error) {
            console.warn("[AgentTerminal] Failed to collect visible test IDs:", error);
            return [];
        }
    }, []);
    const addVisibleTestIdsToContext = useCallback((context, profileName) => {
        if (profileName === "Help") {
            const visibleTestIds = collectVisibleTestIds();
            if (visibleTestIds.length > 0) {
                return {
                    ...context,
                    additionalData: {
                        ...(context?.additionalData || {}),
                        visibleTestIds,
                    },
                };
            }
        }
        return context;
    }, [collectVisibleTestIds]);
    const buildPageContextItem = useCallback((item) => ({
        id: item.id,
        language: item.language,
        version: item.version,
        name: editContext?.item?.name,
        path: editContext?.item?.path,
    }), [editContext?.item?.name, editContext?.item?.path]);
    const buildComponentContext = useCallback((item) => {
        if (!editContext?.selection?.length)
            return undefined;
        return editContext.selection.map((componentId) => {
            let componentName;
            let componentType;
            let renderingItemId;
            if (editContext.page) {
                try {
                    const component = getComponentById(componentId, editContext.page);
                    componentName =
                        component?.datasourceItem?.name || component?.name || undefined;
                    componentType = component?.type || undefined;
                    renderingItemId = component?.rendering?.id || undefined;
                }
                catch { }
            }
            return {
                componentId,
                componentName,
                componentType,
                renderingItemId,
                pageItem: buildPageContextItem(item),
            };
        });
    }, [buildPageContextItem, editContext?.page, editContext?.selection]);
    const buildFieldContext = useCallback(() => {
        const focusedField = fieldsContext?.focusedField;
        if (!focusedField?.fieldId || !focusedField?.item?.id)
            return undefined;
        const fieldItem = focusedField.item;
        const currentItem = editContext?.currentItemDescriptor;
        let fieldItemName = fieldItem.id === currentItem?.id ? editContext?.item?.name : undefined;
        if (!fieldItemName && editContext?.page) {
            try {
                const component = getComponentById(fieldItem.id, editContext.page);
                fieldItemName =
                    component?.datasourceItem?.name || component?.name || undefined;
            }
            catch { }
        }
        return {
            fieldId: focusedField.fieldId,
            fieldName: focusedField.fieldName,
            item: {
                id: fieldItem.id,
                language: fieldItem.language || currentItem?.language || "en",
                version: fieldItem.version ?? currentItem?.version ?? 0,
                name: fieldItem.name || fieldItemName,
            },
        };
    }, [
        editContext?.currentItemDescriptor,
        editContext?.item?.name,
        editContext?.page,
        fieldsContext?.focusedField,
    ]);
    const buildEditorContextPayload = useCallback((item) => ({
        items: item ? [buildPageContextItem(item)] : undefined,
        currentItemId: item?.id,
        components: item ? buildComponentContext(item) : undefined,
        field: buildFieldContext(),
        activeWorkspace: editContext?.workspaceId,
        hasPageLoaded: !!editContext?.getActiveSlotContext()?.primaryPageViewContext?.page,
        openSidebars: editContext?.openSidebars,
    }), [
        buildComponentContext,
        buildFieldContext,
        buildPageContextItem,
        editContext,
    ]);
    const buildCurrentContext = useCallback(() => {
        const item = editContext?.currentItemDescriptor;
        return buildEditorContextPayload(item);
    }, [buildEditorContextPayload, editContext?.currentItemDescriptor]);
    return {
        addVisibleTestIdsToContext,
        buildEditorContextPayload,
        buildCurrentContext,
    };
}
//# sourceMappingURL=useAgentEditorContext.js.map