import { isNonEmptyArray } from "./arrays.js";
import { DeepMerger } from "./mergeDeep.js";
import { isNonNullObject } from "./objects.js";
export function isExecutionPatchIncrementalResult(value) {
    return "incremental" in value;
}
export function isExecutionPatchInitialResult(value) {
    return "hasNext" in value && "data" in value;
}
export function isExecutionPatchResult(value) {
    return (isExecutionPatchIncrementalResult(value) ||
        isExecutionPatchInitialResult(value));
}
// This function detects an Apollo payload result before it is transformed
// into a FetchResult via HttpLink; it cannot detect an ApolloPayloadResult
// once it leaves the link chain.
export function isApolloPayloadResult(value) {
    return isNonNullObject(value) && "payload" in value;
}
export function mergeIncrementalData(prevResult, result) {
    let mergedData = prevResult;
    const merger = new DeepMerger();
    if (isExecutionPatchIncrementalResult(result) &&
        isNonEmptyArray(result.incremental)) {
        result.incremental.forEach(({ data, path }) => {
            for (let i = path.length - 1; i >= 0; --i) {
                const key = path[i];
                const isNumericKey = !isNaN(+key);
                const parent = isNumericKey ? [] : {};
                parent[key] = data;
                data = parent;
            }
            mergedData = merger.merge(mergedData, data);
        });
    }
    return mergedData;
}
//# sourceMappingURL=incrementalResult.js.map
