export function getAgentContextItemKey(item) {
    if (!item?.id)
        return null;
    return [
        String(item.id).toLowerCase(),
        String(item.language ?? "").toLowerCase(),
        String(item.version ?? ""),
    ].join("|");
}
export function getAgentContextComponentKey(component) {
    if (!component?.componentId)
        return null;
    const pageKey = getAgentContextItemKey(component.pageItem);
    return `${String(component.componentId).toLowerCase()}|${pageKey ?? ""}`;
}
function mergeByKey(preferred, fallback, getKey) {
    const merged = [];
    const seen = new Set();
    for (const value of [...(preferred ?? []), ...(fallback ?? [])]) {
        const key = getKey(value);
        if (!key || seen.has(key))
            continue;
        seen.add(key);
        merged.push(value);
    }
    return merged;
}
/**
 * Whether a built editor-context payload actually references something a user is
 * viewing. `buildCurrentContext()` always returns an object (it can carry only
 * workspace/page-loaded/sidebar flags), so gate UI and merges on real content.
 */
export function hasViewedContent(viewed) {
    return !!(viewed?.items?.length || viewed?.components?.length || viewed?.field);
}
export function mergeViewedContext(storedContext, liveContext) {
    if (!storedContext && !liveContext) {
        return null;
    }
    if (!liveContext) {
        return storedContext ? { ...storedContext } : null;
    }
    const merged = {
        ...(storedContext || {}),
    };
    const items = mergeByKey(liveContext.items, storedContext?.items, getAgentContextItemKey);
    if (items.length > 0) {
        merged.items = items;
    }
    else {
        delete merged.items;
    }
    const components = mergeByKey(liveContext.components, storedContext?.components, getAgentContextComponentKey);
    if (components.length > 0) {
        merged.components = components;
    }
    else {
        delete merged.components;
    }
    merged.currentItemId =
        liveContext.currentItemId ?? storedContext?.currentItemId;
    if (liveContext.field) {
        merged.field = {
            ...(storedContext?.field || {}),
            ...liveContext.field,
            fieldName: liveContext.field.fieldName ?? storedContext?.field?.fieldName,
            fieldType: liveContext.field.fieldType ?? storedContext?.field?.fieldType,
            item: liveContext.field.item ?? storedContext?.field?.item,
        };
    }
    else if (!storedContext?.field) {
        delete merged.field;
    }
    if (liveContext.activeWorkspace !== undefined) {
        merged.activeWorkspace = liveContext.activeWorkspace;
    }
    if (liveContext.hasPageLoaded !== undefined) {
        merged.hasPageLoaded = liveContext.hasPageLoaded;
    }
    if (liveContext.openSidebars !== undefined) {
        merged.openSidebars = liveContext.openSidebars;
    }
    return merged;
}
export function toEditorContextSnapshot(context) {
    if (!hasViewedContent(context)) {
        return undefined;
    }
    const snapshot = {};
    if (context?.selectedText !== undefined)
        snapshot.selectedText = context.selectedText;
    if (context?.items !== undefined)
        snapshot.items = context.items;
    if (context?.currentItemId !== undefined) {
        snapshot.currentItemId = context.currentItemId;
    }
    if (context?.components !== undefined)
        snapshot.components = context.components;
    if (context?.field !== undefined)
        snapshot.field = context.field;
    if (context?.activeWorkspace !== undefined) {
        snapshot.activeWorkspace = context.activeWorkspace;
    }
    if (context?.hasPageLoaded !== undefined) {
        snapshot.hasPageLoaded = context.hasPageLoaded;
    }
    if (context?.openSidebars !== undefined)
        snapshot.openSidebars = context.openSidebars;
    return snapshot;
}
//# sourceMappingURL=agentContextMerge.js.map