import { useEffect, useState } from "react";
import { getComponentById } from "../../componentTreeHelper";
export function useAgentContextDisplayNames(input) {
    const { agentMetadata, editContext } = input;
    const [resolvedPageName, setResolvedPageName] = useState(undefined);
    const [resolvedComponentName, setResolvedComponentName] = useState(undefined);
    const [resolvedFieldName, setResolvedFieldName] = useState(undefined);
    useEffect(() => {
        const metaCtx = agentMetadata;
        if (!metaCtx) {
            setResolvedPageName(undefined);
            setResolvedComponentName(undefined);
            setResolvedFieldName(undefined);
            return;
        }
        if (metaCtx.items?.length) {
            const firstPage = metaCtx.items[0];
            if (firstPage) {
                let name = firstPage.name;
                if (!name &&
                    editContext?.item?.descriptor.id === firstPage.id &&
                    editContext?.item?.name) {
                    name = editContext.item.name;
                }
                else if (!name) {
                    name = undefined;
                }
                setResolvedPageName(name);
            }
            else {
                setResolvedPageName(undefined);
            }
        }
        else {
            setResolvedPageName(undefined);
        }
        if (metaCtx.components?.length && editContext?.page) {
            const firstComponent = metaCtx.components[0];
            if (firstComponent?.componentId) {
                try {
                    const comp = getComponentById(firstComponent.componentId, editContext.page);
                    setResolvedComponentName(comp?.name);
                }
                catch {
                    setResolvedComponentName(undefined);
                }
            }
            else {
                setResolvedComponentName(undefined);
            }
        }
        else {
            setResolvedComponentName(undefined);
        }
        let isCurrent = true;
        void (async () => {
            if (metaCtx.field && editContext?.itemsRepository) {
                try {
                    const itemDescriptor = metaCtx.field.item || {
                        id: editContext.currentItemDescriptor?.id || "",
                        language: editContext.currentItemDescriptor?.language || "en",
                        version: editContext.currentItemDescriptor?.version || 0,
                    };
                    const field = await editContext.itemsRepository.getField({
                        item: itemDescriptor,
                        fieldId: metaCtx.field.fieldId,
                    });
                    if (isCurrent) {
                        setResolvedFieldName(field?.name || metaCtx.field.fieldName);
                    }
                }
                catch {
                    if (isCurrent) {
                        setResolvedFieldName(metaCtx.field.fieldName);
                    }
                }
            }
            else if (isCurrent) {
                setResolvedFieldName(undefined);
            }
        })();
        return () => {
            isCurrent = false;
        };
    }, [
        agentMetadata,
        editContext?.page,
        editContext?.item,
        editContext?.currentItemDescriptor,
        editContext?.itemsRepository,
    ]);
    return {
        resolvedPageName,
        resolvedComponentName,
        resolvedFieldName,
    };
}
//# sourceMappingURL=useAgentContextDisplayNames.js.map