"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExportModule = void 0;
const tslib_1 = require("tslib");
const AdaptableModuleBase_1 = require("./AdaptableModuleBase");
const ModuleConstants = tslib_1.__importStar(require("../Utilities/Constants/ModuleConstants"));
const ExportRedux = tslib_1.__importStar(require("../Redux/ActionsReducers/ExportRedux"));
const NewReportWizard_1 = require("../View/Export/Wizard/NewReportWizard");
const getExportColumnsViewItems_1 = require("./Utilities/Export/getExportColumnsViewItems");
const getExportRowsViewItems_1 = require("./Utilities/Export/getExportRowsViewItems");
const getObjectTagsViewItems_1 = require("../Utilities/getObjectTagsViewItems");
const ExportStatusBar_1 = require("../View/Export/ExportStatusBar");
const ReportListItem_1 = require("../View/Export/ReportListItem");
class ExportModule extends AdaptableModuleBase_1.AdaptableModuleBase {
    constructor(api) {
        super(ModuleConstants.ExportModuleId, ModuleConstants.ExportFriendlyName, 'export-data', 'ExportPopup', 'Export data from the Grid to numerous locations in numerous formatso', api);
    }
    getModuleAdaptableObjects() {
        return this.api.exportApi.getAllReports();
    }
    getExplicitlyReferencedColumnIds(report) {
        if (report.ReportColumnScope === 'ScopeColumns' &&
            this.api.columnScopeApi.scopeHasColumns(report.Scope)) {
            return this.api.columnScopeApi
                .getColumnsInScope(report.Scope)
                .map((adaptableColumn) => adaptableColumn.columnId);
        }
        return [];
    }
    getReferencedNamedQueryNames(report) {
        if (!report.Query) {
            return [];
        }
        return this.api.namedQueryApi.internalApi.getReferencedNamedQueryNames(report.Query.BooleanExpression);
    }
    createContextMenuItems(menuContext) {
        const canExport = !menuContext.isRowGroupColumn && this.isModuleVisible();
        if (!canExport) {
            return;
        }
        let returnMenuItems = [];
        const allReportNames = this.api.exportApi.internalApi.getAllAvailableReportNames();
        const allReportFormats = this.api.exportApi.getAvailableSystemFormats();
        allReportNames.forEach((reportName) => {
            const formatItems = allReportFormats.map((reportFormat) => {
                const supportedDestinations = this.api.exportApi.getSupportedExportDestinations(reportFormat);
                const exportDestinationItems = supportedDestinations.map((exportDestination) => this.createMenuItemClickFunction(this.getMenuItemName(reportName, reportFormat, exportDestination), exportDestination, this.getDestinationMenuItemIcon(exportDestination), () => this.api.exportApi.exportReport(reportName, reportFormat, exportDestination)));
                const formatMenuItemGroup = {
                    name: this.getMenuItemName(reportName, reportFormat),
                    label: reportFormat,
                    isVisible: true,
                    category: 'Export',
                    icon: {
                        name: this.getFormatMenuItemIcon(reportFormat),
                    },
                    subItems: exportDestinationItems,
                };
                return formatMenuItemGroup;
            });
            const reportMenuItemGroup = {
                name: this.getMenuItemName(reportName),
                label: reportName,
                isVisible: true,
                category: 'Export',
                icon: {
                    name: 'export-data',
                },
                subItems: formatItems,
            };
            returnMenuItems.push(reportMenuItemGroup);
        });
        return returnMenuItems;
    }
    getMenuItemName(reportName, reportFormat, exportDestination) {
        // convert to lowercase and replace empty spaces with hyphens
        const adjustName = (name = '') => {
            const result = name.replace(/ /g, '-');
            return result.toLowerCase();
        };
        let name = `export-${adjustName(reportName)}`;
        if (reportFormat) {
            name += `-${adjustName(reportFormat)}`;
        }
        if (exportDestination) {
            name += `-${adjustName(exportDestination)}`;
        }
        return name;
    }
    getDestinationMenuItemIcon(exportDestination) {
        switch (exportDestination) {
            case 'Download':
                return 'downloaded';
            case 'Clipboard':
                return 'clipboard';
            default:
                return 'export-data';
        }
    }
    getFormatMenuItemIcon(reportFormat) {
        switch (reportFormat) {
            case 'Excel':
                return 'excel';
            case 'VisualExcel':
                return 'color-palette';
            case 'CSV':
                return 'csv';
            case 'JSON':
                return 'json';
            default:
                return 'export';
        }
    }
    getTeamSharingAction() {
        return {
            ModuleEntities: this.api.exportApi.getAllReports(),
            AddAction: ExportRedux.ReportAdd,
            EditAction: ExportRedux.ReportEdit,
        };
    }
    toView(report) {
        const isSystemReport = this.api.exportApi.internalApi.isSystemReport(report.Name);
        if (isSystemReport) {
            return {
                items: [
                    {
                        name: 'Report',
                        values: [report.Name],
                    },
                    (0, getObjectTagsViewItems_1.getObjectTagsViewItems)(report, this.api),
                ],
                abObject: report,
            };
        }
        return {
            items: [
                {
                    name: 'Report',
                    values: [report.Name],
                },
                (0, getExportColumnsViewItems_1.getExportColumnsViewItems)(report, this.api),
                (0, getExportRowsViewItems_1.getExportRowsViewItems)(report, this.api),
                (0, getObjectTagsViewItems_1.getObjectTagsViewItems)(report, this.api),
            ],
            abObject: report,
        };
    }
    toViewAll() {
        return this.getModuleAdaptableObjects().map((report) => this.toView(report));
    }
    getViewProperties() {
        return {
            newTooltipText: 'Create New Report',
            actions: [ReportListItem_1.ReportListItem],
            getDeleteAction: ExportRedux.ReportDelete,
            getEditWizard() {
                return NewReportWizard_1.NewReportWizard;
            },
            getStatusBarPanelProps: () => {
                return {
                    content: ExportStatusBar_1.ExportStatusBar,
                    triggerActionOnWrapperClick: false,
                };
            },
        };
    }
}
exports.ExportModule = ExportModule;
