/**
 * DevExtreme (cjs/exporter/exceljs/export_pivot_grid.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/
 */
"use strict";
exports.exportPivotGrid = exportPivotGrid;
var _type = require("../../core/utils/type");
var _export = require("./export");
var _position = require("../../core/utils/position");
var _inflector = require("../../core/utils/inflector");
var _export_merged_ranges_manager = require("./export_merged_ranges_manager");

function _extends() {
    _extends = Object.assign ? Object.assign.bind() : function(target) {
        for (var i = 1; i < arguments.length; i++) {
            var source = arguments[i];
            for (var key in source) {
                if (Object.prototype.hasOwnProperty.call(source, key)) {
                    target[key] = source[key]
                }
            }
        }
        return target
    };
    return _extends.apply(this, arguments)
}
const FIELD_HEADERS_SEPARATOR = ", ";
let PivotGridHelpers = function() {
    function PivotGridHelpers(component, dataProvider, worksheet, options) {
        this.component = component;
        this.dataProvider = dataProvider;
        this.worksheet = worksheet;
        this.mergedRangesManager = new _export_merged_ranges_manager.MergedRangesManager(dataProvider, worksheet);
        this.topLeftCell = options.topLeftCell;
        this.customizeCell = options.customizeCell;
        this.mergeColumnFieldValues = options.mergeColumnFieldValues;
        this.mergeRowFieldValues = options.mergeRowFieldValues;
        this.exportFilterFieldHeaders = options.exportFilterFieldHeaders;
        this.exportDataFieldHeaders = options.exportDataFieldHeaders;
        this.exportColumnFieldHeaders = options.exportColumnFieldHeaders;
        this.exportRowFieldHeaders = options.exportRowFieldHeaders;
        this.rtlEnabled = component.option("rtlEnabled");
        this.rowHeaderLayout = component.option("rowHeaderLayout");
        this.wrapText = !!component.option("wordWrapEnabled");
        this.filterFieldHeaders = this._tryGetFieldHeaders("filter");
        this.dataFieldHeaders = this._tryGetFieldHeaders("data");
        this.columnFieldHeaders = this._tryGetFieldHeaders("column");
        this.rowFieldHeaders = this._tryGetFieldHeaders("row")
    }
    var _proto = PivotGridHelpers.prototype;
    _proto._getFirstColumnIndex = function() {
        return this.topLeftCell.column
    };
    _proto._getWorksheetFrozenState = function(cellRange) {
        const {
            x: x,
            y: y
        } = this.dataProvider.getFrozenArea();
        return {
            state: "frozen",
            xSplit: cellRange.from.column + x - 1,
            ySplit: cellRange.from.row + y + this._getFieldHeaderRowsCount() - 1
        }
    };
    _proto._getFieldHeaderRowsCount = function() {
        return 0 + this._allowExportFilterFieldHeaders() + (this._allowExportDataFieldHeaders() || this._allowExportColumnFieldHeaders())
    };
    _proto._isFrozenZone = function() {
        return true
    };
    _proto._isHeaderCell = function(rowIndex, cellIndex) {
        return rowIndex < this.dataProvider.getColumnAreaRowCount() || cellIndex < this.dataProvider.getRowAreaColCount()
    };
    _proto._getDefaultFieldHeaderCellsData = function(value) {
        return {
            text: value,
            value: value
        }
    };
    _proto._isInfoCell = function(rowIndex, cellIndex) {
        return rowIndex < this.dataProvider.getColumnAreaRowCount() && cellIndex < this.dataProvider.getRowAreaColCount()
    };
    _proto._allowToMergeRange = function(rowIndex, cellIndex, rowspan, colspan) {
        return !(this.dataProvider.isColumnAreaCell(rowIndex, cellIndex) && !this.mergeColumnFieldValues && !!colspan || this.dataProvider.isRowAreaCell(rowIndex, cellIndex) && !this.mergeRowFieldValues && !!rowspan)
    };
    _proto._trySetAutoFilter = function() {};
    _proto._trySetFont = function(excelCell, bold) {
        if ((0, _type.isDefined)(bold)) {
            excelCell.font = excelCell.font || {};
            excelCell.font.bold = bold
        }
    };
    _proto._getFieldHeaderStyles = function() {
        const borderStyle = {
            style: "thin",
            color: {
                argb: "FF7E7E7E"
            }
        };
        return {
            alignment: (0, _position.getDefaultAlignment)(this.rtlEnabled),
            bold: true,
            border: {
                bottom: borderStyle,
                left: borderStyle,
                right: borderStyle,
                top: borderStyle
            }
        }
    };
    _proto._trySetOutlineLevel = function() {};
    _proto._getAllFieldHeaders = function() {
        return this.dataProvider._exportController.getDataSource()._descriptions
    };
    _proto._tryGetFieldHeaders = function(area) {
        if (!this["export".concat((0, _inflector.camelize)(area, true), "FieldHeaders")]) {
            return []
        }
        const fields = this._getAllFieldHeaders()["data" === area ? "values" : "".concat(area, "s")].filter(fieldHeader => fieldHeader.area === area);
        if ("right" === (0, _position.getDefaultAlignment)(this.rtlEnabled)) {
            fields.sort((a, b) => b.areaIndex - a.areaIndex)
        }
        return fields.map(field => field.caption)
    };
    _proto._customizeCell = function(excelCell, pivotCell, shouldPreventCall) {
        if ((0, _type.isFunction)(this.customizeCell) && !shouldPreventCall) {
            this.customizeCell({
                excelCell: excelCell,
                pivotCell: pivotCell
            })
        }
    };
    _proto._isRowFieldHeadersRow = function(rowIndex) {
        const isLastInfoRangeCell = this._isInfoCell(rowIndex, 0) && "row" === this.dataProvider.getCellData(rowIndex + 1, 0, true).cellSourceData.area;
        return this._allowExportRowFieldHeaders() && isLastInfoRangeCell
    };
    _proto._exportAllFieldHeaders = function(columns, setAlignment) {
        const totalCellsCount = columns.length;
        const rowAreaColCount = this.dataProvider.getRowAreaColCount();
        let rowIndex = this.topLeftCell.row;
        if (this._allowExportFilterFieldHeaders()) {
            this._exportFieldHeaders("filter", rowIndex, 0, totalCellsCount, setAlignment);
            rowIndex++
        }
        if (this._allowExportDataFieldHeaders()) {
            this._exportFieldHeaders("data", rowIndex, 0, rowAreaColCount, setAlignment);
            if (!this._allowExportColumnFieldHeaders()) {
                this._exportFieldHeaders("column", rowIndex, rowAreaColCount, totalCellsCount - rowAreaColCount, setAlignment)
            }
        }
        if (this._allowExportColumnFieldHeaders()) {
            if (!this._allowExportDataFieldHeaders()) {
                this._exportFieldHeaders("data", rowIndex, 0, rowAreaColCount, setAlignment)
            }
            this._exportFieldHeaders("column", rowIndex, rowAreaColCount, totalCellsCount - rowAreaColCount, setAlignment)
        }
    };
    _proto._exportFieldHeaders = function(area, rowIndex, startColumnIndex, totalColumnsCount, setAlignment) {
        const fieldHeaders = this["".concat(area, "FieldHeaders")];
        const row = this.worksheet.getRow(rowIndex);
        const shouldMergeHeaderField = "row" !== area || "row" === area && "tree" === this.rowHeaderLayout;
        if (shouldMergeHeaderField) {
            this.mergedRangesManager.addMergedRange(row.getCell(this.topLeftCell.column + startColumnIndex), 0, totalColumnsCount - 1)
        }
        for (let cellIndex = 0; cellIndex < totalColumnsCount; cellIndex++) {
            const excelCell = row.getCell(this.topLeftCell.column + startColumnIndex + cellIndex);
            const values = fieldHeaders;
            let cellData = [];
            const value = values.length > totalColumnsCount || shouldMergeHeaderField ? values.join(", ") : values[cellIndex];
            cellData = _extends({}, this._getDefaultFieldHeaderCellsData(value), {
                headerType: area
            });
            excelCell.value = value;
            this._applyHeaderStyles(excelCell, setAlignment);
            this._customizeCell(excelCell, cellData)
        }
    };
    _proto._applyHeaderStyles = function(excelCell, setAlignment) {
        const {
            bold: bold,
            alignment: alignment,
            border: border
        } = this._getFieldHeaderStyles();
        this._trySetFont(excelCell, bold);
        setAlignment(excelCell, this.wrapText, alignment);
        excelCell.border = border
    };
    _proto._allowExportRowFieldHeaders = function() {
        return this.rowFieldHeaders.length > 0
    };
    _proto._allowExportFilterFieldHeaders = function() {
        return this.filterFieldHeaders.length > 0
    };
    _proto._allowExportDataFieldHeaders = function() {
        return this.dataFieldHeaders.length > 0
    };
    _proto._allowExportColumnFieldHeaders = function() {
        return this.columnFieldHeaders.length > 0
    };
    return PivotGridHelpers
}();

