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 activePageDescriptor = editContext?.page?.item?.descriptor;
    const rootItemDescriptor = editContext?.currentItemDescriptor;
    const currentItemDescriptor = activePageDescriptor ?? rootItemDescriptor;
    const buildPageContextItem = useCallback((item) => ({
        id: item.id,
        language: item.language,
        version: item.version,
        name: item.name ||
            item.displayName ||
            (activePageDescriptor?.id === item.id
                ? editContext?.page?.item?.name
                : undefined) ||
            editContext?.item?.name,
        path: item.path ||
            (activePageDescriptor?.id === item.id
                ? editContext?.page?.item?.path
                : undefined) ||
            editContext?.item?.path,
    }), [
        activePageDescriptor?.id,
        editContext?.item?.name,
        editContext?.item?.path,
        editContext?.page?.item?.name,
        editContext?.page?.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 = currentItemDescriptor;
        let fieldItemName = fieldItem.id === currentItem?.id
            ? editContext?.page?.item?.name || 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,
            },
        };
    }, [
        currentItemDescriptor?.id,
        currentItemDescriptor?.language,
        currentItemDescriptor?.version,
        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?.getActiveSlotContext,
        editContext?.openSidebars,
        editContext?.workspaceId,
    ]);
    const buildCurrentContext = useCallback(() => {
        return buildEditorContextPayload(currentItemDescriptor);
    }, [
        buildEditorContextPayload,
        currentItemDescriptor?.id,
        currentItemDescriptor?.language,
        currentItemDescriptor?.version,
        currentItemDescriptor?.name,
        currentItemDescriptor?.displayName,
        currentItemDescriptor?.path,
    ]);
    return {
        addVisibleTestIdsToContext,
        buildEditorContextPayload,
        buildCurrentContext,
    };
}
//# sourceMappingURL=useAgentEditorContext.js.map