import { entityPayload, isExactLookupColumnEntity, isRangeEntity, isSortedLookupColumnEntity } from '../../entity-ids.js';
export function setFormulaBindingReverseEdgeSlice(state, entityId, slice) {
    const empty = slice.ptr < 0 || slice.len === 0;
    if (isRangeEntity(entityId)) {
        state.reverseRangeEdges[entityPayload(entityId)] = empty ? undefined : slice;
        return;
    }
    if (isExactLookupColumnEntity(entityId)) {
        if (empty) {
            state.reverseExactLookupColumnEdges.delete(entityPayload(entityId));
        }
        else {
            state.reverseExactLookupColumnEdges.set(entityPayload(entityId), slice);
        }
        return;
    }
    if (isSortedLookupColumnEntity(entityId)) {
        if (empty) {
            state.reverseSortedLookupColumnEdges.delete(entityPayload(entityId));
        }
        else {
            state.reverseSortedLookupColumnEdges.set(entityPayload(entityId), slice);
        }
        return;
    }
    state.reverseCellEdges[entityPayload(entityId)] = empty ? undefined : slice;
}
export function getFormulaBindingReverseEdgeSlice(state, entityId) {
    if (isRangeEntity(entityId)) {
        return state.reverseRangeEdges[entityPayload(entityId)];
    }
    if (isExactLookupColumnEntity(entityId)) {
        return state.reverseExactLookupColumnEdges.get(entityPayload(entityId));
    }
    if (isSortedLookupColumnEntity(entityId)) {
        return state.reverseSortedLookupColumnEdges.get(entityPayload(entityId));
    }
    return state.reverseCellEdges[entityPayload(entityId)];
}
export function appendFormulaBindingReverseEdge(state, edgeArena, entityId, dependentEntityId) {
    const slice = getFormulaBindingReverseEdgeSlice(state, entityId) ?? edgeArena.empty();
    setFormulaBindingReverseEdgeSlice(state, entityId, edgeArena.appendUnique(slice, dependentEntityId));
}
export function appendKnownUniqueFormulaBindingReverseEdge(state, edgeArena, entityId, dependentEntityId) {
    const slice = getFormulaBindingReverseEdgeSlice(state, entityId) ?? edgeArena.empty();
    setFormulaBindingReverseEdgeSlice(state, entityId, edgeArena.appendKnownUnique(slice, dependentEntityId));
}
export function removeFormulaBindingReverseEdge(state, edgeArena, entityId, dependentEntityId) {
    const slice = getFormulaBindingReverseEdgeSlice(state, entityId);
    if (!slice) {
        return;
    }
    setFormulaBindingReverseEdgeSlice(state, entityId, edgeArena.removeValue(slice, dependentEntityId));
}
export function syncFormulaBindingRangeDependencyEdges(state, edgeArena, rangeEntity, deps) {
    const nextSources = new Set(deps.newDependencySources);
    deps.oldDependencySources.forEach((dependencyEntity) => {
        if (!nextSources.has(dependencyEntity)) {
            removeFormulaBindingReverseEdge(state, edgeArena, dependencyEntity, rangeEntity);
        }
    });
    const priorSources = new Set(deps.oldDependencySources);
    deps.newDependencySources.forEach((dependencyEntity) => {
        if (!priorSources.has(dependencyEntity)) {
            appendFormulaBindingReverseEdge(state, edgeArena, dependencyEntity, rangeEntity);
        }
    });
}
//# sourceMappingURL=formula-binding-reverse-edges.js.map