import { entityPayload, makeExactLookupColumnEntity, makeSortedLookupColumnEntity } from '../../entity-ids.js';
import { aggregateColumnDependencyKey } from './direct-formula-recalc-helpers.js';
export function createOperationColumnDependencyTrackerService(args) {
    const hasTrackedExactLookupDependents = (sheetId, col) => {
        const exactLookupEdges = args.reverseState.reverseExactLookupColumnEdges;
        if (exactLookupEdges.size === 0) {
            return false;
        }
        const slice = exactLookupEdges.get(entityPayload(makeExactLookupColumnEntity(sheetId, col)));
        return slice !== undefined && slice.len > 0;
    };
    const hasTrackedSortedLookupDependents = (sheetId, col) => {
        const sortedLookupEdges = args.reverseState.reverseSortedLookupColumnEdges;
        if (sortedLookupEdges.size === 0) {
            return false;
        }
        const slice = sortedLookupEdges.get(entityPayload(makeSortedLookupColumnEntity(sheetId, col)));
        return slice !== undefined && slice.len > 0;
    };
    const hasTrackedDirectRangeDependents = (sheetId, col) => {
        let hasRegionSubscriptions = args.hasRegionFormulaSubscriptionsForColumnAt?.(sheetId, col);
        if (hasRegionSubscriptions === undefined) {
            const sheetName = args.workbook.getSheetNameById(sheetId);
            hasRegionSubscriptions = sheetName ? args.hasRegionFormulaSubscriptionsForColumn(sheetName, col) : false;
        }
        return (hasRegionSubscriptions ||
            (args.reverseState.reverseAggregateColumnEdges.get(aggregateColumnDependencyKey(sheetId, col))?.size ?? 0) > 0);
    };
    const hasTrackedColumnDependents = (sheetId, col) => hasTrackedExactLookupDependents(sheetId, col) ||
        hasTrackedSortedLookupDependents(sheetId, col) ||
        hasTrackedDirectRangeDependents(sheetId, col);
    const hasTrackedColumnDependentsAnywhere = () => {
        return (args.reverseState.reverseExactLookupColumnEdges.size > 0 ||
            args.reverseState.reverseSortedLookupColumnEdges.size > 0 ||
            args.reverseState.reverseAggregateColumnEdges.size > 0 ||
            (args.hasRegionFormulaSubscriptions?.() ?? true));
    };
    const hasNoCellDependents = (cellIndex) => {
        const slice = args.reverseState.reverseCellEdges[cellIndex];
        return slice === undefined || slice.len === 0 || slice.ptr < 0;
    };
    return {
        hasTrackedExactLookupDependents,
        hasTrackedSortedLookupDependents,
        hasTrackedDirectRangeDependents,
        hasTrackedColumnDependents,
        hasTrackedColumnDependentsAnywhere,
        hasNoCellDependents,
    };
}
export function createOperationTrackedColumnDependencyFlagResolver(args) {
    const flagsBySheet = new Map();
    return {
        clear() {
            flagsBySheet.clear();
        },
        resolve(sheetId, col) {
            let flagsByColumn = flagsBySheet.get(sheetId);
            if (flagsByColumn === undefined) {
                flagsByColumn = new Map();
                flagsBySheet.set(sheetId, flagsByColumn);
            }
            const cached = flagsByColumn.get(col);
            if (cached !== undefined) {
                return cached;
            }
            const hasExactLookupDependents = args.hasTrackedExactLookupDependents(sheetId, col);
            const hasSortedLookupDependents = args.hasTrackedSortedLookupDependents(sheetId, col);
            const hasAggregateDependents = args.hasTrackedDirectRangeDependents(sheetId, col);
            const next = {
                hasExactLookupDependents,
                hasSortedLookupDependents,
                hasAggregateDependents,
                needsLookupValueRead: hasExactLookupDependents || hasSortedLookupDependents || hasAggregateDependents,
            };
            flagsByColumn.set(col, next);
            return next;
        },
    };
}
//# sourceMappingURL=operation-column-dependency-tracker.js.map