/**
 * DevExtreme (esm/exporter/exceljs/export.js)
 * Version: 22.2.7 (build 23185-0311)
 * Build date: Tue Jul 04 2023
 *
 * Copyright (c) 2012 - 2023 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import {
    isDefined,
    isString,
    isDate,
    isObject
} from "../../core/utils/type";
import {
    ExportFormat
} from "./export_format";
import {
    extend
} from "../../core/utils/extend";
import {
    ExportLoadPanel
} from "../common/export_load_panel";
import {
    hasWindow
} from "../../core/utils/window";
var MAX_DIGIT_WIDTH_IN_PIXELS = 7;
var MAX_EXCEL_COLUMN_WIDTH = 255;
export var Export = {
    getFullOptions(options) {
        var fullOptions = extend({}, options);
        if (!(isDefined(fullOptions.worksheet) && isObject(fullOptions.worksheet))) {
            throw Error('The "worksheet" field must contain an object.')
        }
        if (!isDefined(fullOptions.topLeftCell)) {
            fullOptions.topLeftCell = {
                row: 1,
                column: 1
            }
        } else if (isString(fullOptions.topLeftCell)) {
            var {
                row: row,
                col: col
            } = fullOptions.worksheet.getCell(fullOptions.topLeftCell);
            fullOptions.topLeftCell = {
                row: row,
                column: col
            }
        }
        if (!isDefined(fullOptions.keepColumnWidths)) {
            fullOptions.keepColumnWidths = true
        }
        if (!isDefined(fullOptions.loadPanel)) {
            fullOptions.loadPanel = {}
        }
        if (!isDefined(fullOptions.loadPanel.enabled)) {
            fullOptions.loadPanel.enabled = true
        }
        if (!isDefined(fullOptions.encodeExecutableContent)) {
            fullOptions.encodeExecutableContent = false
        }
        return fullOptions
    },
    convertDateForExcelJS: date => new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds())),
    setNumberFormat(excelCell, numberFormat) {
        excelCell.numFmt = numberFormat
    },
    getCellStyles(dataProvider) {
        var styles = dataProvider.getStyles();
        styles.forEach(style => {
            var numberFormat = this.tryConvertToExcelNumberFormat(style.format, style.dataType);
            if (isDefined(numberFormat)) {
                numberFormat = numberFormat.replace(/&quot;/g, '"')
            }
            style.numberFormat = numberFormat
        });
        return styles
    },
    tryConvertToExcelNumberFormat(format, dataType) {
        var newFormat = ExportFormat.formatObjectConverter(format, dataType);
        var currency = newFormat.currency;
        format = newFormat.format;
        dataType = newFormat.dataType;
        return ExportFormat.convertFormat(format, newFormat.precision, dataType, currency)
    },
    setAlignment(excelCell, wrapText, horizontalAlignment) {
        var _excelCell$alignment;
        excelCell.alignment = null !== (_excelCell$alignment = excelCell.alignment) && void 0 !== _excelCell$alignment ? _excelCell$alignment : {};
        if (isDefined(wrapText)) {
            excelCell.alignment.wrapText = wrapText
        }
        if (isDefined(horizontalAlignment)) {
            excelCell.alignment.horizontal = horizontalAlignment
        }
        excelCell.alignment.vertical = "top"
    },
    setColumnsWidth(worksheet, widths, startColumnIndex) {
        if (!isDefined(widths)) {
            return
        }
        for (var i = 0; i < widths.length; i++) {
            var columnWidth = widths[i];
            if ("number" === typeof columnWidth && isFinite(columnWidth)) {
                worksheet.getColumn(startColumnIndex + i).width = Math.min(MAX_EXCEL_COLUMN_WIDTH, Math.floor(columnWidth / MAX_DIGIT_WIDTH_IN_PIXELS * 100) / 100)
            }
        }
    },
    export (options, Helpers, getLoadPanelTargetElement, getLoadPanelContainer) {
        var _component$_getIntern;
        var {
            component: component,
            worksheet: worksheet,
            topLeftCell: topLeftCell,
            keepColumnWidths: keepColumnWidths,
            selectedRowsOnly: selectedRowsOnly,
            loadPanel: loadPanel,
            encodeExecutableContent: encodeExecutableContent
        } = options;
        var dataProvider = component.getDataProvider(selectedRowsOnly);
        var internalComponent = (null === (_component$_getIntern = component._getInternalInstance) || void 0 === _component$_getIntern ? void 0 : _component$_getIntern.call(component)) || component;
        var initialLoadPanelEnabledOption = internalComponent.option("loadPanel") && internalComponent.option("loadPanel").enabled;
        if (initialLoadPanelEnabledOption) {
            component.option("loadPanel.enabled", false)
        }
        var exportLoadPanel;
        if (loadPanel.enabled && hasWindow()) {
            var $targetElement = getLoadPanelTargetElement(component);
            var $container = getLoadPanelContainer(component);
            exportLoadPanel = new ExportLoadPanel(component, $targetElement, $container, loadPanel);
            exportLoadPanel.show()
        }
        var wrapText = !!component.option("wordWrapEnabled");
        worksheet.properties.outlineProperties = {
            summaryBelow: false,
            summaryRight: false
        };
        var cellRange = {
            from: {
                row: topLeftCell.row,
                column: topLeftCell.column
            },
            to: {
                row: topLeftCell.row,
                column: topLeftCell.column
            }
        };
        return new Promise(resolve => {
            dataProvider.ready().done(() => {
                var columns = dataProvider.getColumns();
                var dataRowsCount = dataProvider.getRowsCount();
                var helpers = new Helpers(component, dataProvider, worksheet, options);
                if (keepColumnWidths) {
                    this.setColumnsWidth(worksheet, dataProvider.getColumnsWidths(), cellRange.from.column)
                }
                helpers._exportAllFieldHeaders(columns, this.setAlignment);
                var fieldHeaderRowsCount = helpers._getFieldHeaderRowsCount();
                cellRange.to.row = cellRange.from.row + fieldHeaderRowsCount;
                var styles = this.getCellStyles(dataProvider);
                for (var rowIndex = 0; rowIndex < dataRowsCount; rowIndex++) {
                    var currentRowIndex = cellRange.from.row + fieldHeaderRowsCount + rowIndex;
                    var row = worksheet.getRow(currentRowIndex);
                    var startColumnIndex = 0;
                    if (helpers._isRowFieldHeadersRow(rowIndex)) {
                        startColumnIndex = dataProvider.getRowAreaColCount();
                        helpers._exportFieldHeaders("row", currentRowIndex, 0, startColumnIndex, this.setAlignment)
                    }
                    helpers._trySetOutlineLevel(row, rowIndex);
                    this.exportRow(dataProvider, helpers, row, rowIndex, startColumnIndex, columns.length, wrapText, styles, encodeExecutableContent);
                    cellRange.to.row = currentRowIndex
                }
                helpers.mergedRangesManager.applyMergedRages();
                cellRange.to.column += columns.length > 0 ? columns.length - 1 : 0;
                var worksheetViewSettings = worksheet.views[0] || {};
                if (component.option("rtlEnabled")) {
                    worksheetViewSettings.rightToLeft = true
                }
                if (helpers._isFrozenZone(dataProvider)) {
                    if (-1 === Object.keys(worksheetViewSettings).indexOf("state")) {
                        extend(worksheetViewSettings, helpers._getWorksheetFrozenState(cellRange))
                    }
                    helpers._trySetAutoFilter(cellRange)
                }
                if (Object.keys(worksheetViewSettings).length > 0) {
                    worksheet.views = [worksheetViewSettings]
                }
                resolve(cellRange)
            }).always(() => {
                if (initialLoadPanelEnabledOption) {
                    component.option("loadPanel.enabled", initialLoadPanelEnabledOption)
                }
                if (loadPanel.enabled && hasWindow()) {
                    exportLoadPanel.dispose()
                }
            })
        })
    },
    exportRow(dataProvider, helpers, row, rowIndex, startColumnIndex, columnsCount, wrapText, styles, encodeExecutableContent) {
        for (var cellIndex = startColumnIndex; cellIndex < columnsCount; cellIndex++) {
            var cellData = dataProvider.getCellData(rowIndex, cellIndex, true);
            var excelCell = row.getCell(helpers._getFirstColumnIndex() + cellIndex);
            helpers.mergedRangesManager.updateMergedRanges(excelCell, rowIndex, cellIndex, helpers);
            var cellInfo = helpers.mergedRangesManager.findMergedCellInfo(rowIndex, cellIndex, helpers._isHeaderCell(rowIndex, cellIndex));
            if (isDefined(cellInfo) && excelCell !== cellInfo.masterCell) {
                excelCell.style = cellInfo.masterCell.style;
                excelCell.value = cellInfo.masterCell.value
            } else {
                if (isDate(cellData.value)) {
                    excelCell.value = this.convertDateForExcelJS(cellData.value)
                } else {
                    excelCell.value = cellData.value
                }
                if (isDefined(excelCell.value)) {
                    var {
                        bold: bold,
                        alignment: horizontalAlignment,
                        numberFormat: numberFormat
                    } = styles[dataProvider.getStyleId(rowIndex, cellIndex)];
                    if (isDefined(numberFormat)) {
                        this.setNumberFormat(excelCell, numberFormat)
                    } else if (isString(excelCell.value) && /^[@=+-]/.test(excelCell.value)) {
                        this.setNumberFormat(excelCell, "@")
                    }
                    helpers._trySetFont(excelCell, bold);
                    this.setAlignment(excelCell, wrapText, horizontalAlignment)
                }
            }
            helpers._customizeCell(excelCell, cellData.cellSourceData);
            if (encodeExecutableContent) {
                excelCell.value = ExportFormat.encode(excelCell.value)
            }
        }
    }
};
