<commit_before>import { Injectable} from '@angular/core';
import { IgxSummaryResult } from './grid-summary';
import { DataUtil } from '../../data-operations/data-util';
import { cloneArray } from '../../core/utils';

/** @hidden */
@Injectable()
export class IgxGridSummaryService {
    protected summaryCacheMap: Map<string, Map<string, any[]>> = new Map<string, Map<string, IgxSummaryResult[]>>();
    public grid;
    public rootSummaryID = 'igxGridRootSummary';
    public summaryHeight = 0;
    public maxSummariesLenght = 0;
    public groupingExpressions = [];
    public retriggerRootPipe = 0;
    public deleteOperation = false;

    public recalculateSummaries() {
        this.resetSummaryHeight();
        this.grid.calculateGridHeight();
        this.grid.cdr.detectChanges();
    }

    public clearSummaryCache(args?) {
        if (!this.summaryCacheMap.size) { return; }
        if (!args) {
            this.summaryCacheMap.clear();
            if (this.grid && this.grid.rootSummariesEnabled) {
                this.retriggerRootPipe++;
            }
            return;
        }
        if (args.data) {
            let rowID = args.rowID;
            if (!args.rowID) {
                rowID = this.grid.primaryKey ? args.data[this.grid.primaryKey] : args.data;
            }
            this.removeSummaries(rowID);
        }
        if (args.rowID !== undefined && args.rowID !== null) {
            const columnName = args.cellID ? this.grid.columnList.find(col => col.index === args.cellID.columnID).field : undefined;
            const isGroupedColumn = this.grid.groupingExpressions &&
                    this.grid.groupingExpressions.map(expr => expr.fieldName).indexOf(columnName) !== -1;
            if (columnName && isGroupedColumn) {
                this.removeSummaries(args.rowID);
                return;
            }
            this.removeSummaries(args.rowID, columnName);
        }
    }

    public removeSummaries(rowID, columnName?) {
        if (this.summaryCacheMap.size === 0) { return; }
        this.deleteSummaryCache(this.rootSummaryID, columnName);
        if (this.summaryCacheMap.size === 1 && this.summaryCacheMap.has(this.rootSummaryID)) { return; }
        if (this.isTreeGrid) {
            if (this.grid.transactions.enabled && this.deleteOperation) {
                this.deleteOperation = false;
                // TODO: this.removeChildRowSummaries(rowID, columnName);
                this.summaryCacheMap.clear();
                return;
            }
            this.removeAllTreeGridSummaries(rowID, columnName);
        } else if (this.isHierarchicalGrid) {
            if (this.grid.transactions.enabled && this.deleteOperation) {
                this.deleteOperation = false;
                this.summaryCacheMap.clear();
                return;
            }
        } else {
           const summaryIds = this.getSummaryID(rowID, this.grid.groupingExpressions);
           summaryIds.forEach(id => {
               this.deleteSummaryCache(id, columnName);
           });
        }
    }

    public removeSummariesCachePerColumn(columnName) {
        this.summaryCacheMap.forEach((cache) => {
            if (cache.get(columnName)) {
                cache.delete(columnName);
            }
        });
        if (this.grid.rootSummariesEnabled) {  this.retriggerRootPipe++; }
    }

    public calcMaxSummaryHeight() {
        if (this.summaryHeight) {
            return this.summaryHeight;
        }
        if (!this.grid.data) {return this.summaryHeight = 0; }
        let maxSummaryLength = 0;<commit_msg>fix(IgxSummaries): update summaries when row editing is done #4949<commit_after>import { Injectable} from '@angular/core';
import { IgxSummaryResult } from './grid-summary';
import { DataUtil } from '../../data-operations/data-util';
import { cloneArray } from '../../core/utils';

/** @hidden */
@Injectable()
export class IgxGridSummaryService {
    protected summaryCacheMap: Map<string, Map<string, any[]>> = new Map<string, Map<string, IgxSummaryResult[]>>();
    public grid;
    public rootSummaryID = 'igxGridRootSummary';
    public summaryHeight = 0;
    public maxSummariesLenght = 0;
    public groupingExpressions = [];
    public retriggerRootPipe = 0;
    public deleteOperation = false;

    public recalculateSummaries() {
        this.resetSummaryHeight();
        this.grid.calculateGridHeight();
        this.grid.cdr.detectChanges();
    }

    public clearSummaryCache(args?) {
        if (!this.summaryCacheMap.size) { return; }
        if (!args) {
            this.summaryCacheMap.clear();
            if (this.grid && this.grid.rootSummariesEnabled) {
                this.retriggerRootPipe++;
            }
            return;
        }
        if (args.data) {
            const rowID = this.grid.primaryKey ? args.data[this.grid.primaryKey] : args.data;
            this.removeSummaries(rowID);
        }
        if (args.rowID !== undefined && args.rowID !== null) {
            let columnName = args.cellID ? this.grid.columnList.find(col => col.index === args.cellID.columnID).field : undefined;
            if (columnName && this.grid.rowEditable) { return; }

            const isGroupedColumn = this.grid.groupingExpressions &&
                    this.grid.groupingExpressions.map(expr => expr.fieldName).indexOf(columnName) !== -1;
            if (columnName && isGroupedColumn ) {
                columnName = undefined;
            }
            this.removeSummaries(args.rowID, columnName);
        }
    }

    public removeSummaries(rowID, columnName?) {
        this.deleteSummaryCache(this.rootSummaryID, columnName);
        if (this.summaryCacheMap.size === 1 && this.summaryCacheMap.has(this.rootSummaryID)) { return; }
        if (this.isTreeGrid) {
            if (this.grid.transactions.enabled && this.deleteOperation) {
                this.deleteOperation = false;
                // TODO: this.removeChildRowSummaries(rowID, columnName);
                this.summaryCacheMap.clear();
                return;
            }
            this.removeAllTreeGridSummaries(rowID, columnName);
        } else if (this.isHierarchicalGrid) {
            if (this.grid.transactions.enabled && this.deleteOperation) {
                this.deleteOperation = false;
                this.summaryCacheMap.clear();
            }
        } else {
           const summaryIds = this.getSummaryID(rowID, this.grid.groupingExpressions);
           summaryIds.forEach(id => {
               this.deleteSummaryCache(id, columnName);
           });
        }
    }

    public removeSummariesCachePerColumn(columnName) {
        this.summaryCacheMap.forEach((cache) => {
            if (cache.get(columnName)) {
                cache.delete(columnName);
            }
        });
        if (this.grid.rootSummariesEnabled) {  this.retriggerRootPipe++; }
    }

    public calcMaxSummaryHeight() {
        if (this.summaryHeight) {
            return this.summaryHeight;
        }
        if (!this.grid.data) {return this.summaryHeight = 0; }
        let maxSummaryLength = 0;
{'fraction_non_alphanumeric': 0.12002820874471086, 'fraction_numerical': 0.0026798307475317347, 'mean_word_length': 2.1431737588652484, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5565264', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1962, 'n_words': 348}