export function freshMatrixOverlapsFormulaDependencies(args, matrix) {
    const rowEnd = matrix.rowStart + matrix.rowCount - 1;
    const colEnd = matrix.formulaCol;
    if (args.getRegionFormulaSubscriptionCount?.() === args.state.formulas.size &&
        args.hasRegionFormulaSubscriptionsOverlappingRange?.(matrix.sheetId, matrix.rowStart, rowEnd, matrix.colStart, colEnd) === false) {
        return false;
    }
    if (args.state.ranges.size === 0 &&
        args.hasRegionFormulaSubscriptionsIntersectingRect?.(matrix.sheetId, matrix.rowStart, rowEnd, matrix.colStart, colEnd) === false) {
        return false;
    }
    let overlaps = false;
    args.state.formulas.forEach((formula) => {
        if (overlaps) {
            return;
        }
        overlaps =
            directAggregateOverlapsFreshMatrix(formula.directAggregate, matrix.sheet.name, matrix.rowStart, rowEnd, matrix.colStart, colEnd) ||
                directCriteriaOverlapsFreshMatrix(formula.directCriteria, matrix.sheet.name, matrix.rowStart, rowEnd, matrix.colStart, colEnd) ||
                rangeDependenciesOverlapFreshMatrix(args, formula, matrix.sheetId, matrix.rowStart, rowEnd, matrix.colStart, colEnd);
    });
    return overlaps;
}
function directAggregateOverlapsFreshMatrix(aggregate, sheetName, rowStart, rowEnd, colStart, colEnd) {
    return (aggregate !== undefined &&
        aggregate.sheetName === sheetName &&
        aggregate.rowEnd >= rowStart &&
        aggregate.rowStart <= rowEnd &&
        aggregate.colEnd >= colStart &&
        aggregate.col <= colEnd);
}
function directCriteriaOverlapsFreshMatrix(criteria, sheetName, rowStart, rowEnd, colStart, colEnd) {
    if (criteria === undefined) {
        return false;
    }
    const rangeOverlaps = (range) => range.sheetName === sheetName && range.rowEnd >= rowStart && range.rowStart <= rowEnd && range.col >= colStart && range.col <= colEnd;
    return ((criteria.aggregateRange !== undefined && rangeOverlaps(criteria.aggregateRange)) ||
        criteria.criteriaPairs.some((pair) => rangeOverlaps(pair.range)));
}
function rangeDependenciesOverlapFreshMatrix(args, formula, sheetId, rowStart, rowEnd, colStart, colEnd) {
    for (let index = 0; index < formula.rangeDependencies.length; index += 1) {
        if (rangeDescriptorOverlapsFreshMatrix(args, formula.rangeDependencies[index], sheetId, rowStart, rowEnd, colStart, colEnd)) {
            return true;
        }
    }
    for (let index = 0; index < formula.graphRangeDependencies.length; index += 1) {
        if (rangeDescriptorOverlapsFreshMatrix(args, formula.graphRangeDependencies[index], sheetId, rowStart, rowEnd, colStart, colEnd)) {
            return true;
        }
    }
    return false;
}
function rangeDescriptorOverlapsFreshMatrix(args, rangeIndex, sheetId, rowStart, rowEnd, colStart, colEnd) {
    const descriptor = args.state.ranges.getDescriptor(rangeIndex);
    return (descriptor.sheetId === sheetId &&
        descriptor.row2 >= rowStart &&
        descriptor.row1 <= rowEnd &&
        descriptor.col2 >= colStart &&
        descriptor.col1 <= colEnd);
}
//# sourceMappingURL=operation-fresh-matrix-dependency-overlap.js.map