function exportPivotGrid(options) {
    return _export.Export.export(_getFullOptions(options), PivotGridHelpers, _getLoadPanelTargetElement, _getLoadPanelContainer)
}

function _getFullOptions(options) {
    if (!((0, _type.isDefined)(options) && (0, _type.isObject)(options))) {
        throw Error('The "exportPivotGrid" method requires a configuration object.')
    }
    if (!((0, _type.isDefined)(options.component) && (0, _type.isObject)(options.component) && "dxPivotGrid" === options.component.NAME)) {
        throw Error('The "component" field must contain a PivotGrid instance.')
    }
    if (!(0, _type.isDefined)(options.mergeRowFieldValues)) {
        options.mergeRowFieldValues = true
    }
    if (!(0, _type.isDefined)(options.mergeColumnFieldValues)) {
        options.mergeColumnFieldValues = true
    }
    if (!(0, _type.isDefined)(options.exportDataFieldHeaders)) {
        options.exportDataFieldHeaders = false
    }
    if (!(0, _type.isDefined)(options.exportRowFieldHeaders)) {
        options.exportRowFieldHeaders = false
    }
    if (!(0, _type.isDefined)(options.exportColumnFieldHeaders)) {
        options.exportColumnFieldHeaders = false
    }
    if (!(0, _type.isDefined)(options.exportFilterFieldHeaders)) {
        options.exportFilterFieldHeaders = false
    }
    return _export.Export.getFullOptions(options)
}

function _getLoadPanelTargetElement(component) {
    return component._dataArea.groupElement()
}

function _getLoadPanelContainer(component) {
    return component.$element()
}
