/**
 * DevExtreme (esm/exporter/exceljs/export_merged_ranges_manager.js)
 * Version: 23.1.6 (build 23316-0110)
 * Build date: Sun Nov 12 2023
 *
 * Copyright (c) 2012 - 2023 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
class MergedRangesManager {
    constructor(dataProvider, worksheet) {
        this.dataProvider = dataProvider;
        this.worksheet = worksheet;
        this.mergedCells = [];
        this.mergedRanges = []
    }
    updateMergedRanges(excelCell, rowIndex, cellIndex, helpers) {
        if (helpers._isHeaderCell(rowIndex, cellIndex) && !this.isCellInMergedRanges(rowIndex, cellIndex)) {
            var {
                rowspan: rowspan,
                colspan: colspan
            } = this.dataProvider.getCellMerging(rowIndex, cellIndex);
            var isMasterCellOfMergedRange = colspan || rowspan;
            if (isMasterCellOfMergedRange) {
                var allowToMergeRange = helpers._allowToMergeRange(rowIndex, cellIndex, rowspan, colspan);
                this.updateMergedCells(excelCell, rowIndex, cellIndex, rowspan, colspan);
                if (allowToMergeRange) {
                    var shouldReduceInfoRange = helpers._isInfoCell(rowIndex, cellIndex) && helpers._allowExportRowFieldHeaders();
                    this.mergedRanges.push({
                        masterCell: excelCell,
                        rowspan: rowspan - (shouldReduceInfoRange && rowspan > 0),
                        colspan: colspan
                    })
                }
            }
        }
    }
    isCellInMergedRanges(rowIndex, cellIndex) {
        return this.mergedCells[rowIndex] && this.mergedCells[rowIndex][cellIndex]
    }
    findMergedCellInfo(rowIndex, cellIndex, isHeaderCell) {
        if (isHeaderCell && this.isCellInMergedRanges(rowIndex, cellIndex)) {
            return this.mergedCells[rowIndex][cellIndex]
        }
    }
    updateMergedCells(excelCell, rowIndex, cellIndex, rowspan, colspan) {
        for (var i = rowIndex; i <= rowIndex + rowspan; i++) {
            for (var j = cellIndex; j <= cellIndex + colspan; j++) {
                if (!this.mergedCells[i]) {
                    this.mergedCells[i] = []
                }
                this.mergedCells[i][j] = {
                    masterCell: excelCell
                }
            }
        }
    }
    addMergedRange(masterCell, rowspan, colspan) {
        this.mergedRanges.push({
            masterCell: masterCell,
            rowspan: rowspan,
            colspan: colspan
        })
    }
    applyMergedRages() {
        this.mergedRanges.forEach(range => {
            var startRowIndex = range.masterCell.fullAddress.row;
            var startColumnIndex = range.masterCell.fullAddress.col;
            var endRowIndex = startRowIndex + range.rowspan;
            var endColumnIndex = startColumnIndex + range.colspan;
            this.worksheet.mergeCells(startRowIndex, startColumnIndex, endRowIndex, endColumnIndex)
        })
    }
}
export {
    MergedRangesManager
};
