/**
 * DevExtreme (esm/__internal/grids/pivot_grid/export/m_export.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import _extends from "@babel/runtime/helpers/esm/extends";
import Class from "../../../../core/class";
import {
    Deferred,
    when
} from "../../../../core/utils/deferred";
import {
    extend
} from "../../../../core/utils/extend";
import {
    each
} from "../../../../core/utils/iterator";
import {
    getDefaultAlignment
} from "../../../../core/utils/position";
import {
    isDefined,
    isFunction
} from "../../../../core/utils/type";
import {
    hasWindow
} from "../../../../core/utils/window";
import formatHelper from "../../../../format_helper";
import localizationNumber from "../../../../localization/number";
import {
    prepareItems
} from "../../../grids/grid_core/m_export";
var DEFAULT_DATA_TYPE = "string";
var DEFAUL_COLUMN_WIDTH = 100;
var ExportController = {
    exportTo() {
        var onExporting = this._createActionByOption("onExporting");
        var eventArgs = {
            rtlEnabled: this.option("rtlEnabled"),
            fileName: "PivotGrid",
            cancel: false
        };
        isFunction(onExporting) && onExporting(eventArgs)
    },
    _getLength(items) {
        var i;
        var itemCount = items[0].length;
        var cellCount = 0;
        for (i = 0; i < itemCount; i += 1) {
            cellCount += items[0][i].colspan || 1
        }
        return cellCount
    },
    _correctCellsInfoItemLengths(cellsInfo, expectedLength) {
        for (var i = 0; i < cellsInfo.length; i += 1) {
            while (cellsInfo[i].length < expectedLength) {
                cellsInfo[i].push({})
            }
        }
        return cellsInfo
    },
    _calculateCellInfoItemLength(columnsRow) {
        var result = 0;
        for (var columnIndex = 0; columnIndex < columnsRow.length; columnIndex += 1) {
            result += isDefined(columnsRow[columnIndex].colspan) ? columnsRow[columnIndex].colspan : 1
        }
        return result
    },
    _getEmptyCell: () => ({
        text: "",
        value: void 0,
        colspan: 1,
        rowspan: 1
    }),
    _getAllItems(columnsInfo, rowsInfoItems, cellsInfo) {
        var cellIndex;
        var rowIndex;
        var correctedCellsInfo = cellsInfo;
        var rowsLength = this._getLength(rowsInfoItems);
        var headerRowsCount = columnsInfo.length;
        if (columnsInfo.length > 0 && columnsInfo[0].length > 0 && cellsInfo.length > 0 && 0 === cellsInfo[0].length) {
            var cellInfoItemLength = this._calculateCellInfoItemLength(columnsInfo[0]);
            if (cellInfoItemLength > 0) {
                correctedCellsInfo = this._correctCellsInfoItemLengths(cellsInfo, cellInfoItemLength)
            }
        }
        if (0 === correctedCellsInfo.length) {
            var rowsCount = rowsInfoItems.length;
            var collapsedColumnCount = columnsInfo.map(headerRowWithColumns => headerRowWithColumns.filter(row => !row.expanded).length).reduce((result, collapsedCount) => result + collapsedCount, 0);
            for (var rowIdx = 0; rowIdx < rowsCount; rowIdx += 1) {
                correctedCellsInfo[rowIdx] = [];
                for (var colIdx = 0; colIdx < collapsedColumnCount; colIdx += 1) {
                    correctedCellsInfo[rowIdx][colIdx] = this._getEmptyCell()
                }
            }
        }
        var sourceItems = columnsInfo.concat(correctedCellsInfo);
        for (rowIndex = 0; rowIndex < rowsInfoItems.length; rowIndex += 1) {
            for (cellIndex = rowsInfoItems[rowIndex].length - 1; cellIndex >= 0; cellIndex -= 1) {
                if (!isDefined(sourceItems[rowIndex + headerRowsCount])) {
                    sourceItems[rowIndex + headerRowsCount] = []
                }
                sourceItems[rowIndex + headerRowsCount].splice(0, 0, extend({}, rowsInfoItems[rowIndex][cellIndex]))
            }
        }
        sourceItems[0].splice(0, 0, extend({}, this._getEmptyCell(), {
            alignment: getDefaultAlignment(this._options.rtlEnabled),
            colspan: rowsLength,
            rowspan: headerRowsCount
        }));
        return prepareItems(sourceItems, this._getEmptyCell())
    },
    getDataProvider() {
        return new DataProvider(this)
    }
};
var DataProvider = Class.inherit({
    ctor(exportController) {
        this._exportController = exportController
    },
    ready() {
        this._initOptions();
        var options = this._options;
        return when(options.items).done(items => {
            var headerSize = items[0][0].rowspan;
            var columns = items[headerSize - 1];
            each(columns, (_, column) => {
                column.width = DEFAUL_COLUMN_WIDTH
            });
            options.columns = columns;
            options.items = items
        })
    },
    _initOptions() {
        var exportController = this._exportController;
        var dataController = exportController._dataController;
        var items = new Deferred;
        dataController.beginLoading();
        setTimeout(() => {
            var columnsInfo = extend(true, [], dataController.getColumnsInfo(true));
            var rowsInfoItems = extend(true, [], dataController.getRowsInfo(true));
            var cellsInfo = dataController.getCellsInfo(true);
            items.resolve(exportController._getAllItems(columnsInfo, rowsInfoItems, cellsInfo));
            dataController.endLoading()
        });
        this._options = {
            items: items,
            rtlEnabled: exportController.option("rtlEnabled"),
            dataFields: exportController.getDataSource().getAreaFields("data"),
            rowsArea: exportController._rowsArea,
            columnsArea: exportController._columnsArea
        }
    },
    getColumns() {
        return this._options.columns
    },
    getColumnsWidths() {
        var colsArea = this._options.columnsArea;
        var {
            rowsArea: rowsArea
        } = this._options;
        var {
            columns: columns
        } = this._options;
        var useDefaultWidth = !hasWindow() || "virtual" === colsArea.option("scrolling.mode") || colsArea.element().is(":hidden");
        return useDefaultWidth ? columns.map(() => DEFAUL_COLUMN_WIDTH) : rowsArea.getColumnsWidth().concat(colsArea.getColumnsWidth())
    },
    getRowsCount() {
        return this._options.items.length
    },
    getGroupLevel: () => 0,
    getCellMerging(rowIndex, cellIndex) {
        var {
            items: items
        } = this._options;
        var item = items[rowIndex] && items[rowIndex][cellIndex];
        return item ? {
            colspan: item.colspan - 1,
            rowspan: item.rowspan - 1
        } : {
            colspan: 0,
            rowspan: 0
        }
    },
    getFrozenArea() {
        return {
            x: this.getRowAreaColCount(),
            y: this.getColumnAreaRowCount()
        }
    },
    getCellType(rowIndex, cellIndex) {
        var style = this.getStyles()[this.getStyleId(rowIndex, cellIndex)];
        return style && style.dataType || "string"
    },
    getCellData(rowIndex, cellIndex, isExcelJS) {
        var result = {};
        var {
            items: items
        } = this._options;
        var item = items[rowIndex] && items[rowIndex][cellIndex] || {};
        if (isExcelJS) {
            result.cellSourceData = item;
            var areaName = this._tryGetAreaName(item, rowIndex, cellIndex);
            if (areaName) {
                result.cellSourceData.area = areaName
            }
            result.cellSourceData.rowIndex = rowIndex;
            result.cellSourceData.columnIndex = cellIndex
        }
        if ("string" === this.getCellType(rowIndex, cellIndex)) {
            result.value = item.text
        } else {
            result.value = item.value
        }
        if (result.cellSourceData && result.cellSourceData.isWhiteSpace) {
            result.value = ""
        }
        return result
    },
    _tryGetAreaName(item, rowIndex, cellIndex) {
        if (this.isColumnAreaCell(rowIndex, cellIndex)) {
            return "column"
        }
        if (this.isRowAreaCell(rowIndex, cellIndex)) {
            return "row"
        }
        if (isDefined(item.dataIndex)) {
            return "data"
        }
        return
    },
    isRowAreaCell(rowIndex, cellIndex) {
        return rowIndex >= this.getColumnAreaRowCount() && cellIndex < this.getRowAreaColCount()
    },
    isColumnAreaCell(rowIndex, cellIndex) {
        return cellIndex >= this.getRowAreaColCount() && rowIndex < this.getColumnAreaRowCount()
    },
    getColumnAreaRowCount() {
        return this._options.items[0][0].rowspan
    },
    getRowAreaColCount() {
        return this._options.items[0][0].colspan
    },
    getHeaderStyles() {
        return [{
            alignment: "center",
            dataType: "string"
        }, {
            alignment: getDefaultAlignment(this._options.rtlEnabled),
            dataType: "string"
        }]
    },
    getDataFieldStyles() {
        var {
            dataFields: dataFields
        } = this._options;
        var dataItemStyle = {
            alignment: this._options.rtlEnabled ? "left" : "right"
        };
        var dataFieldStyles = [];
        if (dataFields.length) {
            dataFields.forEach(dataField => {
                dataFieldStyles.push(_extends(_extends({}, dataItemStyle), {
                    format: dataField.format,
                    dataType: this.getCellDataType(dataField)
                }))
            });
            return dataFieldStyles
        }
        return [dataItemStyle]
    },
    getStyles() {
        if (this._styles) {
            return this._styles
        }
        this._styles = [...this.getHeaderStyles(), ...this.getDataFieldStyles()];
        return this._styles
    },
    getCellDataType(field) {
        if (field && field.customizeText) {
            return "string"
        }
        if (field.dataType) {
            return field.dataType
        }
        if (field.format) {
            if (1 === localizationNumber.parse(formatHelper.format(1, field.format))) {
                return "number"
            }
            if (formatHelper.format(new Date, field.format)) {
                return "date"
            }
        }
        return DEFAULT_DATA_TYPE
    },
    getStyleId(rowIndex, cellIndex) {
        var {
            items: items
        } = this._options;
        var item = items[rowIndex] && items[rowIndex][cellIndex] || {};
        if (0 === cellIndex && 0 === rowIndex || this.isColumnAreaCell(rowIndex, cellIndex)) {
            return 0
        }
        if (this.isRowAreaCell(rowIndex, cellIndex)) {
            return 1
        }
        return this.getHeaderStyles().length + (item.dataIndex || 0)
    }
});
var PivotGridExport = {
    DEFAUL_COLUMN_WIDTH: DEFAUL_COLUMN_WIDTH
};
export default {
    ExportController: ExportController,
    PivotGridExport: PivotGridExport,
    DataProvider: DataProvider
};
export {
    DataProvider,
    ExportController,
    PivotGridExport
};
